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