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"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/models/config"
|
|
|
|
"github.com/fatedier/frp/models/consts"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
|
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
|
2019-01-26 12:52:12 +08:00
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket")
|
2018-04-10 17:46:49 +08:00
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
|
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
|
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
|
|
|
|
xtcpCmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
|
2019-08-12 00:47:35 +08:00
|
|
|
xtcpCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
|
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")
|
|
|
|
xtcpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip")
|
|
|
|
xtcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
|
2018-05-16 23:45:44 +08:00
|
|
|
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")
|
|
|
|
|
|
|
|
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 {
|
2019-08-21 04:53:27 +08:00
|
|
|
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "")
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
proxyConfs := make(map[string]config.ProxyConf)
|
|
|
|
visitorConfs := make(map[string]config.VisitorConf)
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
var prefix string
|
|
|
|
if user != "" {
|
|
|
|
prefix = user + "."
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
if role == "server" {
|
|
|
|
cfg := &config.XtcpProxyConf{}
|
|
|
|
cfg.ProxyName = prefix + proxyName
|
2019-03-03 23:44:44 +08:00
|
|
|
cfg.ProxyType = consts.XtcpProxy
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.UseEncryption = useEncryption
|
|
|
|
cfg.UseCompression = useCompression
|
|
|
|
cfg.Role = role
|
|
|
|
cfg.Sk = sk
|
|
|
|
cfg.LocalIp = localIp
|
|
|
|
cfg.LocalPort = localPort
|
|
|
|
err = cfg.CheckForCli()
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
proxyConfs[cfg.ProxyName] = cfg
|
|
|
|
} else if role == "visitor" {
|
|
|
|
cfg := &config.XtcpVisitorConf{}
|
|
|
|
cfg.ProxyName = prefix + proxyName
|
2019-03-03 23:44:44 +08:00
|
|
|
cfg.ProxyType = consts.XtcpProxy
|
2018-06-25 18:22:35 +08:00
|
|
|
cfg.UseEncryption = useEncryption
|
|
|
|
cfg.UseCompression = useCompression
|
|
|
|
cfg.Role = role
|
|
|
|
cfg.Sk = sk
|
|
|
|
cfg.ServerName = serverName
|
|
|
|
cfg.BindAddr = bindAddr
|
|
|
|
cfg.BindPort = bindPort
|
|
|
|
err = cfg.Check()
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
visitorConfs[cfg.ProxyName] = cfg
|
|
|
|
} else {
|
|
|
|
fmt.Println("invalid role")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
err = startService(clientCfg, proxyConfs, visitorConfs, "")
|
2018-06-25 18:22:35 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|