nathole.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2023 The frp Authors
  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. "net"
  18. "os"
  19. "strconv"
  20. "github.com/spf13/cobra"
  21. "github.com/fatedier/frp/pkg/config"
  22. "github.com/fatedier/frp/pkg/nathole"
  23. )
  24. var (
  25. natHoleSTUNServer string
  26. serverUDPPort int
  27. )
  28. func init() {
  29. RegisterCommonFlags(natholeCmd)
  30. rootCmd.AddCommand(natholeCmd)
  31. natholeCmd.AddCommand(natholeDiscoveryCmd)
  32. natholeCmd.PersistentFlags().StringVarP(&natHoleSTUNServer, "nat_hole_stun_server", "", "stun.easyvoip.com:3478", "STUN server address for nathole")
  33. natholeCmd.PersistentFlags().IntVarP(&serverUDPPort, "server_udp_port", "", 0, "UDP port of frps for nathole")
  34. }
  35. var natholeCmd = &cobra.Command{
  36. Use: "nathole",
  37. Short: "Actions about nathole",
  38. }
  39. var natholeDiscoveryCmd = &cobra.Command{
  40. Use: "discover",
  41. Short: "Discover nathole information by frps and stun server",
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. // ignore error here, because we can use command line pameters
  44. cfg, _, _, _ := config.ParseClientConfig(cfgFile)
  45. if natHoleSTUNServer != "" {
  46. cfg.NatHoleSTUNServer = natHoleSTUNServer
  47. }
  48. if serverUDPPort != 0 {
  49. cfg.ServerUDPPort = serverUDPPort
  50. }
  51. if err := validateForNatHoleDiscovery(cfg); err != nil {
  52. fmt.Println(err)
  53. os.Exit(1)
  54. }
  55. serverAddr := ""
  56. if cfg.ServerUDPPort != 0 {
  57. serverAddr = net.JoinHostPort(cfg.ServerAddr, strconv.Itoa(cfg.ServerUDPPort))
  58. }
  59. addresses, err := nathole.Discover(
  60. serverAddr,
  61. []string{cfg.NatHoleSTUNServer},
  62. []byte(cfg.Token),
  63. )
  64. if err != nil {
  65. fmt.Println("discover error:", err)
  66. os.Exit(1)
  67. }
  68. if len(addresses) < 2 {
  69. fmt.Printf("discover error: can not get enough addresses, need 2, got: %v\n", addresses)
  70. os.Exit(1)
  71. }
  72. natType, behavior, err := nathole.ClassifyNATType(addresses)
  73. if err != nil {
  74. fmt.Println("classify nat type error:", err)
  75. os.Exit(1)
  76. }
  77. fmt.Println("Your NAT type is:", natType)
  78. fmt.Println("Behavior is:", behavior)
  79. fmt.Println("External address is:", addresses)
  80. return nil
  81. },
  82. }
  83. func validateForNatHoleDiscovery(cfg config.ClientCommonConf) error {
  84. if cfg.NatHoleSTUNServer == "" {
  85. return fmt.Errorf("nat_hole_stun_server can not be empty")
  86. }
  87. return nil
  88. }