root.go 3.0 KB

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