nathole.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "os"
  18. "github.com/spf13/cobra"
  19. "github.com/fatedier/frp/pkg/config"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. "github.com/fatedier/frp/pkg/nathole"
  22. )
  23. var (
  24. natHoleSTUNServer string
  25. natHoleLocalAddr string
  26. )
  27. func init() {
  28. rootCmd.AddCommand(natholeCmd)
  29. natholeCmd.AddCommand(natholeDiscoveryCmd)
  30. natholeCmd.PersistentFlags().StringVarP(&natHoleSTUNServer, "nat_hole_stun_server", "", "", "STUN server address for nathole")
  31. natholeCmd.PersistentFlags().StringVarP(&natHoleLocalAddr, "nat_hole_local_addr", "l", "", "local address to connect STUN server")
  32. }
  33. var natholeCmd = &cobra.Command{
  34. Use: "nathole",
  35. Short: "Actions about nathole",
  36. }
  37. var natholeDiscoveryCmd = &cobra.Command{
  38. Use: "discover",
  39. Short: "Discover nathole information from stun server",
  40. RunE: func(cmd *cobra.Command, args []string) error {
  41. // ignore error here, because we can use command line pameters
  42. cfg, _, _, _, err := config.LoadClientConfig(cfgFile, strictConfigMode)
  43. if err != nil {
  44. cfg = &v1.ClientCommonConfig{}
  45. cfg.Complete()
  46. }
  47. if natHoleSTUNServer != "" {
  48. cfg.NatHoleSTUNServer = natHoleSTUNServer
  49. }
  50. if err := validateForNatHoleDiscovery(cfg); err != nil {
  51. fmt.Println(err)
  52. os.Exit(1)
  53. }
  54. addrs, localAddr, err := nathole.Discover([]string{cfg.NatHoleSTUNServer}, natHoleLocalAddr)
  55. if err != nil {
  56. fmt.Println("discover error:", err)
  57. os.Exit(1)
  58. }
  59. if len(addrs) < 2 {
  60. fmt.Printf("discover error: can not get enough addresses, need 2, got: %v\n", addrs)
  61. os.Exit(1)
  62. }
  63. localIPs, _ := nathole.ListLocalIPsForNatHole(10)
  64. natFeature, err := nathole.ClassifyNATFeature(addrs, localIPs)
  65. if err != nil {
  66. fmt.Println("classify nat feature error:", err)
  67. os.Exit(1)
  68. }
  69. fmt.Println("STUN server:", cfg.NatHoleSTUNServer)
  70. fmt.Println("Your NAT type is:", natFeature.NatType)
  71. fmt.Println("Behavior is:", natFeature.Behavior)
  72. fmt.Println("External address is:", addrs)
  73. fmt.Println("Local address is:", localAddr.String())
  74. fmt.Println("Public Network:", natFeature.PublicNetwork)
  75. return nil
  76. },
  77. }
  78. func validateForNatHoleDiscovery(cfg *v1.ClientCommonConfig) error {
  79. if cfg.NatHoleSTUNServer == "" {
  80. return fmt.Errorf("nat_hole_stun_server can not be empty")
  81. }
  82. return nil
  83. }