udp.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/pkg/config"
  20. "github.com/fatedier/frp/pkg/consts"
  21. )
  22. func init() {
  23. RegisterCommonFlags(udpCmd)
  24. udpCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
  25. udpCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
  26. udpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
  27. udpCmd.PersistentFlags().IntVarP(&remotePort, "remote_port", "r", 0, "remote port")
  28. udpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
  29. udpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")
  30. udpCmd.PersistentFlags().StringVarP(&bandwidthLimit, "bandwidth_limit", "", "", "bandwidth limit")
  31. udpCmd.PersistentFlags().StringVarP(&bandwidthLimitMode, "bandwidth_limit_mode", "", config.BandwidthLimitModeClient, "bandwidth limit mode")
  32. rootCmd.AddCommand(udpCmd)
  33. }
  34. var udpCmd = &cobra.Command{
  35. Use: "udp",
  36. Short: "Run frpc with a single udp proxy",
  37. RunE: func(cmd *cobra.Command, args []string) error {
  38. clientCfg, err := parseClientCommonCfgFromCmd()
  39. if err != nil {
  40. fmt.Println(err)
  41. os.Exit(1)
  42. }
  43. cfg := &config.UDPProxyConf{}
  44. var prefix string
  45. if user != "" {
  46. prefix = user + "."
  47. }
  48. cfg.ProxyName = prefix + proxyName
  49. cfg.ProxyType = consts.UDPProxy
  50. cfg.LocalIP = localIP
  51. cfg.LocalPort = localPort
  52. cfg.RemotePort = remotePort
  53. cfg.UseEncryption = useEncryption
  54. cfg.UseCompression = useCompression
  55. cfg.BandwidthLimit, err = config.NewBandwidthQuantity(bandwidthLimit)
  56. if err != nil {
  57. fmt.Println(err)
  58. os.Exit(1)
  59. }
  60. cfg.BandwidthLimitMode = bandwidthLimitMode
  61. err = cfg.CheckForCli()
  62. if err != nil {
  63. fmt.Println(err)
  64. os.Exit(1)
  65. }
  66. proxyConfs := map[string]config.ProxyConf{
  67. cfg.ProxyName: cfg,
  68. }
  69. err = startService(clientCfg, proxyConfs, nil, "")
  70. if err != nil {
  71. fmt.Println(err)
  72. os.Exit(1)
  73. }
  74. return nil
  75. },
  76. }