tcpmux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. rootCmd.AddCommand(tcpMuxCmd)
  34. }
  35. var tcpMuxCmd = &cobra.Command{
  36. Use: "tcpmux",
  37. Short: "Run frpc with a single tcpmux proxy",
  38. RunE: func(cmd *cobra.Command, args []string) error {
  39. clientCfg, err := parseClientCommonCfgFromCmd()
  40. if err != nil {
  41. fmt.Println(err)
  42. os.Exit(1)
  43. }
  44. cfg := &config.TCPMuxProxyConf{}
  45. var prefix string
  46. if user != "" {
  47. prefix = user + "."
  48. }
  49. cfg.ProxyName = prefix + proxyName
  50. cfg.ProxyType = consts.TCPMuxProxy
  51. cfg.LocalIP = localIP
  52. cfg.LocalPort = localPort
  53. cfg.CustomDomains = strings.Split(customDomains, ",")
  54. cfg.SubDomain = subDomain
  55. cfg.Multiplexer = multiplexer
  56. cfg.UseEncryption = useEncryption
  57. cfg.UseCompression = useCompression
  58. err = cfg.CheckForCli()
  59. if err != nil {
  60. fmt.Println(err)
  61. os.Exit(1)
  62. }
  63. proxyConfs := map[string]config.ProxyConf{
  64. cfg.ProxyName: cfg,
  65. }
  66. err, _ = startService(clientCfg, proxyConfs, nil, "")
  67. if err != nil {
  68. fmt.Println(err)
  69. os.Exit(1)
  70. }
  71. return nil
  72. },
  73. }