nathole.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. RegisterCommonFlags(natholeCmd)
  29. rootCmd.AddCommand(natholeCmd)
  30. natholeCmd.AddCommand(natholeDiscoveryCmd)
  31. natholeCmd.PersistentFlags().StringVarP(&natHoleSTUNServer, "nat_hole_stun_server", "", "", "STUN server address for nathole")
  32. natholeCmd.PersistentFlags().StringVarP(&natHoleLocalAddr, "nat_hole_local_addr", "l", "", "local address to connect STUN server")
  33. }
  34. var natholeCmd = &cobra.Command{
  35. Use: "nathole",
  36. Short: "Actions about nathole",
  37. }
  38. var natholeDiscoveryCmd = &cobra.Command{
  39. Use: "discover",
  40. Short: "Discover nathole information from stun server",
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. // ignore error here, because we can use command line pameters
  43. cfg, _, _, _, err := config.LoadClientConfig(cfgFile)
  44. if err != nil {
  45. cfg = &v1.ClientCommonConfig{}
  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. }