root.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. var content []byte
  97. content, err = config.GetRenderedConfFromFile(cfgFile)
  98. if err != nil {
  99. return err
  100. }
  101. cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
  102. } else {
  103. cfg, err = parseServerCommonCfg(CfgFileTypeCmd, nil)
  104. }
  105. if err != nil {
  106. return err
  107. }
  108. err = runServer(cfg)
  109. if err != nil {
  110. fmt.Println(err)
  111. os.Exit(1)
  112. }
  113. return nil
  114. },
  115. }
  116. func Execute() {
  117. if err := rootCmd.Execute(); err != nil {
  118. os.Exit(1)
  119. }
  120. }
  121. func parseServerCommonCfg(fileType int, source []byte) (cfg config.ServerCommonConf, err error) {
  122. if fileType == CfgFileTypeIni {
  123. cfg, err = config.UnmarshalServerConfFromIni(source)
  124. } else if fileType == CfgFileTypeCmd {
  125. cfg, err = parseServerCommonCfgFromCmd()
  126. }
  127. if err != nil {
  128. return
  129. }
  130. cfg.Complete()
  131. err = cfg.Validate()
  132. if err != nil {
  133. err = fmt.Errorf("Parse config error: %v", err)
  134. return
  135. }
  136. return
  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.EnablePrometheus = enablePrometheus
  153. cfg.LogFile = logFile
  154. cfg.LogLevel = logLevel
  155. cfg.LogMaxDays = logMaxDays
  156. cfg.SubDomainHost = subDomainHost
  157. cfg.TLSOnly = tlsOnly
  158. // Only token authentication is supported in cmd mode
  159. cfg.ServerConfig = auth.GetDefaultServerConf()
  160. cfg.Token = token
  161. if len(allowPorts) > 0 {
  162. // e.g. 1000-2000,2001,2002,3000-4000
  163. ports, errRet := util.ParseRangeNumbers(allowPorts)
  164. if errRet != nil {
  165. err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  166. return
  167. }
  168. for _, port := range ports {
  169. cfg.AllowPorts[int(port)] = struct{}{}
  170. }
  171. }
  172. cfg.MaxPortsPerClient = maxPortsPerClient
  173. cfg.DisableLogColor = disableLogColor
  174. return
  175. }
  176. func runServer(cfg config.ServerCommonConf) (err error) {
  177. log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
  178. if cfgFile != "" {
  179. log.Info("frps uses config file: %s", cfgFile)
  180. } else {
  181. log.Info("frps uses command line arguments for config")
  182. }
  183. svr, err := server.NewService(cfg)
  184. if err != nil {
  185. return err
  186. }
  187. log.Info("frps started successfully")
  188. svr.Run()
  189. return
  190. }