frp/cmd/frpc/sub/xtcp.go

117 lines
3.6 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 (
"fmt"
"os"
2022-08-29 01:02:53 +08:00
"github.com/spf13/cobra"
"github.com/fatedier/frp/pkg/config/types"
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/consts"
2018-04-10 17:46:49 +08:00
)
func init() {
2020-05-19 10:49:29 +08:00
RegisterCommonFlags(xtcpCmd)
2018-04-10 17:46:49 +08:00
xtcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
xtcpCmd.PersistentFlags().StringVarP(&role, "role", "", "server", "role")
xtcpCmd.PersistentFlags().StringVarP(&sk, "sk", "", "", "secret key")
xtcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name")
2020-05-24 17:48:37 +08:00
xtcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
2018-04-10 17:46:49 +08:00
xtcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
xtcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr")
xtcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port")
2018-04-10 17:46:49 +08:00
xtcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
xtcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
xtcpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
xtcpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", types.BandwidthLimitModeClient, "bandwidth limit mode")
2018-04-10 17:46:49 +08:00
rootCmd.AddCommand(xtcpCmd)
}
var xtcpCmd = &cobra.Command{
2018-04-24 01:51:52 +08:00
Use: "xtcp",
Short: "Run frpc with a single xtcp proxy",
2018-04-10 17:46:49 +08:00
RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfgFromCmd()
2018-04-10 17:46:49 +08:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pxyCfgs := make([]v1.ProxyConfigurer, 0)
visitorCfgs := make([]v1.VisitorConfigurer, 0)
2018-04-10 17:46:49 +08:00
var prefix string
if user != "" {
prefix = user + "."
}
2022-12-22 17:55:06 +08:00
switch role {
case "server":
cfg := &v1.XTCPProxyConfig{}
cfg.Name = prefix + proxyName
cfg.Type = consts.XTCPProxy
cfg.Transport.UseEncryption = useEncryption
cfg.Transport.UseCompression = useCompression
cfg.Secretkey = sk
2020-05-24 17:48:37 +08:00
cfg.LocalIP = localIP
cfg.LocalPort = localPort
cfg.Transport.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidthLimit)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cfg.Transport.BandwidthLimitMode = bandwidthLimitMode
if err := validation.ValidateProxyConfigurerForClient(cfg); err != nil {
2018-04-10 17:46:49 +08:00
fmt.Println(err)
os.Exit(1)
}
pxyCfgs = append(pxyCfgs, cfg)
2022-12-22 17:55:06 +08:00
case "visitor":
cfg := &v1.XTCPVisitorConfig{}
cfg.Name = prefix + proxyName
cfg.Type = consts.XTCPProxy
cfg.Transport.UseEncryption = useEncryption
cfg.Transport.UseCompression = useCompression
cfg.SecretKey = sk
cfg.ServerName = serverName
cfg.BindAddr = bindAddr
cfg.BindPort = bindPort
if err := validation.ValidateVisitorConfigurer(cfg); err != nil {
2018-04-10 17:46:49 +08:00
fmt.Println(err)
os.Exit(1)
}
visitorCfgs = append(visitorCfgs, cfg)
2022-12-22 17:55:06 +08:00
default:
fmt.Println("invalid role")
os.Exit(1)
}
err = startService(clientCfg, pxyCfgs, visitorCfgs, "")
if err != nil {
fmt.Println(err)
os.Exit(1)
2018-04-10 17:46:49 +08:00
}
return nil
},
}