udp.go 2.7 KB

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