frp/cmd/frpc/sub/tcp.go

80 lines
2.2 KiB
Go
Raw Normal View History

2018-04-10 11:46:49 +02: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"
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/consts"
2018-04-10 11:46:49 +02:00
)
func init() {
2020-05-19 04:49:29 +02:00
RegisterCommonFlags(tcpCmd)
2018-04-10 11:46:49 +02:00
tcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
2020-05-24 11:48:37 +02:00
tcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
2018-04-10 11:46:49 +02:00
tcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
tcpCmd.PersistentFlags().IntVarP(&remotePort, "remote_port", "r", 0, "remote port")
tcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
tcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
rootCmd.AddCommand(tcpCmd)
}
var tcpCmd = &cobra.Command{
Use: "tcp",
Short: "Run frpc with a single tcp proxy",
RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "")
2018-04-10 11:46:49 +02:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2020-05-24 11:48:37 +02:00
cfg := &config.TCPProxyConf{}
2018-04-10 11:46:49 +02:00
var prefix string
if user != "" {
prefix = user + "."
}
cfg.ProxyName = prefix + proxyName
2020-05-24 11:48:37 +02:00
cfg.ProxyType = consts.TCPProxy
cfg.LocalIP = localIP
2018-04-10 11:46:49 +02:00
cfg.LocalPort = localPort
cfg.RemotePort = remotePort
cfg.UseEncryption = useEncryption
cfg.UseCompression = useCompression
err = cfg.CheckForCli()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
proxyConfs := map[string]config.ProxyConf{
cfg.ProxyName: cfg,
}
err = startService(clientCfg, proxyConfs, nil, "")
2018-04-10 11:46:49 +02:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
}