tcpmux.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  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. "strings"
  19. "github.com/spf13/cobra"
  20. "github.com/fatedier/frp/pkg/config/types"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/config/v1/validation"
  23. "github.com/fatedier/frp/pkg/consts"
  24. )
  25. func init() {
  26. RegisterCommonFlags(tcpMuxCmd)
  27. tcpMuxCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  28. tcpMuxCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  29. tcpMuxCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  30. tcpMuxCmd.PersistentFlags().StringVarP(&customDomains, "custom_domain", "d", "", "custom domain")
  31. tcpMuxCmd.PersistentFlags().StringVarP(&subDomain, "sd", "", "", "sub domain")
  32. tcpMuxCmd.PersistentFlags().StringVarP(&multiplexer, "mux", "", "", "multiplexer")
  33. tcpMuxCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  34. tcpMuxCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  35. tcpMuxCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  36. tcpMuxCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", types.BandwidthLimitModeClient, "bandwidth limit mode")
  37. rootCmd.AddCommand(tcpMuxCmd)
  38. }
  39. var tcpMuxCmd = &cobra.Command{
  40. Use: "tcpmux",
  41. Short: "Run frpc with a single tcpmux proxy",
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. clientCfg, err := parseClientCommonCfgFromCmd()
  44. if err != nil {
  45. fmt.Println(err)
  46. os.Exit(1)
  47. }
  48. cfg := &v1.TCPMuxProxyConfig{}
  49. var prefix string
  50. if user != "" {
  51. prefix = user + "."
  52. }
  53. cfg.Name = prefix + proxyName
  54. cfg.Type = consts.TCPMuxProxy
  55. cfg.LocalIP = localIP
  56. cfg.LocalPort = localPort
  57. cfg.CustomDomains = strings.Split(customDomains, ",")
  58. cfg.SubDomain = subDomain
  59. cfg.Multiplexer = multiplexer
  60. cfg.Transport.UseEncryption = useEncryption
  61. cfg.Transport.UseCompression = useCompression
  62. cfg.Transport.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidthLimit)
  63. if err != nil {
  64. fmt.Println(err)
  65. os.Exit(1)
  66. }
  67. cfg.Transport.BandwidthLimitMode = bandwidthLimitMode
  68. if err := validation.ValidateProxyConfigurerForClient(cfg); err != nil {
  69. fmt.Println(err)
  70. os.Exit(1)
  71. }
  72. err = startService(clientCfg, []v1.ProxyConfigurer{cfg}, nil, "")
  73. if err != nil {
  74. fmt.Println(err)
  75. os.Exit(1)
  76. }
  77. return nil
  78. },
  79. }