tcp.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sub
  15. import (
  16. "fmt"
  17. "os"
  18. "github.com/spf13/cobra"
  19. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/consts"
  21. )
  22. func init() {
  23. RegisterCommonFlags(tcpCmd)
  24. tcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  25. tcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  26. tcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  27. tcpCmd.PersistentFlags().IntVarP(&remotePort, "remote_port", "r", 0, "remote port")
  28. tcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  29. tcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  30. rootCmd.AddCommand(tcpCmd)
  31. }
  32. var tcpCmd = &cobra.Command{
  33. Use: "tcp",
  34. Short: "Run frpc with a single tcp proxy",
  35. RunE: func(cmd *cobra.Command, args []string) error {
  36. clientCfg, err := parseClientCommonCfgFromCmd()
  37. if err != nil {
  38. fmt.Println(err)
  39. os.Exit(1)
  40. }
  41. cfg := &config.TCPProxyConf{}
  42. var prefix string
  43. if user != "" {
  44. prefix = user + "."
  45. }
  46. cfg.ProxyName = prefix + proxyName
  47. cfg.ProxyType = consts.TCPProxy
  48. cfg.LocalIP = localIP
  49. cfg.LocalPort = localPort
  50. cfg.RemotePort = remotePort
  51. cfg.UseEncryption = useEncryption
  52. cfg.UseCompression = useCompression
  53. err = cfg.CheckForCli()
  54. if err != nil {
  55. fmt.Println(err)
  56. os.Exit(1)
  57. }
  58. proxyConfs := map[string]config.ProxyConf{
  59. cfg.ProxyName: cfg,
  60. }
  61. err, _ = startService(clientCfg, proxyConfs, nil, "")
  62. if err != nil {
  63. fmt.Println(err)
  64. os.Exit(1)
  65. }
  66. return nil
  67. },
  68. }