| // |
| 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 main |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "strings" |
| 22 |
| 23 | "github.com/spf13/cobra" |
| 24 |
| 25 | "github.com/fatedier/frp/pkg/config" |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | "github.com/fatedier/frp/pkg/config/v1/validation" |
| 28 | "github.com/fatedier/frp/pkg/policy/security" |
| 29 | "github.com/fatedier/frp/pkg/util/log" |
| 30 | "github.com/fatedier/frp/pkg/util/version" |
| 31 | "github.com/fatedier/frp/server" |
| 32 | ) |
| 33 |
| 34 | var ( |
| 35 | cfgFile string |
| 36 | showVersion bool |
| 37 | strictConfigMode bool |
| 38 | allowUnsafe []string |
| 39 |
| 40 | serverCfg v1.ServerConfig |
| 41 | ) |
| 42 |
| 43 | func init() { |
| 44 | rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file of frps") |
| 45 | rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frps") |
| 46 | rootCmd.PersistentFlags().BoolVarP(&strictConfigMode, "strict_config", "", true, "strict config parsing mode, unknown fields will cause errors") |
| 47 | rootCmd.PersistentFlags().StringSliceVarP(&allowUnsafe, "allow-unsafe", "", []string{}, |
| 48 | fmt.Sprintf("allowed unsafe features, one or more of: %s", strings.Join(security.ServerUnsafeFeatures, ", "))) |
| 49 |
| 50 | config.RegisterServerConfigFlags(rootCmd, &serverCfg) |
| 51 | } |
| 52 |
| 53 | var rootCmd = &cobra.Command{ |
| 54 | Use: "frps", |
| 55 | Short: "frps is the server of frp (https://github.com/fatedier/frp)", |
| 56 | RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | if showVersion { |
| 58 | fmt.Println(version.Full()) |
| 59 | return nil |
| 60 | } |
| 61 |
| 62 | var ( |
| 63 | svrCfg *v1.ServerConfig |
| 64 | isLegacyFormat bool |
| 65 | err error |
| 66 | ) |
| 67 | if cfgFile != "" { |
| 68 | svrCfg, isLegacyFormat, err = config.LoadServerConfig(cfgFile, strictConfigMode) |
| 69 | if err != nil { |
| 70 | fmt.Println(err) |
| 71 | os.Exit(1) |
| 72 | } |
| 73 | if isLegacyFormat { |
| 74 | fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " + |
| 75 | "please use yaml/json/toml format instead!\n") |
| 76 | } |
| 77 | } else { |
| 78 | if err := serverCfg.Complete(); err != nil { |
| 79 | fmt.Printf("failed to complete server config: %v\n", err) |
| 80 | os.Exit(1) |
| 81 | } |
| 82 | svrCfg = &serverCfg |
| 83 | } |
| 84 |
| 85 | unsafeFeatures := security.NewUnsafeFeatures(allowUnsafe) |
| 86 | validator := validation.NewConfigValidator(unsafeFeatures) |
| 87 | warning, err := validator.ValidateServerConfig(svrCfg) |
| 88 | if warning != nil { |
| 89 | fmt.Printf("WARNING: %v\n", warning) |
| 90 | } |
| 91 | if err != nil { |
| 92 | fmt.Println(err) |
| 93 | os.Exit(1) |
| 94 | } |
| 95 |
| 96 | if err := runServer(svrCfg); err != nil { |
| 97 | fmt.Println(err) |
| 98 | os.Exit(1) |
| 99 | } |
| 100 | return nil |
| 101 | }, |
| 102 | } |
| 103 |
| 104 | func Execute() { |
| 105 | rootCmd.SetGlobalNormalizationFunc(config.WordSepNormalizeFunc) |
| 106 | if err := rootCmd.Execute(); err != nil { |
| 107 | os.Exit(1) |
| 108 | } |
| 109 | } |
| 110 |
| 111 | func runServer(cfg *v1.ServerConfig) (err error) { |
| 112 | log.InitLogger(cfg.Log.To, cfg.Log.Level, int(cfg.Log.MaxDays), cfg.Log.DisablePrintColor) |
| 113 |
| 114 | if cfgFile != "" { |
| 115 | log.Infof("frps uses config file: %s", cfgFile) |
| 116 | } else { |
| 117 | log.Infof("frps uses command line arguments for config") |
| 118 | } |
| 119 |
| 120 | svr, err := server.NewService(cfg) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | log.Infof("frps started successfully") |
| 125 | svr.Run(context.Background()) |
| 126 | return |
| 127 | } |