123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package sub
- import (
- "context"
- "fmt"
- "io/fs"
- "os"
- "os/signal"
- "path/filepath"
- "sync"
- "syscall"
- "time"
- "github.com/spf13/cobra"
- "github.com/fatedier/frp/client"
- "github.com/fatedier/frp/pkg/config"
- v1 "github.com/fatedier/frp/pkg/config/v1"
- "github.com/fatedier/frp/pkg/config/v1/validation"
- "github.com/fatedier/frp/pkg/util/log"
- "github.com/fatedier/frp/pkg/util/version"
- )
- var (
- cfgFile string
- cfgDir string
- showVersion bool
- strictConfig bool
- )
- func init() {
- rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
- rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
- rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
- rootCmd.PersistentFlags().BoolVarP(&strictConfig, "strict_config", "", false, "strict config parsing mode")
- }
- var rootCmd = &cobra.Command{
- Use: "frpc",
- Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
- RunE: func(cmd *cobra.Command, args []string) error {
- if showVersion {
- fmt.Println(version.Full())
- return nil
- }
-
-
- if cfgDir != "" {
- _ = runMultipleClients(cfgDir)
- return nil
- }
-
- err := runClient(cfgFile)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- return nil
- },
- }
- func runMultipleClients(cfgDir string) error {
- var wg sync.WaitGroup
- err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
- if err != nil || d.IsDir() {
- return nil
- }
- wg.Add(1)
- time.Sleep(time.Millisecond)
- go func() {
- defer wg.Done()
- err := runClient(path)
- if err != nil {
- fmt.Printf("frpc service error for config file [%s]\n", path)
- }
- }()
- return nil
- })
- wg.Wait()
- return err
- }
- func Execute() {
- if err := rootCmd.Execute(); err != nil {
- os.Exit(1)
- }
- }
- func handleTermSignal(svr *client.Service) {
- ch := make(chan os.Signal, 1)
- signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
- <-ch
- svr.GracefulClose(500 * time.Millisecond)
- }
- func runClient(cfgFilePath string) error {
- cfg, pxyCfgs, visitorCfgs, isLegacyFormat, err := config.LoadClientConfig(cfgFilePath, strictConfig)
- if err != nil {
- return err
- }
- if isLegacyFormat {
- fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " +
- "please use yaml/json/toml format instead!\n")
- }
- warning, err := validation.ValidateAllClientConfig(cfg, pxyCfgs, visitorCfgs)
- if warning != nil {
- fmt.Printf("WARNING: %v\n", warning)
- if strictConfig {
- return fmt.Errorf("warning: %v", warning)
- }
- }
- if err != nil {
- return err
- }
- return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath, strictConfig)
- }
- func startService(
- cfg *v1.ClientCommonConfig,
- pxyCfgs []v1.ProxyConfigurer,
- visitorCfgs []v1.VisitorConfigurer,
- cfgFile string,
- strictConfig bool,
- ) error {
- log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
- if cfgFile != "" {
- log.Info("start frpc service for config file [%s]", cfgFile)
- defer log.Info("frpc service for config file [%s] stopped", cfgFile)
- }
- svr := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile, strictConfig)
- shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic"
-
- if shouldGracefulClose {
- go handleTermSignal(svr)
- }
- return svr.Run(context.Background())
- }
|