1
0

tcp.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/types"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. "github.com/fatedier/frp/pkg/config/v1/validation"
  22. "github.com/fatedier/frp/pkg/consts"
  23. )
  24. func init() {
  25. RegisterCommonFlags(tcpCmd)
  26. tcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  27. tcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  28. tcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  29. tcpCmd.PersistentFlags().IntVarP(&remotePort, "remote_port", "r", 0, "remote port")
  30. tcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  31. tcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  32. tcpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  33. tcpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", types.BandwidthLimitModeClient, "bandwidth limit mode")
  34. rootCmd.AddCommand(tcpCmd)
  35. }
  36. var tcpCmd = &cobra.Command{
  37. Use: "tcp",
  38. Short: "Run frpc with a single tcp proxy",
  39. RunE: func(cmd *cobra.Command, args []string) error {
  40. clientCfg, err := parseClientCommonCfgFromCmd()
  41. if err != nil {
  42. fmt.Println(err)
  43. os.Exit(1)
  44. }
  45. cfg := &v1.TCPProxyConfig{}
  46. var prefix string
  47. if user != "" {
  48. prefix = user + "."
  49. }
  50. cfg.Name = prefix + proxyName
  51. cfg.Type = consts.TCPProxy
  52. cfg.LocalIP = localIP
  53. cfg.LocalPort = localPort
  54. cfg.RemotePort = remotePort
  55. cfg.Transport.UseEncryption = useEncryption
  56. cfg.Transport.UseCompression = useCompression
  57. cfg.Transport.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidthLimit)
  58. if err != nil {
  59. fmt.Println(err)
  60. os.Exit(1)
  61. }
  62. cfg.Transport.BandwidthLimitMode = bandwidthLimitMode
  63. if err := validation.ValidateProxyConfigurerForClient(cfg); err != nil {
  64. fmt.Println(err)
  65. os.Exit(1)
  66. }
  67. err = startService(clientCfg, []v1.ProxyConfigurer{cfg}, nil, "")
  68. if err != nil {
  69. fmt.Println(err)
  70. os.Exit(1)
  71. }
  72. return nil
  73. },
  74. }