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", "", false, "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. fmt.Println(err)
  107. os.Exit(1)
  108. }
  109. return nil
  110. },
  111. }
  112. func runMultipleClients(cfgDir string) error {
  113. var wg sync.WaitGroup
  114. err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
  115. if err != nil || d.IsDir() {
  116. return nil
  117. }
  118. wg.Add(1)
  119. time.Sleep(time.Millisecond)
  120. go func() {
  121. defer wg.Done()
  122. err := runClient(path)
  123. if err != nil {
  124. fmt.Printf("frpc service error for config file [%s]\n", path)
  125. }
  126. }()
  127. return nil
  128. })
  129. wg.Wait()
  130. return err
  131. }
  132. func Execute() {
  133. if err := rootCmd.Execute(); err != nil {
  134. os.Exit(1)
  135. }
  136. }
  137. func handleSignal(svr *client.Service, doneCh chan struct{}) {
  138. ch := make(chan os.Signal, 1)
  139. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  140. <-ch
  141. svr.GracefulClose(500 * time.Millisecond)
  142. close(doneCh)
  143. }
  144. func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
  145. cfg = config.GetDefaultClientConf()
  146. ipStr, portStr, err := net.SplitHostPort(serverAddr)
  147. if err != nil {
  148. err = fmt.Errorf("invalid server_addr: %v", err)
  149. return
  150. }
  151. cfg.ServerAddr = ipStr
  152. cfg.ServerPort, err = strconv.Atoi(portStr)
  153. if err != nil {
  154. err = fmt.Errorf("invalid server_addr: %v", err)
  155. return
  156. }
  157. cfg.User = user
  158. cfg.Protocol = protocol
  159. cfg.LogLevel = logLevel
  160. cfg.LogFile = logFile
  161. cfg.LogMaxDays = int64(logMaxDays)
  162. cfg.DisableLogColor = disableLogColor
  163. cfg.DNSServer = dnsServer
  164. // Only token authentication is supported in cmd mode
  165. cfg.ClientConfig = auth.GetDefaultClientConf()
  166. cfg.Token = token
  167. cfg.TLSEnable = tlsEnable
  168. cfg.Complete()
  169. if err = cfg.Validate(); err != nil {
  170. err = fmt.Errorf("parse config error: %v", err)
  171. return
  172. }
  173. return
  174. }
  175. func runClient(cfgFilePath string) error {
  176. cfg, pxyCfgs, visitorCfgs, err := config.ParseClientConfig(cfgFilePath)
  177. if err != nil {
  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.Trace("start frpc service for config file [%s]", cfgFile)
  192. defer log.Trace("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. }