root.go 3.5 KB

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