"Fossies" - the Fresh Open Source Software Archive

Member "frp-0.36.2/cmd/frps/root.go" (22 Mar 2021, 6816 Bytes) of package /linux/misc/frp-0.36.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "root.go": 0.36.1_vs_0.36.2.

    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 
   15 package main
   16 
   17 import (
   18     "fmt"
   19     "os"
   20 
   21     "github.com/fatedier/frp/pkg/auth"
   22     "github.com/fatedier/frp/pkg/config"
   23     "github.com/fatedier/frp/pkg/util/log"
   24     "github.com/fatedier/frp/pkg/util/util"
   25     "github.com/fatedier/frp/pkg/util/version"
   26     "github.com/fatedier/frp/server"
   27 
   28     "github.com/spf13/cobra"
   29 )
   30 
   31 const (
   32     CfgFileTypeIni = iota
   33     CfgFileTypeCmd
   34 )
   35 
   36 var (
   37     cfgFile     string
   38     showVersion bool
   39 
   40     bindAddr          string
   41     bindPort          int
   42     bindUDPPort       int
   43     kcpBindPort       int
   44     proxyBindAddr     string
   45     vhostHTTPPort     int
   46     vhostHTTPSPort    int
   47     vhostHTTPTimeout  int64
   48     dashboardAddr     string
   49     dashboardPort     int
   50     dashboardUser     string
   51     dashboardPwd      string
   52     enablePrometheus  bool
   53     assetsDir         string
   54     logFile           string
   55     logLevel          string
   56     logMaxDays        int64
   57     disableLogColor   bool
   58     token             string
   59     subDomainHost     string
   60     tcpMux            bool
   61     allowPorts        string
   62     maxPoolCount      int64
   63     maxPortsPerClient int64
   64     tlsOnly           bool
   65 )
   66 
   67 func init() {
   68     rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file of frps")
   69     rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frps")
   70 
   71     rootCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "0.0.0.0", "bind address")
   72     rootCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "p", 7000, "bind port")
   73     rootCmd.PersistentFlags().IntVarP(&bindUDPPort, "bind_udp_port", "", 0, "bind udp port")
   74     rootCmd.PersistentFlags().IntVarP(&kcpBindPort, "kcp_bind_port", "", 0, "kcp bind udp port")
   75     rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "proxy_bind_addr", "", "0.0.0.0", "proxy bind address")
   76     rootCmd.PersistentFlags().IntVarP(&vhostHTTPPort, "vhost_http_port", "", 0, "vhost http port")
   77     rootCmd.PersistentFlags().IntVarP(&vhostHTTPSPort, "vhost_https_port", "", 0, "vhost https port")
   78     rootCmd.PersistentFlags().Int64VarP(&vhostHTTPTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout")
   79     rootCmd.PersistentFlags().StringVarP(&dashboardAddr, "dashboard_addr", "", "0.0.0.0", "dasboard address")
   80     rootCmd.PersistentFlags().IntVarP(&dashboardPort, "dashboard_port", "", 0, "dashboard port")
   81     rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user")
   82     rootCmd.PersistentFlags().StringVarP(&dashboardPwd, "dashboard_pwd", "", "admin", "dashboard password")
   83     rootCmd.PersistentFlags().BoolVarP(&enablePrometheus, "enable_prometheus", "", false, "enable prometheus dashboard")
   84     rootCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "log file")
   85     rootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
   86     rootCmd.PersistentFlags().Int64VarP(&logMaxDays, "log_max_days", "", 3, "log max days")
   87     rootCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
   88 
   89     rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
   90     rootCmd.PersistentFlags().StringVarP(&subDomainHost, "subdomain_host", "", "", "subdomain host")
   91     rootCmd.PersistentFlags().StringVarP(&allowPorts, "allow_ports", "", "", "allow ports")
   92     rootCmd.PersistentFlags().Int64VarP(&maxPortsPerClient, "max_ports_per_client", "", 0, "max ports per client")
   93     rootCmd.PersistentFlags().BoolVarP(&tlsOnly, "tls_only", "", false, "frps tls only")
   94 }
   95 
   96 var rootCmd = &cobra.Command{
   97     Use:   "frps",
   98     Short: "frps is the server of frp (https://github.com/fatedier/frp)",
   99     RunE: func(cmd *cobra.Command, args []string) error {
  100         if showVersion {
  101             fmt.Println(version.Full())
  102             return nil
  103         }
  104 
  105         var cfg config.ServerCommonConf
  106         var err error
  107         if cfgFile != "" {
  108             var content []byte
  109             content, err = config.GetRenderedConfFromFile(cfgFile)
  110             if err != nil {
  111                 return err
  112             }
  113             cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
  114         } else {
  115             cfg, err = parseServerCommonCfg(CfgFileTypeCmd, nil)
  116         }
  117         if err != nil {
  118             return err
  119         }
  120 
  121         err = runServer(cfg)
  122         if err != nil {
  123             fmt.Println(err)
  124             os.Exit(1)
  125         }
  126         return nil
  127     },
  128 }
  129 
  130 func Execute() {
  131     if err := rootCmd.Execute(); err != nil {
  132         os.Exit(1)
  133     }
  134 }
  135 
  136 func parseServerCommonCfg(fileType int, source []byte) (cfg config.ServerCommonConf, err error) {
  137     if fileType == CfgFileTypeIni {
  138         cfg, err = config.UnmarshalServerConfFromIni(source)
  139     } else if fileType == CfgFileTypeCmd {
  140         cfg, err = parseServerCommonCfgFromCmd()
  141     }
  142     if err != nil {
  143         return
  144     }
  145     cfg.Complete()
  146     err = cfg.Validate()
  147     if err != nil {
  148         err = fmt.Errorf("Parse config error: %v", err)
  149         return
  150     }
  151     return
  152 }
  153 
  154 func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
  155     cfg = config.GetDefaultServerConf()
  156 
  157     cfg.BindAddr = bindAddr
  158     cfg.BindPort = bindPort
  159     cfg.BindUDPPort = bindUDPPort
  160     cfg.KCPBindPort = kcpBindPort
  161     cfg.ProxyBindAddr = proxyBindAddr
  162     cfg.VhostHTTPPort = vhostHTTPPort
  163     cfg.VhostHTTPSPort = vhostHTTPSPort
  164     cfg.VhostHTTPTimeout = vhostHTTPTimeout
  165     cfg.DashboardAddr = dashboardAddr
  166     cfg.DashboardPort = dashboardPort
  167     cfg.DashboardUser = dashboardUser
  168     cfg.DashboardPwd = dashboardPwd
  169     cfg.EnablePrometheus = enablePrometheus
  170     cfg.LogFile = logFile
  171     cfg.LogLevel = logLevel
  172     cfg.LogMaxDays = logMaxDays
  173     cfg.SubDomainHost = subDomainHost
  174     cfg.TLSOnly = tlsOnly
  175 
  176     // Only token authentication is supported in cmd mode
  177     cfg.ServerConfig = auth.GetDefaultServerConf()
  178     cfg.Token = token
  179     if len(allowPorts) > 0 {
  180         // e.g. 1000-2000,2001,2002,3000-4000
  181         ports, errRet := util.ParseRangeNumbers(allowPorts)
  182         if errRet != nil {
  183             err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
  184             return
  185         }
  186 
  187         for _, port := range ports {
  188             cfg.AllowPorts[int(port)] = struct{}{}
  189         }
  190     }
  191     cfg.MaxPortsPerClient = maxPortsPerClient
  192     cfg.DisableLogColor = disableLogColor
  193     return
  194 }
  195 
  196 func runServer(cfg config.ServerCommonConf) (err error) {
  197     log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
  198 
  199     if cfgFile != "" {
  200         log.Info("frps uses config file: %s", cfgFile)
  201     } else {
  202         log.Info("frps uses command line arguments for config")
  203     }
  204 
  205     svr, err := server.NewService(cfg)
  206     if err != nil {
  207         return err
  208     }
  209     log.Info("frps started successfully")
  210     svr.Run()
  211     return
  212 }