xtcp.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(xtcpCmd)
  24. xtcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  25. xtcpCmd.PersistentFlags().StringVarP(&role, "role", "", "server", "role")
  26. xtcpCmd.PersistentFlags().StringVarP(&sk, "sk", "", "", "secret key")
  27. xtcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name")
  28. xtcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  29. xtcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  30. xtcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr")
  31. xtcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port")
  32. xtcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  33. xtcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  34. xtcpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  35. xtcpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", config.BandwidthLimitModeClient, "bandwidth limit mode")
  36. rootCmd.AddCommand(xtcpCmd)
  37. }
  38. var xtcpCmd = &cobra.Command{
  39. Use: "xtcp",
  40. Short: "Run frpc with a single xtcp proxy",
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. clientCfg, err := parseClientCommonCfgFromCmd()
  43. if err != nil {
  44. fmt.Println(err)
  45. os.Exit(1)
  46. }
  47. proxyConfs := make(map[string]config.ProxyConf)
  48. visitorConfs := make(map[string]config.VisitorConf)
  49. var prefix string
  50. if user != "" {
  51. prefix = user + "."
  52. }
  53. switch role {
  54. case "server":
  55. cfg := &config.XTCPProxyConf{}
  56. cfg.ProxyName = prefix + proxyName
  57. cfg.ProxyType = consts.XTCPProxy
  58. cfg.UseEncryption = useEncryption
  59. cfg.UseCompression = useCompression
  60. cfg.Role = role
  61. cfg.Sk = sk
  62. cfg.LocalIP = localIP
  63. cfg.LocalPort = localPort
  64. cfg.BandwidthLimit, err = config.NewBandwidthQuantity(bandwidthLimit)
  65. if err != nil {
  66. fmt.Println(err)
  67. os.Exit(1)
  68. }
  69. cfg.BandwidthLimitMode = bandwidthLimitMode
  70. err = cfg.ValidateForClient()
  71. if err != nil {
  72. fmt.Println(err)
  73. os.Exit(1)
  74. }
  75. proxyConfs[cfg.ProxyName] = cfg
  76. case "visitor":
  77. cfg := &config.XTCPVisitorConf{}
  78. cfg.ProxyName = prefix + proxyName
  79. cfg.ProxyType = consts.XTCPProxy
  80. cfg.UseEncryption = useEncryption
  81. cfg.UseCompression = useCompression
  82. cfg.Role = role
  83. cfg.Sk = sk
  84. cfg.ServerName = serverName
  85. cfg.BindAddr = bindAddr
  86. cfg.BindPort = bindPort
  87. err = cfg.Validate()
  88. if err != nil {
  89. fmt.Println(err)
  90. os.Exit(1)
  91. }
  92. visitorConfs[cfg.ProxyName] = cfg
  93. default:
  94. fmt.Println("invalid role")
  95. os.Exit(1)
  96. }
  97. err = startService(clientCfg, proxyConfs, visitorConfs, "")
  98. if err != nil {
  99. fmt.Println(err)
  100. os.Exit(1)
  101. }
  102. return nil
  103. },
  104. }