xtcp.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/models/config"
  20. "github.com/fatedier/frp/models/consts"
  21. )
  22. func init() {
  23. xtcpCmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
  24. xtcpCmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
  25. xtcpCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp or websocket")
  26. xtcpCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  27. xtcpCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  28. xtcpCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
  29. xtcpCmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
  30. xtcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  31. xtcpCmd.PersistentFlags().StringVarP(&role, "role", "", "server", "role")
  32. xtcpCmd.PersistentFlags().StringVarP(&sk, "sk", "", "", "secret key")
  33. xtcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name")
  34. xtcpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip")
  35. xtcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  36. xtcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr")
  37. xtcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port")
  38. xtcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  39. xtcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  40. rootCmd.AddCommand(xtcpCmd)
  41. }
  42. var xtcpCmd = &cobra.Command{
  43. Use: "xtcp",
  44. Short: "Run frpc with a single xtcp proxy",
  45. RunE: func(cmd *cobra.Command, args []string) error {
  46. err := parseClientCommonCfg(CfgFileTypeCmd, "")
  47. if err != nil {
  48. fmt.Println(err)
  49. os.Exit(1)
  50. }
  51. proxyConfs := make(map[string]config.ProxyConf)
  52. visitorConfs := make(map[string]config.VisitorConf)
  53. var prefix string
  54. if user != "" {
  55. prefix = user + "."
  56. }
  57. if role == "server" {
  58. cfg := &config.XtcpProxyConf{}
  59. cfg.ProxyName = prefix + proxyName
  60. cfg.ProxyType = consts.StcpProxy
  61. cfg.UseEncryption = useEncryption
  62. cfg.UseCompression = useCompression
  63. cfg.Role = role
  64. cfg.Sk = sk
  65. cfg.LocalIp = localIp
  66. cfg.LocalPort = localPort
  67. err = cfg.CheckForCli()
  68. if err != nil {
  69. fmt.Println(err)
  70. os.Exit(1)
  71. }
  72. proxyConfs[cfg.ProxyName] = cfg
  73. } else if role == "visitor" {
  74. cfg := &config.XtcpVisitorConf{}
  75. cfg.ProxyName = prefix + proxyName
  76. cfg.ProxyType = consts.StcpProxy
  77. cfg.UseEncryption = useEncryption
  78. cfg.UseCompression = useCompression
  79. cfg.Role = role
  80. cfg.Sk = sk
  81. cfg.ServerName = serverName
  82. cfg.BindAddr = bindAddr
  83. cfg.BindPort = bindPort
  84. err = cfg.Check()
  85. if err != nil {
  86. fmt.Println(err)
  87. os.Exit(1)
  88. }
  89. visitorConfs[cfg.ProxyName] = cfg
  90. } else {
  91. fmt.Println("invalid role")
  92. os.Exit(1)
  93. }
  94. err = startService(proxyConfs, visitorConfs)
  95. if err != nil {
  96. fmt.Println(err)
  97. os.Exit(1)
  98. }
  99. return nil
  100. },
  101. }