| // |
| 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 |
| 15 | package sub |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "io/fs" |
| 21 | "os" |
| 22 | "os/signal" |
| 23 | "path/filepath" |
| 24 | "strings" |
| 25 | "sync" |
| 26 | "syscall" |
| 27 | "time" |
| 28 |
| 29 | "github.com/spf13/cobra" |
| 30 |
| 31 | "github.com/fatedier/frp/client" |
| 32 | "github.com/fatedier/frp/pkg/config" |
| 33 | "github.com/fatedier/frp/pkg/config/source" |
| 34 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 35 | "github.com/fatedier/frp/pkg/config/v1/validation" |
| 36 | "github.com/fatedier/frp/pkg/policy/featuregate" |
| 37 | "github.com/fatedier/frp/pkg/policy/security" |
| 38 | "github.com/fatedier/frp/pkg/util/log" |
| 39 | "github.com/fatedier/frp/pkg/util/version" |
| 40 | ) |
| 41 |
| 42 | var ( |
| 43 | cfgFile string |
| 44 | cfgDir string |
| 45 | showVersion bool |
| 46 | strictConfigMode bool |
| 47 | allowUnsafe []string |
| 48 | ) |
| 49 |
| 50 | func init() { |
| 51 | rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc") |
| 52 | rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory") |
| 53 | rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc") |
| 54 | rootCmd.PersistentFlags().BoolVarP(&strictConfigMode, "strict_config", "", true, "strict config parsing mode, unknown fields will cause an errors") |
| 55 |
| 56 | rootCmd.PersistentFlags().StringSliceVarP(&allowUnsafe, "allow-unsafe", "", []string{}, |
| 57 | fmt.Sprintf("allowed unsafe features, one or more of: %s", strings.Join(security.ClientUnsafeFeatures, ", "))) |
| 58 | } |
| 59 |
| 60 | var rootCmd = &cobra.Command{ |
| 61 | Use: "frpc", |
| 62 | Short: "frpc is the client of frp (https://github.com/fatedier/frp)", |
| 63 | RunE: func(cmd *cobra.Command, args []string) error { |
| 64 | if showVersion { |
| 65 | fmt.Println(version.Full()) |
| 66 | return nil |
| 67 | } |
| 68 |
| 69 | unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe) |
| 70 |
| 71 | // If cfgDir is not empty, run multiple frpc service for each config file in cfgDir. |
| 72 | // Note that it's only designed for testing. It's not guaranteed to be stable. |
| 73 | if cfgDir != "" { |
| 74 | _ = runMultipleClients(cfgDir, unsafeFeatures) |
| 75 | return nil |
| 76 | } |
| 77 |
| 78 | // Do not show command usage here. |
| 79 | err := runClient(cfgFile, unsafeFeatures) |
| 80 | if err != nil { |
| 81 | fmt.Println(err) |
| 82 | os.Exit(1) |
| 83 | } |
| 84 | return nil |
| 85 | }, |
| 86 | } |
| 87 |
| 88 | func runMultipleClients(cfgDir string, unsafeFeatures *security.UnsafeFeatures) error { |
| 89 | var wg sync.WaitGroup |
| 90 | err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error { |
| 91 | if err != nil || d.IsDir() { |
| 92 | return nil |
| 93 | } |
| 94 | wg.Add(1) |
| 95 | time.Sleep(time.Millisecond) |
| 96 | go func() { |
| 97 | defer wg.Done() |
| 98 | err := runClient(path, unsafeFeatures) |
| 99 | if err != nil { |
| 100 | fmt.Printf("frpc service error for config file [%s]\n", path) |
| 101 | } |
| 102 | }() |
| 103 | return nil |
| 104 | }) |
| 105 | wg.Wait() |
| 106 | return err |
| 107 | } |
| 108 |
| 109 | func Execute() { |
| 110 | rootCmd.SetGlobalNormalizationFunc(config.WordSepNormalizeFunc) |
| 111 | if err := rootCmd.Execute(); err != nil { |
| 112 | os.Exit(1) |
| 113 | } |
| 114 | } |
| 115 |
| 116 | func handleTermSignal(svr *client.Service) { |
| 117 | ch := make(chan os.Signal, 1) |
| 118 | signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) |
| 119 | <-ch |
| 120 | svr.GracefulClose(500 * time.Millisecond) |
| 121 | } |
| 122 |
| 123 | func runClient(cfgFilePath string, unsafeFeatures *security.UnsafeFeatures) error { |
| 124 | // Load configuration |
| 125 | result, err := config.LoadClientConfigResult(cfgFilePath, strictConfigMode) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | if result.IsLegacyFormat { |
| 130 | fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " + |
| 131 | "please use yaml/json/toml format instead!\n") |
| 132 | } |
| 133 |
| 134 | if len(result.Common.FeatureGates) > 0 { |
| 135 | if err := featuregate.SetFromMap(result.Common.FeatureGates); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | } |
| 139 |
| 140 | return runClientWithAggregator(result, unsafeFeatures, cfgFilePath) |
| 141 | } |
| 142 |
| 143 | // runClientWithAggregator runs the client using the internal source aggregator. |
| 144 | func runClientWithAggregator(result *config.ClientConfigLoadResult, unsafeFeatures *security.UnsafeFeatures, cfgFilePath string) error { |
| 145 | configSource := source.NewConfigSource() |
| 146 | if err := configSource.ReplaceAll(result.Proxies, result.Visitors); err != nil { |
| 147 | return fmt.Errorf("failed to set config source: %w", err) |
| 148 | } |
| 149 |
| 150 | var storeSource *source.StoreSource |
| 151 |
| 152 | if result.Common.Store.IsEnabled() { |
| 153 | storePath := result.Common.Store.Path |
| 154 | if storePath != "" && cfgFilePath != "" && !filepath.IsAbs(storePath) { |
| 155 | storePath = filepath.Join(filepath.Dir(cfgFilePath), storePath) |
| 156 | } |
| 157 |
| 158 | s, err := source.NewStoreSource(source.StoreSourceConfig{ |
| 159 | Path: storePath, |
| 160 | }) |
| 161 | if err != nil { |
| 162 | return fmt.Errorf("failed to create store source: %w", err) |
| 163 | } |
| 164 | storeSource = s |
| 165 | } |
| 166 |
| 167 | aggregator := source.NewAggregator(configSource) |
| 168 | if storeSource != nil { |
| 169 | aggregator.SetStoreSource(storeSource) |
| 170 | } |
| 171 |
| 172 | proxyCfgs, visitorCfgs, err := aggregator.Load() |
| 173 | if err != nil { |
| 174 | return fmt.Errorf("failed to load config from sources: %w", err) |
| 175 | } |
| 176 |
| 177 | proxyCfgs, visitorCfgs = config.FilterClientConfigurers(result.Common, proxyCfgs, visitorCfgs) |
| 178 | proxyCfgs = config.CompleteProxyConfigurers(proxyCfgs) |
| 179 | visitorCfgs = config.CompleteVisitorConfigurers(visitorCfgs) |
| 180 |
| 181 | warning, err := validation.ValidateAllClientConfig(result.Common, proxyCfgs, visitorCfgs, unsafeFeatures) |
| 182 | if warning != nil { |
| 183 | fmt.Printf("WARNING: %v\n", warning) |
| 184 | } |
| 185 | if err != nil { |
| 186 | return err |
| 187 | } |
| 188 |
| 189 | return startServiceWithAggregator(result.Common, aggregator, unsafeFeatures, cfgFilePath) |
| 190 | } |
| 191 |
| 192 | func startServiceWithAggregator( |
| 193 | cfg *v1.ClientCommonConfig, |
| 194 | aggregator *source.Aggregator, |
| 195 | unsafeFeatures *security.UnsafeFeatures, |
| 196 | cfgFile string, |
| 197 | ) error { |
| 198 | log.InitLogger(cfg.Log.To, cfg.Log.Level, int(cfg.Log.MaxDays), cfg.Log.DisablePrintColor) |
| 199 |
| 200 | if cfgFile != "" { |
| 201 | log.Infof("start frpc service for config file [%s] with aggregated configuration", cfgFile) |
| 202 | defer log.Infof("frpc service for config file [%s] stopped", cfgFile) |
| 203 | } |
| 204 | svr, err := client.NewService(client.ServiceOptions{ |
| 205 | Common: cfg, |
| 206 | ConfigSourceAggregator: aggregator, |
| 207 | UnsafeFeatures: unsafeFeatures, |
| 208 | ConfigFilePath: cfgFile, |
| 209 | }) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 |
| 214 | shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic" |
| 215 | if shouldGracefulClose { |
| 216 | go handleTermSignal(svr) |
| 217 | } |
| 218 | return svr.Run(context.Background()) |
| 219 | } |