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