1
0

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