root.go 6.6 KB

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