tcpmux.go 2.9 KB

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