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 (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/auth"
|
|
|
|
"github.com/fatedier/frp/pkg/config"
|
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/version"
|
2018-04-10 17:46:49 +08:00
|
|
|
"github.com/fatedier/frp/server"
|
2020-09-23 13:49:14 +08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-04-10 17:46:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CfgFileTypeIni = iota
|
|
|
|
CfgFileTypeCmd
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgFile string
|
|
|
|
showVersion bool
|
|
|
|
|
|
|
|
bindAddr string
|
|
|
|
bindPort int
|
2020-05-24 17:48:37 +08:00
|
|
|
bindUDPPort int
|
2018-04-10 17:46:49 +08:00
|
|
|
kcpBindPort int
|
|
|
|
proxyBindAddr string
|
2020-05-24 17:48:37 +08:00
|
|
|
vhostHTTPPort int
|
|
|
|
vhostHTTPSPort int
|
|
|
|
vhostHTTPTimeout int64
|
2018-04-10 17:46:49 +08:00
|
|
|
dashboardAddr string
|
|
|
|
dashboardPort int
|
|
|
|
dashboardUser string
|
|
|
|
dashboardPwd string
|
2020-11-06 15:33:59 +08:00
|
|
|
enablePrometheus bool
|
2018-04-10 17:46:49 +08:00
|
|
|
assetsDir string
|
|
|
|
logFile string
|
|
|
|
logLevel string
|
|
|
|
logMaxDays int64
|
2019-08-12 00:47:35 +08:00
|
|
|
disableLogColor bool
|
2018-04-10 17:46:49 +08:00
|
|
|
token string
|
|
|
|
subDomainHost string
|
|
|
|
tcpMux bool
|
|
|
|
allowPorts string
|
|
|
|
maxPoolCount int64
|
|
|
|
maxPortsPerClient int64
|
2020-05-12 14:33:34 +08:00
|
|
|
tlsOnly bool
|
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")
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2018-04-23 02:40:25 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "0.0.0.0", "bind address")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "p", 7000, "bind port")
|
2020-05-24 17:48:37 +08:00
|
|
|
rootCmd.PersistentFlags().IntVarP(&bindUDPPort, "bind_udp_port", "", 0, "bind udp port")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().IntVarP(&kcpBindPort, "kcp_bind_port", "", 0, "kcp bind udp port")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "proxy_bind_addr", "", "0.0.0.0", "proxy bind address")
|
2020-05-24 17:48:37 +08:00
|
|
|
rootCmd.PersistentFlags().IntVarP(&vhostHTTPPort, "vhost_http_port", "", 0, "vhost http port")
|
|
|
|
rootCmd.PersistentFlags().IntVarP(&vhostHTTPSPort, "vhost_https_port", "", 0, "vhost https port")
|
|
|
|
rootCmd.PersistentFlags().Int64VarP(&vhostHTTPTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&dashboardAddr, "dashboard_addr", "", "0.0.0.0", "dasboard address")
|
|
|
|
rootCmd.PersistentFlags().IntVarP(&dashboardPort, "dashboard_port", "", 0, "dashboard port")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&dashboardPwd, "dashboard_pwd", "", "admin", "dashboard password")
|
2020-11-06 15:33:59 +08:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&enablePrometheus, "enable_prometheus", "", false, "enable prometheus dashboard")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "log file")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
|
2019-06-01 10:09:13 +08:00
|
|
|
rootCmd.PersistentFlags().Int64VarP(&logMaxDays, "log_max_days", "", 3, "log max days")
|
2019-08-12 00:47:35 +08:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
|
|
|
|
|
2018-05-18 10:58:08 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&subDomainHost, "subdomain_host", "", "", "subdomain host")
|
2018-06-07 00:25:55 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&allowPorts, "allow_ports", "", "", "allow ports")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().Int64VarP(&maxPortsPerClient, "max_ports_per_client", "", 0, "max ports per client")
|
2020-05-12 14:33:34 +08:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&tlsOnly, "tls_only", "", false, "frps tls only")
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
var cfg config.ServerCommonConf
|
2018-04-28 00:30:13 +08:00
|
|
|
var err error
|
2018-04-10 17:46:49 +08:00
|
|
|
if cfgFile != "" {
|
2018-12-11 11:46:12 +08:00
|
|
|
var content string
|
|
|
|
content, err = config.GetRenderedConfFromFile(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
|
2018-04-10 17:46:49 +08:00
|
|
|
} else {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "")
|
2018-04-28 00:30:13 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
err = runServer(cfg)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
func parseServerCommonCfg(fileType int, content string) (cfg config.ServerCommonConf, err error) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if fileType == CfgFileTypeIni {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg, err = parseServerCommonCfgFromIni(content)
|
2018-04-10 17:46:49 +08:00
|
|
|
} else if fileType == CfgFileTypeCmd {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg, err = parseServerCommonCfgFromCmd()
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
err = cfg.Check()
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
func parseServerCommonCfgFromIni(content string) (config.ServerCommonConf, error) {
|
|
|
|
cfg, err := config.UnmarshalServerConfFromIni(content)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
2019-08-20 02:07:37 +08:00
|
|
|
return config.ServerCommonConf{}, err
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-08-20 02:07:37 +08:00
|
|
|
return cfg, nil
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
|
|
|
|
cfg = config.GetDefaultServerConf()
|
|
|
|
|
|
|
|
cfg.BindAddr = bindAddr
|
|
|
|
cfg.BindPort = bindPort
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.BindUDPPort = bindUDPPort
|
|
|
|
cfg.KCPBindPort = kcpBindPort
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.ProxyBindAddr = proxyBindAddr
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.VhostHTTPPort = vhostHTTPPort
|
|
|
|
cfg.VhostHTTPSPort = vhostHTTPSPort
|
|
|
|
cfg.VhostHTTPTimeout = vhostHTTPTimeout
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.DashboardAddr = dashboardAddr
|
|
|
|
cfg.DashboardPort = dashboardPort
|
|
|
|
cfg.DashboardUser = dashboardUser
|
|
|
|
cfg.DashboardPwd = dashboardPwd
|
2020-11-06 15:33:59 +08:00
|
|
|
cfg.EnablePrometheus = enablePrometheus
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.LogFile = logFile
|
|
|
|
cfg.LogLevel = logLevel
|
|
|
|
cfg.LogMaxDays = logMaxDays
|
|
|
|
cfg.SubDomainHost = subDomainHost
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.TLSOnly = tlsOnly
|
2020-03-01 10:57:01 +08:00
|
|
|
|
|
|
|
// Only token authentication is supported in cmd mode
|
2020-05-24 17:48:37 +08:00
|
|
|
cfg.ServerConfig = auth.GetDefaultServerConf()
|
2020-03-01 10:57:01 +08:00
|
|
|
cfg.Token = token
|
2018-06-07 00:25:55 +08:00
|
|
|
if len(allowPorts) > 0 {
|
|
|
|
// e.g. 1000-2000,2001,2002,3000-4000
|
|
|
|
ports, errRet := util.ParseRangeNumbers(allowPorts)
|
|
|
|
if errRet != nil {
|
|
|
|
err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range ports {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.AllowPorts[int(port)] = struct{}{}
|
2018-06-07 00:25:55 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.MaxPortsPerClient = maxPortsPerClient
|
2018-12-09 21:56:46 +08:00
|
|
|
|
|
|
|
if logFile == "console" {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.LogWay = "console"
|
2018-12-09 21:56:46 +08:00
|
|
|
} else {
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.LogWay = "file"
|
2018-12-09 21:56:46 +08:00
|
|
|
}
|
2019-08-20 02:07:37 +08:00
|
|
|
cfg.DisableLogColor = disableLogColor
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:07:37 +08:00
|
|
|
func runServer(cfg config.ServerCommonConf) (err error) {
|
|
|
|
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
|
|
|
|
svr, err := server.NewService(cfg)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-12 20:13:12 +08:00
|
|
|
log.Info("start frps success")
|
2018-04-10 17:46:49 +08:00
|
|
|
svr.Run()
|
|
|
|
return
|
|
|
|
}
|