mirror of
https://github.com/fatedier/frp.git
synced 2024-12-04 02:36:39 +08:00
246 lines
6.9 KiB
Go
246 lines
6.9 KiB
Go
// 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"
|
|
"fmt"
|
|
"io/fs"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/samber/lo"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/fatedier/frp/client"
|
|
"github.com/fatedier/frp/pkg/config"
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
"github.com/fatedier/frp/pkg/config/v1/validation"
|
|
"github.com/fatedier/frp/pkg/util/log"
|
|
"github.com/fatedier/frp/pkg/util/version"
|
|
)
|
|
|
|
var (
|
|
cfgFile string
|
|
cfgDir string
|
|
showVersion bool
|
|
|
|
serverAddr string
|
|
user string
|
|
protocol string
|
|
token string
|
|
logLevel string
|
|
logFile string
|
|
logMaxDays int
|
|
disableLogColor bool
|
|
dnsServer string
|
|
|
|
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
|
|
|
|
tlsEnable bool
|
|
tlsServerName string
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "./frpc.ini", "config file of frpc")
|
|
rootCmd.PersistentFlags().StringVarP(&cfgDir, "config_dir", "", "", "config directory, run one frpc service for each file in config directory")
|
|
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
|
|
}
|
|
|
|
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, kcp, quic, websocket, wss")
|
|
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", "", true, "enable frpc tls")
|
|
cmd.PersistentFlags().StringVarP(&tlsServerName, "tls_server_name", "", "", "specify the custom server name of tls certificate")
|
|
cmd.PersistentFlags().StringVarP(&dnsServer, "dns_server", "", "", "specify dns server instead of using system default one")
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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 != "" {
|
|
_ = runMultipleClients(cfgDir)
|
|
return nil
|
|
}
|
|
|
|
// Do not show command usage here.
|
|
err := runClient(cfgFile)
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func handleTermSignal(svr *client.Service) {
|
|
ch := make(chan os.Signal, 1)
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
<-ch
|
|
svr.GracefulClose(500 * time.Millisecond)
|
|
}
|
|
|
|
func parseClientCommonCfgFromCmd() (*v1.ClientCommonConfig, error) {
|
|
cfg := &v1.ClientCommonConfig{}
|
|
|
|
ipStr, portStr, err := net.SplitHostPort(serverAddr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid server_addr: %v", err)
|
|
}
|
|
|
|
cfg.ServerAddr = ipStr
|
|
cfg.ServerPort, err = strconv.Atoi(portStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid server_addr: %v", err)
|
|
}
|
|
|
|
cfg.User = user
|
|
cfg.Transport.Protocol = protocol
|
|
cfg.Log.Level = logLevel
|
|
cfg.Log.To = logFile
|
|
cfg.Log.MaxDays = int64(logMaxDays)
|
|
cfg.Log.DisablePrintColor = disableLogColor
|
|
cfg.DNSServer = dnsServer
|
|
|
|
// Only token authentication is supported in cmd mode
|
|
cfg.Auth.Token = token
|
|
cfg.Transport.TLS.Enable = lo.ToPtr(tlsEnable)
|
|
cfg.Transport.TLS.ServerName = tlsServerName
|
|
|
|
cfg.Complete()
|
|
|
|
warning, err := validation.ValidateClientCommonConfig(cfg)
|
|
if warning != nil {
|
|
fmt.Printf("WARNING: %v\n", warning)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse config error: %v", err)
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func runClient(cfgFilePath string) error {
|
|
cfg, pxyCfgs, visitorCfgs, isLegacyFormat, err := config.LoadClientConfig(cfgFilePath)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
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
|
|
}
|
|
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
|
|
}
|
|
|
|
func startService(
|
|
cfg *v1.ClientCommonConfig,
|
|
pxyCfgs []v1.ProxyConfigurer,
|
|
visitorCfgs []v1.VisitorConfigurer,
|
|
cfgFile string,
|
|
) error {
|
|
log.InitLog(cfg.Log.To, cfg.Log.Level, cfg.Log.MaxDays, cfg.Log.DisablePrintColor)
|
|
|
|
if cfgFile != "" {
|
|
log.Info("start frpc service for config file [%s]", cfgFile)
|
|
defer log.Info("frpc service for config file [%s] stopped", cfgFile)
|
|
}
|
|
svr, err := client.NewService(cfg, pxyCfgs, visitorCfgs, cfgFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
shouldGracefulClose := cfg.Transport.Protocol == "kcp" || cfg.Transport.Protocol == "quic"
|
|
// Capture the exit signal if we use kcp or quic.
|
|
if shouldGracefulClose {
|
|
go handleTermSignal(svr)
|
|
}
|
|
|
|
_ = svr.Run(context.Background())
|
|
return nil
|
|
}
|