2018-04-10 11:46:49 +02: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 (
|
2021-06-01 18:39:05 +02:00
|
|
|
"bytes"
|
2018-04-22 20:59:40 +02:00
|
|
|
"context"
|
2018-04-10 11:46:49 +02:00
|
|
|
"fmt"
|
2021-06-01 18:39:05 +02:00
|
|
|
"io/ioutil"
|
2018-04-22 20:59:40 +02:00
|
|
|
"net"
|
2018-04-10 11:46:49 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-06-01 18:39:05 +02:00
|
|
|
"path/filepath"
|
2018-04-10 11:46:49 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/client"
|
2020-09-23 07:49:14 +02: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/version"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-04-10 11:46:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CfgFileTypeIni = iota
|
|
|
|
CfgFileTypeCmd
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgFile string
|
|
|
|
showVersion bool
|
|
|
|
|
2019-08-11 18:47:35 +02:00
|
|
|
serverAddr string
|
|
|
|
user string
|
|
|
|
protocol string
|
|
|
|
token string
|
|
|
|
logLevel string
|
|
|
|
logFile string
|
|
|
|
logMaxDays int
|
|
|
|
disableLogColor bool
|
2018-04-10 11:46:49 +02:00
|
|
|
|
|
|
|
proxyName string
|
2020-05-24 11:48:37 +02:00
|
|
|
localIP string
|
2018-04-10 11:46:49 +02: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
|
2020-03-05 14:47:49 +01:00
|
|
|
multiplexer string
|
2018-04-10 11:46:49 +02:00
|
|
|
serverName string
|
|
|
|
bindAddr string
|
2018-05-16 17:45:44 +02:00
|
|
|
bindPort int
|
2018-12-11 08:06:54 +01:00
|
|
|
|
2020-05-12 08:33:34 +02:00
|
|
|
tlsEnable bool
|
|
|
|
|
2018-12-11 08:06:54 +01:00
|
|
|
kcpDoneCh chan struct{}
|
2018-04-10 11:46:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-04-08 09:39:14 +02:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
|
2018-04-10 11:46:49 +02:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
|
2018-12-11 08:06:54 +01:00
|
|
|
|
|
|
|
kcpDoneCh = make(chan struct{})
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 04:49:29 +02: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 11:46:49 +02: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 08:06:54 +01:00
|
|
|
close(kcpDoneCh)
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 04:31:08 +01:00
|
|
|
func parseClientCommonCfg(fileType int, source []byte) (cfg config.ClientCommonConf, err error) {
|
2018-04-10 11:46:49 +02:00
|
|
|
if fileType == CfgFileTypeIni {
|
2021-01-26 04:31:08 +01:00
|
|
|
cfg, err = config.UnmarshalClientConfFromIni(source)
|
2018-04-10 11:46:49 +02:00
|
|
|
} else if fileType == CfgFileTypeCmd {
|
2019-08-20 22:53:27 +02:00
|
|
|
cfg, err = parseClientCommonCfgFromCmd()
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-22 07:53:30 +01:00
|
|
|
|
|
|
|
cfg.Complete()
|
|
|
|
err = cfg.Validate()
|
2018-04-10 11:46:49 +02:00
|
|
|
if err != nil {
|
2021-03-22 07:53:30 +01:00
|
|
|
err = fmt.Errorf("Parse config error: %v", err)
|
2018-04-10 11:46:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
|
|
|
|
cfg = config.GetDefaultClientConf()
|
|
|
|
|
2020-12-24 14:48:26 +01:00
|
|
|
ipStr, portStr, err := net.SplitHostPort(serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid server_addr: %v", err)
|
2018-04-10 11:46:49 +02:00
|
|
|
return
|
|
|
|
}
|
2020-12-24 14:48:26 +01:00
|
|
|
|
|
|
|
cfg.ServerAddr = ipStr
|
|
|
|
cfg.ServerPort, err = strconv.Atoi(portStr)
|
2018-04-10 11:46:49 +02:00
|
|
|
if err != nil {
|
2020-12-24 14:48:26 +01:00
|
|
|
err = fmt.Errorf("invalid server_addr: %v", err)
|
2018-04-10 11:46:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
cfg.User = user
|
|
|
|
cfg.Protocol = protocol
|
|
|
|
cfg.LogLevel = logLevel
|
|
|
|
cfg.LogFile = logFile
|
|
|
|
cfg.LogMaxDays = int64(logMaxDays)
|
|
|
|
cfg.DisableLogColor = disableLogColor
|
|
|
|
|
2020-03-01 03:57:01 +01:00
|
|
|
// Only token authentication is supported in cmd mode
|
2020-05-24 11:48:37 +02:00
|
|
|
cfg.ClientConfig = auth.GetDefaultClientConf()
|
2020-03-01 03:57:01 +01:00
|
|
|
cfg.Token = token
|
2020-05-12 08:33:34 +02:00
|
|
|
cfg.TLSEnable = tlsEnable
|
2020-03-01 03:57:01 +01:00
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
return
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 18:39:05 +02:00
|
|
|
func runClient(cfgFilePath string) error {
|
|
|
|
cfg, pxyCfgs, visitorCfgs, err := parseConfig(cfgFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseConfig(cfgFilePath string) (
|
|
|
|
cfg config.ClientCommonConf,
|
|
|
|
pxyCfgs map[string]config.ProxyConf,
|
|
|
|
visitorCfgs map[string]config.VisitorConf,
|
|
|
|
err error,
|
|
|
|
) {
|
2021-01-26 04:31:08 +01:00
|
|
|
var content []byte
|
2018-12-11 04:46:12 +01:00
|
|
|
content, err = config.GetRenderedConfFromFile(cfgFilePath)
|
2018-04-10 11:46:49 +02:00
|
|
|
if err != nil {
|
2021-06-01 18:39:05 +02:00
|
|
|
return
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
2021-06-01 18:39:05 +02:00
|
|
|
configBuffer := bytes.NewBuffer(nil)
|
|
|
|
configBuffer.Write(content)
|
2018-04-10 11:46:49 +02:00
|
|
|
|
2021-06-01 18:39:05 +02:00
|
|
|
// Parse common section.
|
|
|
|
cfg, err = parseClientCommonCfg(CfgFileTypeIni, content)
|
2018-04-10 11:46:49 +02:00
|
|
|
if err != nil {
|
2021-06-01 18:39:05 +02:00
|
|
|
return
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 18:39:05 +02:00
|
|
|
// Aggregate proxy configs from include files.
|
|
|
|
var buf []byte
|
|
|
|
buf, err = getIncludeContents(cfg.IncludeConfigFiles)
|
2018-04-10 11:46:49 +02:00
|
|
|
if err != nil {
|
2021-06-01 18:39:05 +02:00
|
|
|
err = fmt.Errorf("getIncludeContents error: %v", err)
|
|
|
|
return
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
2021-06-01 18:39:05 +02:00
|
|
|
configBuffer.WriteString("\n")
|
|
|
|
configBuffer.Write(buf)
|
2018-04-10 11:46:49 +02:00
|
|
|
|
2021-06-01 18:39:05 +02:00
|
|
|
// Parse all proxy and visitor configs.
|
|
|
|
pxyCfgs, visitorCfgs, err = config.LoadAllProxyConfsFromIni(cfg.User, configBuffer.Bytes(), cfg.Start)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// getIncludeContents renders all configs from paths.
|
|
|
|
// files format can be a single file path or directory or regex path.
|
|
|
|
func getIncludeContents(paths []string) ([]byte, error) {
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
for _, path := range paths {
|
|
|
|
absDir, err := filepath.Abs(filepath.Dir(path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(absDir); os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files, err := ioutil.ReadDir(absDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
absFile := filepath.Join(absDir, fi.Name())
|
|
|
|
if matched, _ := filepath.Match(filepath.Join(absDir, filepath.Base(path)), absFile); matched {
|
|
|
|
tmpContent, err := config.GetRenderedConfFromFile(absFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("render extra config %s error: %v", absFile, err)
|
|
|
|
}
|
|
|
|
out.Write(tmpContent)
|
|
|
|
out.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.Bytes(), nil
|
2018-04-10 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 16:48:55 +02:00
|
|
|
func startService(
|
|
|
|
cfg config.ClientCommonConf,
|
|
|
|
pxyCfgs map[string]config.ProxyConf,
|
|
|
|
visitorCfgs map[string]config.VisitorConf,
|
|
|
|
cfgFile string,
|
|
|
|
) (err error) {
|
|
|
|
|
2019-08-20 22:53:27 +02:00
|
|
|
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel,
|
|
|
|
cfg.LogMaxDays, cfg.DisableLogColor)
|
2019-08-11 18:47:35 +02:00
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
if cfg.DNSServer != "" {
|
|
|
|
s := cfg.DNSServer
|
2018-04-22 20:59:40 +02:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 22:53:27 +02:00
|
|
|
svr, errRet := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
|
2019-02-01 12:26:10 +01:00
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 11:46:49 +02:00
|
|
|
|
|
|
|
// Capture the exit signal if we use kcp.
|
2019-08-20 22:53:27 +02:00
|
|
|
if cfg.Protocol == "kcp" {
|
2018-04-10 11:46:49 +02:00
|
|
|
go handleSignal(svr)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svr.Run()
|
2021-04-22 12:33:10 +02:00
|
|
|
if err == nil && cfg.Protocol == "kcp" {
|
2018-12-11 08:06:54 +01:00
|
|
|
<-kcpDoneCh
|
|
|
|
}
|
2018-04-10 11:46:49 +02:00
|
|
|
return
|
|
|
|
}
|