frp/cmd/frps/root.go

117 lines
2.9 KiB
Go
Raw Normal View History

2018-05-02 22:09:15 +08:00
// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-04-10 17:46:49 +08:00
package main
import (
"context"
2018-04-10 17:46:49 +08:00
"fmt"
"os"
2022-08-29 01:02:53 +08:00
"github.com/spf13/cobra"
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/config"
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/fatedier/frp/pkg/config/v1/validation"
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/util/log"
"github.com/fatedier/frp/pkg/util/version"
2018-04-10 17:46:49 +08:00
"github.com/fatedier/frp/server"
)
var (
2023-11-16 21:03:36 +08:00
cfgFile string
showVersion bool
strictConfigMode bool
2018-04-10 17:46:49 +08:00
serverCfg v1.ServerConfig
2018-04-10 17:46:49 +08:00
)
func init() {
2019-04-08 15:39:14 +08:00
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file of frps")
2020-05-10 17:58:35 +08:00
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frps")
2023-11-22 14:30:22 +08:00
rootCmd.PersistentFlags().BoolVarP(&strictConfigMode, "strict_config", "", false, "strict config parsing mode, unknown fields will cause error")
2018-04-10 17:46:49 +08:00
RegisterServerConfigFlags(rootCmd, &serverCfg)
2018-04-10 17:46:49 +08:00
}
var rootCmd = &cobra.Command{
Use: "frps",
Short: "frps is the server of frp (https://github.com/fatedier/frp)",
RunE: func(cmd *cobra.Command, args []string) error {
if showVersion {
fmt.Println(version.Full())
return nil
}
var (
svrCfg *v1.ServerConfig
isLegacyFormat bool
err error
)
2018-04-10 17:46:49 +08:00
if cfgFile != "" {
2023-11-16 21:03:36 +08:00
svrCfg, isLegacyFormat, err = config.LoadServerConfig(cfgFile, strictConfigMode)
if err != nil {
2023-09-13 16:32:39 +08:00
fmt.Println(err)
os.Exit(1)
}
if isLegacyFormat {
fmt.Printf("WARNING: ini format is deprecated and the support will be removed in the future, " +
"please use yaml/json/toml format instead!\n")
}
2018-04-10 17:46:49 +08:00
} else {
serverCfg.Complete()
svrCfg = &serverCfg
}
warning, err := validation.ValidateServerConfig(svrCfg)
if warning != nil {
fmt.Printf("WARNING: %v\n", warning)
2018-04-28 00:30:13 +08:00
}
if err != nil {
2023-09-13 16:32:39 +08:00
fmt.Println(err)
os.Exit(1)
2018-04-10 17:46:49 +08:00
}
if err := runServer(svrCfg); err != nil {
2018-04-10 17:46:49 +08:00
fmt.Println(err)
os.Exit(1)
}
return nil
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func runServer(cfg *v1.ServerConfig) (err error) {
log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
2021-03-22 14:53:30 +08:00
if cfgFile != "" {
log.Info("frps uses config file: %s", cfgFile)
} else {
log.Info("frps uses command line arguments for config")
}
svr, err := server.NewService(cfg)
2018-04-10 17:46:49 +08:00
if err != nil {
return err
}
log.Info("frps started successfully")
svr.Run(context.Background())
2018-04-10 17:46:49 +08:00
return
}