root.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "os"
  20. "os/signal"
  21. "path/filepath"
  22. "sync"
  23. "syscall"
  24. "time"
  25. "github.com/spf13/cobra"
  26. "github.com/fatedier/frp/client"
  27. "github.com/fatedier/frp/pkg/config"
  28. v1 "github.com/fatedier/frp/pkg/config/v1"
  29. "github.com/fatedier/frp/pkg/config/v1/validation"
  30. "github.com/fatedier/frp/pkg/util/log"
  31. "github.com/fatedier/frp/pkg/util/version"
  32. )
  33. var (
  34. cfgFile string
  35. cfgDir string
  36. showVersion bool
  37. )
  38. func init() {
  39. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
  40. rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
  41. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
  42. }
  43. var rootCmd = &cobra.Command{
  44. Use: "frpc",
  45. Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
  46. RunE: func(cmd *cobra.Command, args []string) error {
  47. if showVersion {
  48. fmt.Println(version.Full())
  49. return nil
  50. }
  51. // If cfgDir is not empty, run multiple frpc service for each config file in cfgDir.
  52. // Note that it's only designed for testing. It's not guaranteed to be stable.
  53. if cfgDir != "" {
  54. _ = runMultipleClients(cfgDir)
  55. return nil
  56. }
  57. // Do not show command usage here.
  58. err := runClient(cfgFile)
  59. if err != nil {
  60. fmt.Println(err)
  61. os.Exit(1)
  62. }
  63. return nil
  64. },
  65. }
  66. func runMultipleClients(cfgDir string) error {
  67. var wg sync.WaitGroup
  68. err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
  69. if err != nil || d.IsDir() {
  70. return nil
  71. }
  72. wg.Add(1)
  73. time.Sleep(time.Millisecond)
  74. go func() {
  75. defer wg.Done()
  76. err := runClient(path)
  77. if err != nil {
  78. fmt.Printf("frpc service error for config file [%s]\n", path)
  79. }
  80. }()
  81. return nil
  82. })
  83. wg.Wait()
  84. return err
  85. }
  86. func Execute() {
  87. if err := rootCmd.Execute(); err != nil {
  88. os.Exit(1)
  89. }
  90. }
  91. func handleTermSignal(svr *client.Service) {
  92. ch := make(chan os.Signal, 1)
  93. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  94. <-ch
  95. svr.GracefulClose(500 * time.Millisecond)
  96. }
  97. func runClient(cfgFilePath string) error {
  98. cfg, pxyCfgs, visitorCfgs, isLegacyFormat, err := config.LoadClientConfig(cfgFilePath)
  99. if err != nil {
  100. return err
  101. }
  102. if isLegacyFormat {
  103. fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " +
  104. "please use yaml/json/toml format instead!\n")
  105. }
  106. warning, err := validation.ValidateAllClientConfig(cfg, pxyCfgs, visitorCfgs)
  107. if warning != nil {
  108. fmt.Printf("WARNING: %v\n", warning)
  109. }
  110. if err != nil {
  111. return err
  112. }
  113. return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
  114. }
  115. func startService(
  116. cfg *v1.ClientCommonConfig,
  117. pxyCfgs []v1.ProxyConfigurer,
  118. visitorCfgs []v1.VisitorConfigurer,
  119. cfgFile string,
  120. ) error {
  121. log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
  122. if cfgFile != "" {
  123. log.Info("start frpc service for config file [%s]", cfgFile)
  124. defer log.Info("frpc service for config file [%s] stopped", cfgFile)
  125. }
  126. svr, err := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
  127. if err != nil {
  128. return err
  129. }
  130. shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic"
  131. // Capture the exit signal if we use kcp or quic.
  132. if shouldGracefulClose {
  133. go handleTermSignal(svr)
  134. }
  135. _ = svr.Run(context.Background())
  136. return nil
  137. }