root.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "context"
  17. "fmt"
  18. "io/fs"
  19. "net"
  20. "os"
  21. "os/signal"
  22. "path/filepath"
  23. "strconv"
  24. "sync"
  25. "syscall"
  26. "time"
  27. "github.com/samber/lo"
  28. "github.com/spf13/cobra"
  29. "github.com/fatedier/frp/client"
  30. "github.com/fatedier/frp/pkg/config"
  31. v1 "github.com/fatedier/frp/pkg/config/v1"
  32. "github.com/fatedier/frp/pkg/config/v1/validation"
  33. "github.com/fatedier/frp/pkg/util/log"
  34. "github.com/fatedier/frp/pkg/util/version"
  35. )
  36. var (
  37. cfgFile string
  38. cfgDir string
  39. showVersion bool
  40. serverAddr string
  41. user string
  42. protocol string
  43. token string
  44. logLevel string
  45. logFile string
  46. logMaxDays int
  47. disableLogColor bool
  48. dnsServer string
  49. proxyName string
  50. localIP string
  51. localPort int
  52. remotePort int
  53. useEncryption bool
  54. useCompression bool
  55. bandwidthLimit string
  56. bandwidthLimitMode string
  57. customDomains string
  58. subDomain string
  59. httpUser string
  60. httpPwd string
  61. locations string
  62. hostHeaderRewrite string
  63. role string
  64. sk string
  65. multiplexer string
  66. serverName string
  67. bindAddr string
  68. bindPort int
  69. tlsEnable bool
  70. tlsServerName string
  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, kcp, quic, websocket, wss")
  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(&tlsServerName, "tls_server_name", "", "", "specify the custom server name of tls certificate")
  88. cmd.PersistentFlags().StringVarP(&dnsServer, "dns_server", "", "", "specify dns server instead of using system default one")
  89. }
  90. var rootCmd = &cobra.Command{
  91. Use: "frpc",
  92. Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
  93. RunE: func(cmd *cobra.Command, args []string) error {
  94. if showVersion {
  95. fmt.Println(version.Full())
  96. return nil
  97. }
  98. // If cfgDir is not empty, run multiple frpc service for each config file in cfgDir.
  99. // Note that it's only designed for testing. It's not guaranteed to be stable.
  100. if cfgDir != "" {
  101. _ = runMultipleClients(cfgDir)
  102. return nil
  103. }
  104. // Do not show command usage here.
  105. err := runClient(cfgFile)
  106. if err != nil {
  107. fmt.Println(err)
  108. os.Exit(1)
  109. }
  110. return nil
  111. },
  112. }
  113. func runMultipleClients(cfgDir string) error {
  114. var wg sync.WaitGroup
  115. err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
  116. if err != nil || d.IsDir() {
  117. return nil
  118. }
  119. wg.Add(1)
  120. time.Sleep(time.Millisecond)
  121. go func() {
  122. defer wg.Done()
  123. err := runClient(path)
  124. if err != nil {
  125. fmt.Printf("frpc service error for config file [%s]\n", path)
  126. }
  127. }()
  128. return nil
  129. })
  130. wg.Wait()
  131. return err
  132. }
  133. func Execute() {
  134. if err := rootCmd.Execute(); err != nil {
  135. os.Exit(1)
  136. }
  137. }
  138. func handleTermSignal(svr *client.Service) {
  139. ch := make(chan os.Signal, 1)
  140. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  141. <-ch
  142. svr.GracefulClose(500 * time.Millisecond)
  143. }
  144. func parseClientCommonCfgFromCmd() (*v1.ClientCommonConfig, error) {
  145. cfg := &v1.ClientCommonConfig{}
  146. ipStr, portStr, err := net.SplitHostPort(serverAddr)
  147. if err != nil {
  148. return nil, fmt.Errorf("invalid server_addr: %v", err)
  149. }
  150. cfg.ServerAddr = ipStr
  151. cfg.ServerPort, err = strconv.Atoi(portStr)
  152. if err != nil {
  153. return nil, fmt.Errorf("invalid server_addr: %v", err)
  154. }
  155. cfg.User = user
  156. cfg.Transport.Protocol = protocol
  157. cfg.Log.Level = logLevel
  158. cfg.Log.To = logFile
  159. cfg.Log.MaxDays = int64(logMaxDays)
  160. cfg.Log.DisablePrintColor = disableLogColor
  161. cfg.DNSServer = dnsServer
  162. // Only token authentication is supported in cmd mode
  163. cfg.Auth.Token = token
  164. cfg.Transport.TLS.Enable = lo.ToPtr(tlsEnable)
  165. cfg.Transport.TLS.ServerName = tlsServerName
  166. cfg.Complete()
  167. warning, err := validation.ValidateClientCommonConfig(cfg)
  168. if warning != nil {
  169. fmt.Printf("WARNING: %v\n", warning)
  170. }
  171. if err != nil {
  172. return nil, fmt.Errorf("parse config error: %v", err)
  173. }
  174. return cfg, nil
  175. }
  176. func runClient(cfgFilePath string) error {
  177. cfg, pxyCfgs, visitorCfgs, isLegacyFormat, err := config.LoadClientConfig(cfgFilePath)
  178. if err != nil {
  179. return err
  180. }
  181. if isLegacyFormat {
  182. fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " +
  183. "please use yaml/json/toml format instead!\n")
  184. }
  185. warning, err := validation.ValidateAllClientConfig(cfg, pxyCfgs, visitorCfgs)
  186. if warning != nil {
  187. fmt.Printf("WARNING: %v\n", warning)
  188. }
  189. if err != nil {
  190. return err
  191. }
  192. return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
  193. }
  194. func startService(
  195. cfg *v1.ClientCommonConfig,
  196. pxyCfgs []v1.ProxyConfigurer,
  197. visitorCfgs []v1.VisitorConfigurer,
  198. cfgFile string,
  199. ) error {
  200. log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
  201. if cfgFile != "" {
  202. log.Info("start frpc service for config file [%s]", cfgFile)
  203. defer log.Info("frpc service for config file [%s] stopped", cfgFile)
  204. }
  205. svr, err := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
  206. if err != nil {
  207. return err
  208. }
  209. shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic"
  210. // Capture the exit signal if we use kcp or quic.
  211. if shouldGracefulClose {
  212. go handleTermSignal(svr)
  213. }
  214. _ = svr.Run(context.Background())
  215. return nil
  216. }