1
0

root.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/fatedier/frp/pkg/auth"
  19. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/util/log"
  21. "github.com/fatedier/frp/pkg/util/util"
  22. "github.com/fatedier/frp/pkg/util/version"
  23. "github.com/fatedier/frp/server"
  24. "github.com/spf13/cobra"
  25. )
  26. const (
  27. CfgFileTypeIni = iota
  28. CfgFileTypeCmd
  29. )
  30. var (
  31. cfgFile string
  32. showVersion bool
  33. bindAddr string
  34. bindPort int
  35. bindUDPPort int
  36. kcpBindPort int
  37. proxyBindAddr string
  38. vhostHTTPPort int
  39. vhostHTTPSPort int
  40. vhostHTTPTimeout int64
  41. dashboardAddr string
  42. dashboardPort int
  43. dashboardUser string
  44. dashboardPwd string
  45. enablePrometheus bool
  46. assetsDir string
  47. logFile string
  48. logLevel string
  49. logMaxDays int64
  50. disableLogColor bool
  51. token string
  52. subDomainHost string
  53. tcpMux bool
  54. allowPorts string
  55. maxPoolCount int64
  56. maxPortsPerClient int64
  57. tlsOnly bool
  58. )
  59. func init() {
  60. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file of frps")
  61. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frps")
  62. rootCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "0.0.0.0", "bind address")
  63. rootCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "p", 7000, "bind port")
  64. rootCmd.PersistentFlags().IntVarP(&bindUDPPort, "bind_udp_port", "", 0, "bind udp port")
  65. rootCmd.PersistentFlags().IntVarP(&kcpBindPort, "kcp_bind_port", "", 0, "kcp bind udp port")
  66. rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "proxy_bind_addr", "", "0.0.0.0", "proxy bind address")
  67. rootCmd.PersistentFlags().IntVarP(&vhostHTTPPort, "vhost_http_port", "", 0, "vhost http port")
  68. rootCmd.PersistentFlags().IntVarP(&vhostHTTPSPort, "vhost_https_port", "", 0, "vhost https port")
  69. rootCmd.PersistentFlags().Int64VarP(&vhostHTTPTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout")
  70. rootCmd.PersistentFlags().StringVarP(&dashboardAddr, "dashboard_addr", "", "0.0.0.0", "dasboard address")
  71. rootCmd.PersistentFlags().IntVarP(&dashboardPort, "dashboard_port", "", 0, "dashboard port")
  72. rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user")
  73. rootCmd.PersistentFlags().StringVarP(&dashboardPwd, "dashboard_pwd", "", "admin", "dashboard password")
  74. rootCmd.PersistentFlags().BoolVarP(&enablePrometheus, "enable_prometheus", "", false, "enable prometheus dashboard")
  75. rootCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "log file")
  76. rootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  77. rootCmd.PersistentFlags().Int64VarP(&logMaxDays, "log_max_days", "", 3, "log max days")
  78. rootCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
  79. rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  80. rootCmd.PersistentFlags().StringVarP(&subDomainHost, "subdomain_host", "", "", "subdomain host")
  81. rootCmd.PersistentFlags().StringVarP(&allowPorts, "allow_ports", "", "", "allow ports")
  82. rootCmd.PersistentFlags().Int64VarP(&maxPortsPerClient, "max_ports_per_client", "", 0, "max ports per client")
  83. rootCmd.PersistentFlags().BoolVarP(&tlsOnly, "tls_only", "", false, "frps tls only")
  84. }
  85. var rootCmd = &cobra.Command{
  86. Use: "frps",
  87. Short: "frps is the server of frp (https://github.com/fatedier/frp)",
  88. RunE: func(cmd *cobra.Command, args []string) error {
  89. if showVersion {
  90. fmt.Println(version.Full())
  91. return nil
  92. }
  93. var cfg config.ServerCommonConf
  94. var err error
  95. if cfgFile != "" {
  96. log.Info("frps uses config file: %s", cfgFile)
  97. var content string
  98. content, err = config.GetRenderedConfFromFile(cfgFile)
  99. if err != nil {
  100. return err
  101. }
  102. cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
  103. } else {
  104. log.Info("frps uses command line arguments for config")
  105. cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "")
  106. }
  107. if err != nil {
  108. return err
  109. }
  110. err = runServer(cfg)
  111. if err != nil {
  112. fmt.Println(err)
  113. os.Exit(1)
  114. }
  115. return nil
  116. },
  117. }
  118. func Execute() {
  119. if err := rootCmd.Execute(); err != nil {
  120. os.Exit(1)
  121. }
  122. }
  123. func parseServerCommonCfg(fileType int, content string) (cfg config.ServerCommonConf, err error) {
  124. if fileType == CfgFileTypeIni {
  125. cfg, err = parseServerCommonCfgFromIni(content)
  126. } else if fileType == CfgFileTypeCmd {
  127. cfg, err = parseServerCommonCfgFromCmd()
  128. }
  129. if err != nil {
  130. return
  131. }
  132. err = cfg.Check()
  133. if err != nil {
  134. return
  135. }
  136. return
  137. }
  138. func parseServerCommonCfgFromIni(content string) (config.ServerCommonConf, error) {
  139. cfg, err := config.UnmarshalServerConfFromIni(content)
  140. if err != nil {
  141. return config.ServerCommonConf{}, err
  142. }
  143. return cfg, nil
  144. }
  145. func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
  146. cfg = config.GetDefaultServerConf()
  147. cfg.BindAddr = bindAddr
  148. cfg.BindPort = bindPort
  149. cfg.BindUDPPort = bindUDPPort
  150. cfg.KCPBindPort = kcpBindPort
  151. cfg.ProxyBindAddr = proxyBindAddr
  152. cfg.VhostHTTPPort = vhostHTTPPort
  153. cfg.VhostHTTPSPort = vhostHTTPSPort
  154. cfg.VhostHTTPTimeout = vhostHTTPTimeout
  155. cfg.DashboardAddr = dashboardAddr
  156. cfg.DashboardPort = dashboardPort
  157. cfg.DashboardUser = dashboardUser
  158. cfg.DashboardPwd = dashboardPwd
  159. cfg.EnablePrometheus = enablePrometheus
  160. cfg.LogFile = logFile
  161. cfg.LogLevel = logLevel
  162. cfg.LogMaxDays = logMaxDays
  163. cfg.SubDomainHost = subDomainHost
  164. cfg.TLSOnly = tlsOnly
  165. // Only token authentication is supported in cmd mode
  166. cfg.ServerConfig = auth.GetDefaultServerConf()
  167. cfg.Token = token
  168. if len(allowPorts) > 0 {
  169. // e.g. 1000-2000,2001,2002,3000-4000
  170. ports, errRet := util.ParseRangeNumbers(allowPorts)
  171. if errRet != nil {
  172. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  173. return
  174. }
  175. for _, port := range ports {
  176. cfg.AllowPorts[int(port)] = struct{}{}
  177. }
  178. }
  179. cfg.MaxPortsPerClient = maxPortsPerClient
  180. if logFile == "console" {
  181. cfg.LogWay = "console"
  182. } else {
  183. cfg.LogWay = "file"
  184. }
  185. cfg.DisableLogColor = disableLogColor
  186. return
  187. }
  188. func runServer(cfg config.ServerCommonConf) (err error) {
  189. log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
  190. svr, err := server.NewService(cfg)
  191. if err != nil {
  192. return err
  193. }
  194. log.Info("frps started successfully")
  195. svr.Run()
  196. return
  197. }