stcp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2018 fatedier, [email protected]
  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/fatedier/frp/pkg/config"
  19. "github.com/fatedier/frp/pkg/consts"
  20. "github.com/spf13/cobra"
  21. )
  22. func init() {
  23. RegisterCommonFlags(stcpCmd)
  24. stcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  25. stcpCmd.PersistentFlags().StringVarP(&role, "role", "", "server", "role")
  26. stcpCmd.PersistentFlags().StringVarP(&sk, "sk", "", "", "secret key")
  27. stcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name")
  28. stcpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  29. stcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  30. stcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr")
  31. stcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port")
  32. stcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  33. stcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  34. rootCmd.AddCommand(stcpCmd)
  35. }
  36. var stcpCmd = &cobra.Command{
  37. Use: "stcp",
  38. Short: "Run frpc with a single stcp 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. if role == "server" {
  52. cfg := &config.STCPProxyConf{}
  53. cfg.ProxyName = prefix + proxyName
  54. cfg.ProxyType = consts.STCPProxy
  55. cfg.UseEncryption = useEncryption
  56. cfg.UseCompression = useCompression
  57. cfg.Role = role
  58. cfg.Sk = sk
  59. cfg.LocalIP = localIP
  60. cfg.LocalPort = localPort
  61. err = cfg.CheckForCli()
  62. if err != nil {
  63. fmt.Println(err)
  64. os.Exit(1)
  65. }
  66. proxyConfs[cfg.ProxyName] = cfg
  67. } else if role == "visitor" {
  68. cfg := &config.STCPVisitorConf{}
  69. cfg.ProxyName = prefix + proxyName
  70. cfg.ProxyType = consts.STCPProxy
  71. cfg.UseEncryption = useEncryption
  72. cfg.UseCompression = useCompression
  73. cfg.Role = role
  74. cfg.Sk = sk
  75. cfg.ServerName = serverName
  76. cfg.BindAddr = bindAddr
  77. cfg.BindPort = bindPort
  78. err = cfg.Check()
  79. if err != nil {
  80. fmt.Println(err)
  81. os.Exit(1)
  82. }
  83. visitorConfs[cfg.ProxyName] = cfg
  84. } else {
  85. fmt.Println("invalid role")
  86. os.Exit(1)
  87. }
  88. err, _ = startService(clientCfg, proxyConfs, visitorConfs, "")
  89. if err != nil {
  90. fmt.Println(err)
  91. os.Exit(1)
  92. }
  93. return nil
  94. },
  95. }