1
0

root.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 sub
  15. import (
  16. "fmt"
  17. "io/fs"
  18. "net"
  19. "os"
  20. "os/signal"
  21. "path/filepath"
  22. "strconv"
  23. "sync"
  24. "syscall"
  25. "time"
  26. "github.com/spf13/cobra"
  27. "github.com/fatedier/frp/client"
  28. "github.com/fatedier/frp/pkg/auth"
  29. "github.com/fatedier/frp/pkg/config"
  30. "github.com/fatedier/frp/pkg/util/log"
  31. "github.com/fatedier/frp/pkg/util/version"
  32. )
  33. const (
  34. CfgFileTypeIni = iota
  35. CfgFileTypeCmd
  36. )
  37. var (
  38. cfgFile string
  39. cfgDir string
  40. showVersion bool
  41. serverAddr string
  42. user string
  43. protocol string
  44. token string
  45. logLevel string
  46. logFile string
  47. logMaxDays int
  48. disableLogColor bool
  49. dnsServer string
  50. proxyName string
  51. localIP string
  52. localPort int
  53. remotePort int
  54. useEncryption bool
  55. useCompression bool
  56. bandwidthLimit string
  57. bandwidthLimitMode string
  58. customDomains string
  59. subDomain string
  60. httpUser string
  61. httpPwd string
  62. locations string
  63. hostHeaderRewrite string
  64. role string
  65. sk string
  66. multiplexer string
  67. serverName string
  68. bindAddr string
  69. bindPort int
  70. tlsEnable bool
  71. )
  72. func init() {
  73. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
  74. rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
  75. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
  76. }
  77. func RegisterCommonFlags(cmd *cobra.Command) {
  78. cmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
  79. cmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
  80. cmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket")
  81. cmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  82. cmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  83. cmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
  84. cmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
  85. cmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
  86. cmd.PersistentFlags().BoolVarP(&tlsEnable, "tls_enable", "", true, "enable frpc tls")
  87. cmd.PersistentFlags().StringVarP(&dnsServer, "dns_server", "", "", "specify dns server instead of using system default one")
  88. }
  89. var rootCmd = &cobra.Command{
  90. Use: "frpc",
  91. Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
  92. RunE: func(cmd *cobra.Command, args []string) error {
  93. if showVersion {
  94. fmt.Println(version.Full())
  95. return nil
  96. }
  97. // If cfgDir is not empty, run multiple frpc service for each config file in cfgDir.
  98. // Note that it's only designed for testing. It's not guaranteed to be stable.
  99. if cfgDir != "" {
  100. _ = runMultipleClients(cfgDir)
  101. return nil
  102. }
  103. // Do not show command usage here.
  104. err := runClient(cfgFile)
  105. if err != nil {
  106. os.Exit(1)
  107. }
  108. return nil
  109. },
  110. }
  111. func runMultipleClients(cfgDir string) error {
  112. var wg sync.WaitGroup
  113. err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
  114. if err != nil || d.IsDir() {
  115. return nil
  116. }
  117. wg.Add(1)
  118. time.Sleep(time.Millisecond)
  119. go func() {
  120. defer wg.Done()
  121. err := runClient(path)
  122. if err != nil {
  123. fmt.Printf("frpc service error for config file [%s]\n", path)
  124. }
  125. }()
  126. return nil
  127. })
  128. wg.Wait()
  129. return err
  130. }
  131. func Execute() {
  132. if err := rootCmd.Execute(); err != nil {
  133. os.Exit(1)
  134. }
  135. }
  136. func handleSignal(svr *client.Service, doneCh chan struct{}) {
  137. ch := make(chan os.Signal, 1)
  138. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  139. <-ch
  140. svr.GracefulClose(500 * time.Millisecond)
  141. close(doneCh)
  142. }
  143. func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
  144. cfg = config.GetDefaultClientConf()
  145. ipStr, portStr, err := net.SplitHostPort(serverAddr)
  146. if err != nil {
  147. err = fmt.Errorf("invalid server_addr: %v", err)
  148. return
  149. }
  150. cfg.ServerAddr = ipStr
  151. cfg.ServerPort, err = strconv.Atoi(portStr)
  152. if err != nil {
  153. err = fmt.Errorf("invalid server_addr: %v", err)
  154. return
  155. }
  156. cfg.User = user
  157. cfg.Protocol = protocol
  158. cfg.LogLevel = logLevel
  159. cfg.LogFile = logFile
  160. cfg.LogMaxDays = int64(logMaxDays)
  161. cfg.DisableLogColor = disableLogColor
  162. cfg.DNSServer = dnsServer
  163. // Only token authentication is supported in cmd mode
  164. cfg.ClientConfig = auth.GetDefaultClientConf()
  165. cfg.Token = token
  166. cfg.TLSEnable = tlsEnable
  167. cfg.Complete()
  168. if err = cfg.Validate(); err != nil {
  169. err = fmt.Errorf("parse config error: %v", err)
  170. return
  171. }
  172. return
  173. }
  174. func runClient(cfgFilePath string) error {
  175. cfg, pxyCfgs, visitorCfgs, err := config.ParseClientConfig(cfgFilePath)
  176. if err != nil {
  177. fmt.Println(err)
  178. return err
  179. }
  180. return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
  181. }
  182. func startService(
  183. cfg config.ClientCommonConf,
  184. pxyCfgs map[string]config.ProxyConf,
  185. visitorCfgs map[string]config.VisitorConf,
  186. cfgFile string,
  187. ) (err error) {
  188. log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel,
  189. cfg.LogMaxDays, cfg.DisableLogColor)
  190. if cfgFile != "" {
  191. log.Info("start frpc service for config file [%s]", cfgFile)
  192. defer log.Info("frpc service for config file [%s] stopped", cfgFile)
  193. }
  194. svr, errRet := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
  195. if errRet != nil {
  196. err = errRet
  197. return
  198. }
  199. closedDoneCh := make(chan struct{})
  200. shouldGracefulClose := cfg.Protocol == "kcp" || cfg.Protocol == "quic"
  201. // Capture the exit signal if we use kcp or quic.
  202. if shouldGracefulClose {
  203. go handleSignal(svr, closedDoneCh)
  204. }
  205. err = svr.Run()
  206. if err == nil && shouldGracefulClose {
  207. <-closedDoneCh
  208. }
  209. return
  210. }