frp/cmd/frpc/sub/root.go

255 lines
6.0 KiB
Go
Raw Normal View History

2018-04-10 17:46:49 +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.
package sub
import (
"context"
2018-04-10 17:46:49 +08:00
"fmt"
"net"
2018-04-10 17:46:49 +08:00
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/models/auth"
2018-04-10 17:46:49 +08:00
"github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/utils/log"
"github.com/fatedier/frp/utils/version"
)
const (
CfgFileTypeIni = iota
CfgFileTypeCmd
)
var (
cfgFile string
showVersion bool
serverAddr string
user string
protocol string
token string
logLevel string
logFile string
logMaxDays int
disableLogColor bool
2018-04-10 17:46:49 +08:00
proxyName string
2020-05-24 17:48:37 +08:00
localIP string
2018-04-10 17:46:49 +08:00
localPort int
remotePort int
useEncryption bool
useCompression bool
customDomains string
subDomain string
httpUser string
httpPwd string
locations string
hostHeaderRewrite string
role string
sk string
multiplexer string
2018-04-10 17:46:49 +08:00
serverName string
bindAddr string
bindPort int
2018-12-11 15:06:54 +08:00
tlsEnable bool
2018-12-11 15:06:54 +08:00
kcpDoneCh chan struct{}
2018-04-10 17:46:49 +08:00
)
func init() {
2019-04-08 15:39:14 +08:00
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
2018-04-10 17:46:49 +08:00
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
2018-12-11 15:06:54 +08:00
kcpDoneCh = make(chan struct{})
2018-04-10 17:46:49 +08:00
}
2020-05-19 10:49:29 +08:00
func RegisterCommonFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
cmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
cmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket")
cmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
cmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
cmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
cmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
cmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
cmd.PersistentFlags().BoolVarP(&tlsEnable, "tls_enable", "", false, "enable frpc tls")
}
2018-04-10 17:46:49 +08:00
var rootCmd = &cobra.Command{
Use: "frpc",
Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
RunE: func(cmd *cobra.Command, args []string) error {
if showVersion {
fmt.Println(version.Full())
return nil
}
// Do not show command usage here.
err := runClient(cfgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func handleSignal(svr *client.Service) {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
svr.Close()
time.Sleep(250 * time.Millisecond)
2018-12-11 15:06:54 +08:00
close(kcpDoneCh)
2018-04-10 17:46:49 +08:00
}
func parseClientCommonCfg(fileType int, content string) (cfg config.ClientCommonConf, err error) {
2018-04-10 17:46:49 +08:00
if fileType == CfgFileTypeIni {
cfg, err = parseClientCommonCfgFromIni(content)
2018-04-10 17:46:49 +08:00
} else if fileType == CfgFileTypeCmd {
cfg, err = parseClientCommonCfgFromCmd()
2018-04-10 17:46:49 +08:00
}
if err != nil {
return
}
err = cfg.Check()
2018-04-10 17:46:49 +08:00
if err != nil {
return
}
return
}
func parseClientCommonCfgFromIni(content string) (config.ClientCommonConf, error) {
cfg, err := config.UnmarshalClientConfFromIni(content)
2018-04-10 17:46:49 +08:00
if err != nil {
return config.ClientCommonConf{}, err
2018-04-10 17:46:49 +08:00
}
return cfg, err
2018-04-10 17:46:49 +08:00
}
func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
cfg = config.GetDefaultClientConf()
2018-04-10 17:46:49 +08:00
strs := strings.Split(serverAddr, ":")
if len(strs) < 2 {
err = fmt.Errorf("invalid server_addr")
return
}
if strs[0] != "" {
cfg.ServerAddr = strs[0]
2018-04-10 17:46:49 +08:00
}
cfg.ServerPort, err = strconv.Atoi(strs[1])
2018-04-10 17:46:49 +08:00
if err != nil {
err = fmt.Errorf("invalid server_addr")
return
}
cfg.User = user
cfg.Protocol = protocol
cfg.LogLevel = logLevel
cfg.LogFile = logFile
cfg.LogMaxDays = int64(logMaxDays)
2018-12-09 21:56:46 +08:00
if logFile == "console" {
cfg.LogWay = "console"
2018-12-09 21:56:46 +08:00
} else {
cfg.LogWay = "file"
2018-12-09 21:56:46 +08:00
}
cfg.DisableLogColor = disableLogColor
// Only token authentication is supported in cmd mode
2020-05-24 17:48:37 +08:00
cfg.ClientConfig = auth.GetDefaultClientConf()
cfg.Token = token
cfg.TLSEnable = tlsEnable
return
2018-04-10 17:46:49 +08:00
}
func runClient(cfgFilePath string) (err error) {
var content string
content, err = config.GetRenderedConfFromFile(cfgFilePath)
2018-04-10 17:46:49 +08:00
if err != nil {
return
}
cfg, err := parseClientCommonCfg(CfgFileTypeIni, content)
2018-04-10 17:46:49 +08:00
if err != nil {
return
2018-04-10 17:46:49 +08:00
}
pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(cfg.User, content, cfg.Start)
2018-04-10 17:46:49 +08:00
if err != nil {
return err
}
err = startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
2018-04-10 17:46:49 +08:00
return
}
2020-06-02 22:48:55 +08:00
func startService(
cfg config.ClientCommonConf,
pxyCfgs map[string]config.ProxyConf,
visitorCfgs map[string]config.VisitorConf,
cfgFile string,
) (err error) {
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel,
cfg.LogMaxDays, cfg.DisableLogColor)
2020-05-24 17:48:37 +08:00
if cfg.DNSServer != "" {
s := cfg.DNSServer
if !strings.Contains(s, ":") {
s += ":53"
}
// Change default dns server for frpc
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", s)
},
}
}
svr, errRet := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
2019-02-01 19:26:10 +08:00
if errRet != nil {
err = errRet
return
}
2018-04-10 17:46:49 +08:00
// Capture the exit signal if we use kcp.
if cfg.Protocol == "kcp" {
2018-04-10 17:46:49 +08:00
go handleSignal(svr)
}
err = svr.Run()
if cfg.Protocol == "kcp" {
2018-12-11 15:06:54 +08:00
<-kcpDoneCh
}
2018-04-10 17:46:49 +08:00
return
}