root.go 2.7 KB

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