stcp.go 3.8 KB

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