root.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "os"
  18. "github.com/spf13/cobra"
  19. "github.com/fatedier/frp/models/config"
  20. "github.com/fatedier/frp/server"
  21. "github.com/fatedier/frp/utils/log"
  22. "github.com/fatedier/frp/utils/util"
  23. "github.com/fatedier/frp/utils/version"
  24. )
  25. const (
  26. CfgFileTypeIni = iota
  27. CfgFileTypeCmd
  28. )
  29. var (
  30. cfgFile string
  31. showVersion bool
  32. bindAddr string
  33. bindPort int
  34. bindUdpPort int
  35. kcpBindPort int
  36. proxyBindAddr string
  37. vhostHttpPort int
  38. vhostHttpsPort int
  39. vhostHttpTimeout int64
  40. dashboardAddr string
  41. dashboardPort int
  42. dashboardUser string
  43. dashboardPwd string
  44. assetsDir string
  45. logFile string
  46. logLevel string
  47. logMaxDays int64
  48. disableLogColor bool
  49. token string
  50. subDomainHost string
  51. tcpMux bool
  52. allowPorts string
  53. maxPoolCount int64
  54. maxPortsPerClient int64
  55. )
  56. func init() {
  57. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file of frps")
  58. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
  59. rootCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "0.0.0.0", "bind address")
  60. rootCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "p", 7000, "bind port")
  61. rootCmd.PersistentFlags().IntVarP(&bindUdpPort, "bind_udp_port", "", 0, "bind udp port")
  62. rootCmd.PersistentFlags().IntVarP(&kcpBindPort, "kcp_bind_port", "", 0, "kcp bind udp port")
  63. rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "proxy_bind_addr", "", "0.0.0.0", "proxy bind address")
  64. rootCmd.PersistentFlags().IntVarP(&vhostHttpPort, "vhost_http_port", "", 0, "vhost http port")
  65. rootCmd.PersistentFlags().IntVarP(&vhostHttpsPort, "vhost_https_port", "", 0, "vhost https port")
  66. rootCmd.PersistentFlags().Int64VarP(&vhostHttpTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout")
  67. rootCmd.PersistentFlags().StringVarP(&dashboardAddr, "dashboard_addr", "", "0.0.0.0", "dasboard address")
  68. rootCmd.PersistentFlags().IntVarP(&dashboardPort, "dashboard_port", "", 0, "dashboard port")
  69. rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user")
  70. rootCmd.PersistentFlags().StringVarP(&dashboardPwd, "dashboard_pwd", "", "admin", "dashboard password")
  71. rootCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "log file")
  72. rootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  73. rootCmd.PersistentFlags().Int64VarP(&logMaxDays, "log_max_days", "", 3, "log max days")
  74. rootCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
  75. rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  76. rootCmd.PersistentFlags().StringVarP(&subDomainHost, "subdomain_host", "", "", "subdomain host")
  77. rootCmd.PersistentFlags().StringVarP(&allowPorts, "allow_ports", "", "", "allow ports")
  78. rootCmd.PersistentFlags().Int64VarP(&maxPortsPerClient, "max_ports_per_client", "", 0, "max ports per client")
  79. }
  80. var rootCmd = &cobra.Command{
  81. Use: "frps",
  82. Short: "frps is the server of frp (https://github.com/fatedier/frp)",
  83. RunE: func(cmd *cobra.Command, args []string) error {
  84. if showVersion {
  85. fmt.Println(version.Full())
  86. return nil
  87. }
  88. var cfg config.ServerCommonConf
  89. var err error
  90. if cfgFile != "" {
  91. var content string
  92. content, err = config.GetRenderedConfFromFile(cfgFile)
  93. if err != nil {
  94. return err
  95. }
  96. cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
  97. } else {
  98. cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "")
  99. }
  100. if err != nil {
  101. return err
  102. }
  103. err = runServer(cfg)
  104. if err != nil {
  105. fmt.Println(err)
  106. os.Exit(1)
  107. }
  108. return nil
  109. },
  110. }
  111. func Execute() {
  112. if err := rootCmd.Execute(); err != nil {
  113. os.Exit(1)
  114. }
  115. }
  116. func parseServerCommonCfg(fileType int, content string) (cfg config.ServerCommonConf, err error) {
  117. if fileType == CfgFileTypeIni {
  118. cfg, err = parseServerCommonCfgFromIni(content)
  119. } else if fileType == CfgFileTypeCmd {
  120. cfg, err = parseServerCommonCfgFromCmd()
  121. }
  122. if err != nil {
  123. return
  124. }
  125. err = cfg.Check()
  126. if err != nil {
  127. return
  128. }
  129. return
  130. }
  131. func parseServerCommonCfgFromIni(content string) (config.ServerCommonConf, error) {
  132. cfg, err := config.UnmarshalServerConfFromIni(content)
  133. if err != nil {
  134. return config.ServerCommonConf{}, err
  135. }
  136. return cfg, nil
  137. }
  138. func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
  139. cfg = config.GetDefaultServerConf()
  140. cfg.BindAddr = bindAddr
  141. cfg.BindPort = bindPort
  142. cfg.BindUdpPort = bindUdpPort
  143. cfg.KcpBindPort = kcpBindPort
  144. cfg.ProxyBindAddr = proxyBindAddr
  145. cfg.VhostHttpPort = vhostHttpPort
  146. cfg.VhostHttpsPort = vhostHttpsPort
  147. cfg.VhostHttpTimeout = vhostHttpTimeout
  148. cfg.DashboardAddr = dashboardAddr
  149. cfg.DashboardPort = dashboardPort
  150. cfg.DashboardUser = dashboardUser
  151. cfg.DashboardPwd = dashboardPwd
  152. cfg.LogFile = logFile
  153. cfg.LogLevel = logLevel
  154. cfg.LogMaxDays = logMaxDays
  155. cfg.Token = token
  156. cfg.SubDomainHost = subDomainHost
  157. if len(allowPorts) > 0 {
  158. // e.g. 1000-2000,2001,2002,3000-4000
  159. ports, errRet := util.ParseRangeNumbers(allowPorts)
  160. if errRet != nil {
  161. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  162. return
  163. }
  164. for _, port := range ports {
  165. cfg.AllowPorts[int(port)] = struct{}{}
  166. }
  167. }
  168. cfg.MaxPortsPerClient = maxPortsPerClient
  169. if logFile == "console" {
  170. cfg.LogWay = "console"
  171. } else {
  172. cfg.LogWay = "file"
  173. }
  174. cfg.DisableLogColor = disableLogColor
  175. return
  176. }
  177. func runServer(cfg config.ServerCommonConf) (err error) {
  178. log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
  179. svr, err := server.NewService(cfg)
  180. if err != nil {
  181. return err
  182. }
  183. log.Info("start frps success")
  184. svr.Run()
  185. return
  186. }