tcp.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. tcpCmd.PersistentFlags().StringVarP(&serverAddr, "server_addr", "s", "127.0.0.1:7000", "frp server's address")
  24. tcpCmd.PersistentFlags().StringVarP(&user, "user", "u", "", "user")
  25. tcpCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "tcp", "tcp or kcp")
  26. tcpCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
  27. tcpCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
  28. tcpCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "console or file path")
  29. tcpCmd.PersistentFlags().IntVarP(&logMaxDays, "log_max_days", "", 3, "log file reversed days")
  30. tcpCmd.PersistentFlags().BoolVarP(&disableLogColor, "disable_log_color", "", false, "disable log color in console")
  31. tcpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  32. tcpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip")
  33. tcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  34. tcpCmd.PersistentFlags().IntVarP(&remotePort, "remote_port", "r", 0, "remote port")
  35. tcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  36. tcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  37. rootCmd.AddCommand(tcpCmd)
  38. }
  39. var tcpCmd = &cobra.Command{
  40. Use: "tcp",
  41. Short: "Run frpc with a single tcp proxy",
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. err := parseClientCommonCfg(CfgFileTypeCmd, "")
  44. if err != nil {
  45. fmt.Println(err)
  46. os.Exit(1)
  47. }
  48. cfg := &config.TcpProxyConf{}
  49. var prefix string
  50. if user != "" {
  51. prefix = user + "."
  52. }
  53. cfg.ProxyName = prefix + proxyName
  54. cfg.ProxyType = consts.TcpProxy
  55. cfg.LocalIp = localIp
  56. cfg.LocalPort = localPort
  57. cfg.RemotePort = remotePort
  58. cfg.UseEncryption = useEncryption
  59. cfg.UseCompression = useCompression
  60. err = cfg.CheckForCli()
  61. if err != nil {
  62. fmt.Println(err)
  63. os.Exit(1)
  64. }
  65. proxyConfs := map[string]config.ProxyConf{
  66. cfg.ProxyName: cfg,
  67. }
  68. err = startService(proxyConfs, nil)
  69. if err != nil {
  70. fmt.Println(err)
  71. os.Exit(1)
  72. }
  73. return nil
  74. },
  75. }