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 (
|
2023-06-29 18:04:20 +08:00
|
|
|
"context"
|
2018-04-10 17:46:49 +08:00
|
|
|
"fmt"
|
2022-03-28 12:12:35 +08:00
|
|
|
"io/fs"
|
2018-04-23 02:59:40 +08:00
|
|
|
"net"
|
2018-04-10 17:46:49 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-03-28 12:12:35 +08:00
|
|
|
"path/filepath"
|
2018-04-10 17:46:49 +08:00
|
|
|
"strconv"
|
2022-03-28 12:12:35 +08:00
|
|
|
"sync"
|
2018-04-10 17:46:49 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
"github.com/samber/lo"
|
2022-08-29 01:02:53 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
"github.com/fatedier/frp/client"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgFile string
|
2022-03-28 12:12:35 +08:00
|
|
|
cfgDir string
|
2018-04-10 17:46:49 +08:00
|
|
|
showVersion bool
|
|
|
|
|
2019-08-12 00:47:35 +08:00
|
|
|
serverAddr string
|
|
|
|
user string
|
|
|
|
protocol string
|
|
|
|
token string
|
|
|
|
logLevel string
|
|
|
|
logFile string
|
|
|
|
logMaxDays int
|
|
|
|
disableLogColor bool
|
2023-05-28 16:50:43 +08:00
|
|
|
dnsServer string
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2023-02-09 00:38:36 +08:00
|
|
|
proxyName string
|
|
|
|
localIP string
|
|
|
|
localPort int
|
|
|
|
remotePort int
|
|
|
|
useEncryption bool
|
|
|
|
useCompression bool
|
|
|
|
bandwidthLimit string
|
|
|
|
bandwidthLimitMode string
|
|
|
|
customDomains string
|
|
|
|
subDomain string
|
|
|
|
httpUser string
|
|
|
|
httpPwd string
|
|
|
|
locations string
|
|
|
|
hostHeaderRewrite string
|
|
|
|
role string
|
|
|
|
sk string
|
|
|
|
multiplexer string
|
|
|
|
serverName string
|
|
|
|
bindAddr string
|
|
|
|
bindPort int
|
2018-12-11 15:06:54 +08:00
|
|
|
|
2023-06-29 11:20:45 +08:00
|
|
|
tlsEnable bool
|
|
|
|
tlsServerName string
|
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")
|
2022-03-28 12:12:35 +08:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
|
2018-04-10 17:46:49 +08:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
|
|
|
|
}
|
|
|
|
|
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")
|
2023-06-29 11:20:45 +08:00
|
|
|
cmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp, kcp, quic, websocket, wss")
|
2020-05-19 10:49:29 +08:00
|
|
|
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")
|
2023-06-26 00:10:27 +08:00
|
|
|
cmd.PersistentFlags().BoolVarP(&tlsEnable, "tls_enable", "", true, "enable frpc tls")
|
2023-06-29 11:20:45 +08:00
|
|
|
cmd.PersistentFlags().StringVarP(&tlsServerName, "tls_server_name", "", "", "specify the custom server name of tls certificate")
|
2023-05-28 16:50:43 +08:00
|
|
|
cmd.PersistentFlags().StringVarP(&dnsServer, "dns_server", "", "", "specify dns server instead of using system default one")
|
2020-05-19 10:49:29 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-28 12:12:35 +08:00
|
|
|
// If cfgDir is not empty, run multiple frpc service for each config file in cfgDir.
|
|
|
|
// Note that it's only designed for testing. It's not guaranteed to be stable.
|
|
|
|
if cfgDir != "" {
|
2023-05-28 16:50:43 +08:00
|
|
|
_ = runMultipleClients(cfgDir)
|
2022-03-28 12:12:35 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
// Do not show command usage here.
|
|
|
|
err := runClient(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
func runMultipleClients(cfgDir string) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
err := filepath.WalkDir(cfgDir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil || d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := runClient(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("frpc service error for config file [%s]\n", path)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
func handleTermSignal(svr *client.Service) {
|
2022-03-28 12:12:35 +08:00
|
|
|
ch := make(chan os.Signal, 1)
|
2018-04-10 17:46:49 +08:00
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
2021-10-19 15:02:45 +08:00
|
|
|
svr.GracefulClose(500 * time.Millisecond)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func parseClientCommonCfgFromCmd() (*v1.ClientCommonConfig, error) {
|
|
|
|
cfg := &v1.ClientCommonConfig{}
|
2019-08-21 04:53:27 +08:00
|
|
|
|
2020-12-24 21:48:26 +08:00
|
|
|
ipStr, portStr, err := net.SplitHostPort(serverAddr)
|
|
|
|
if err != nil {
|
2023-09-06 10:18:02 +08:00
|
|
|
return nil, fmt.Errorf("invalid server_addr: %v", err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2020-12-24 21:48:26 +08:00
|
|
|
|
|
|
|
cfg.ServerAddr = ipStr
|
|
|
|
cfg.ServerPort, err = strconv.Atoi(portStr)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
2023-09-06 10:18:02 +08:00
|
|
|
return nil, fmt.Errorf("invalid server_addr: %v", err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
cfg.User = user
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg.Transport.Protocol = protocol
|
|
|
|
cfg.Log.Level = logLevel
|
|
|
|
cfg.Log.To = logFile
|
|
|
|
cfg.Log.MaxDays = int64(logMaxDays)
|
|
|
|
cfg.Log.DisablePrintColor = disableLogColor
|
2023-05-28 16:50:43 +08:00
|
|
|
cfg.DNSServer = dnsServer
|
2019-08-21 04:53:27 +08:00
|
|
|
|
2020-03-01 10:57:01 +08:00
|
|
|
// Only token authentication is supported in cmd mode
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg.Auth.Token = token
|
|
|
|
cfg.Transport.TLS.Enable = lo.ToPtr(tlsEnable)
|
|
|
|
cfg.Transport.TLS.ServerName = tlsServerName
|
2020-03-01 10:57:01 +08:00
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
cfg.Complete()
|
2023-09-06 10:18:02 +08:00
|
|
|
|
|
|
|
err, warning := validation.ValidateClientCommonConfig(cfg)
|
|
|
|
if warning != nil {
|
|
|
|
fmt.Printf("WARNING: %v\n", warning)
|
2021-06-02 23:54:22 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse config error: %v", err)
|
|
|
|
}
|
|
|
|
return cfg, nil
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 00:39:05 +08:00
|
|
|
func runClient(cfgFilePath string) error {
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg, pxyCfgs, visitorCfgs, isLegacyFormat, err := config.LoadClientConfig(cfgFilePath)
|
2021-06-02 00:39:05 +08:00
|
|
|
if err != nil {
|
2023-05-30 10:55:00 +08:00
|
|
|
fmt.Println(err)
|
2021-06-02 00:39:05 +08:00
|
|
|
return err
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
warning, err := validation.ValidateAllClientConfig(cfg, pxyCfgs, visitorCfgs)
|
|
|
|
if warning != nil {
|
|
|
|
fmt.Printf("WARNING: %v\n", warning)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-02 00:39:05 +08:00
|
|
|
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:48:55 +08:00
|
|
|
func startService(
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg *v1.ClientCommonConfig,
|
|
|
|
pxyCfgs []v1.ProxyConfigurer,
|
|
|
|
visitorCfgs []v1.VisitorConfigurer,
|
2020-06-02 22:48:55 +08:00
|
|
|
cfgFile string,
|
2023-09-06 10:18:02 +08:00
|
|
|
) error {
|
|
|
|
log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
|
2019-08-12 00:47:35 +08:00
|
|
|
|
2022-03-28 12:12:35 +08:00
|
|
|
if cfgFile != "" {
|
2023-05-30 10:55:00 +08:00
|
|
|
log.Info("start frpc service for config file [%s]", cfgFile)
|
|
|
|
defer log.Info("frpc service for config file [%s] stopped", cfgFile)
|
2018-04-23 02:59:40 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
svr, err := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-01 19:26:10 +08:00
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic"
|
2023-02-01 13:09:31 +08:00
|
|
|
// Capture the exit signal if we use kcp or quic.
|
|
|
|
if shouldGracefulClose {
|
2023-06-30 17:35:37 +08:00
|
|
|
go handleTermSignal(svr)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
2023-06-30 17:35:37 +08:00
|
|
|
_ = svr.Run(context.Background())
|
2023-09-06 10:18:02 +08:00
|
|
|
return nil
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|