xtcp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. rootCmd.AddCommand(xtcpCmd)
  35. }
  36. var xtcpCmd = &cobra.Command{
  37. Use: "xtcp",
  38. Short: "Run frpc with a single xtcp 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. proxyConfs := make(map[string]config.ProxyConf)
  46. visitorConfs := make(map[string]config.VisitorConf)
  47. var prefix string
  48. if user != "" {
  49. prefix = user + "."
  50. }
  51. switch role {
  52. case "server":
  53. cfg := &config.XTCPProxyConf{}
  54. cfg.ProxyName = prefix + proxyName
  55. cfg.ProxyType = consts.XTCPProxy
  56. cfg.UseEncryption = useEncryption
  57. cfg.UseCompression = useCompression
  58. cfg.Role = role
  59. cfg.Sk = sk
  60. cfg.LocalIP = localIP
  61. cfg.LocalPort = localPort
  62. err = cfg.CheckForCli()
  63. if err != nil {
  64. fmt.Println(err)
  65. os.Exit(1)
  66. }
  67. proxyConfs[cfg.ProxyName] = cfg
  68. case "visitor":
  69. cfg := &config.XTCPVisitorConf{}
  70. cfg.ProxyName = prefix + proxyName
  71. cfg.ProxyType = consts.XTCPProxy
  72. cfg.UseEncryption = useEncryption
  73. cfg.UseCompression = useCompression
  74. cfg.Role = role
  75. cfg.Sk = sk
  76. cfg.ServerName = serverName
  77. cfg.BindAddr = bindAddr
  78. cfg.BindPort = bindPort
  79. err = cfg.Check()
  80. if err != nil {
  81. fmt.Println(err)
  82. os.Exit(1)
  83. }
  84. visitorConfs[cfg.ProxyName] = cfg
  85. default:
  86. fmt.Println("invalid role")
  87. os.Exit(1)
  88. }
  89. err = startService(clientCfg, proxyConfs, visitorConfs, "")
  90. if err != nil {
  91. fmt.Println(err)
  92. os.Exit(1)
  93. }
  94. return nil
  95. },
  96. }