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 (
|
2023-06-29 18:04:20 +08:00
|
|
|
"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"
|
2023-09-06 10:18:02 +08:00
|
|
|
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
|
|
|
|
2023-09-15 10:33:32 +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-16 21:03:36 +08:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&strictConfigMode, "strict_config", "", false, "strict config parsing mode, unknown fileds will cause error")
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2023-09-15 10:33:32 +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
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
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)
|
2018-12-11 11:46:12 +08:00
|
|
|
if err != nil {
|
2023-09-13 16:32:39 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2018-12-11 11:46:12 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
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 {
|
2023-09-15 10:33:32 +08:00
|
|
|
serverCfg.Complete()
|
|
|
|
svrCfg = &serverCfg
|
2023-09-06 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
svr, err := server.NewService(cfg)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-23 17:01:31 +08:00
|
|
|
log.Info("frps started successfully")
|
2023-06-29 18:04:20 +08:00
|
|
|
svr.Run(context.Background())
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|