2023-03-30 20:28:15 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/pkg/config"
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-03-30 20:28:15 +08:00
|
|
|
"github.com/fatedier/frp/pkg/nathole"
|
|
|
|
)
|
|
|
|
|
2023-04-10 21:45:00 +08:00
|
|
|
var (
|
|
|
|
natHoleSTUNServer string
|
2023-05-28 16:50:43 +08:00
|
|
|
natHoleLocalAddr string
|
2023-04-10 21:45:00 +08:00
|
|
|
)
|
2023-03-30 22:02:00 +08:00
|
|
|
|
2023-03-30 20:28:15 +08:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(natholeCmd)
|
|
|
|
natholeCmd.AddCommand(natholeDiscoveryCmd)
|
2023-03-30 22:02:00 +08:00
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
natholeCmd.PersistentFlags().StringVarP(&natHoleSTUNServer, "nat_hole_stun_server", "", "", "STUN server address for nathole")
|
|
|
|
natholeCmd.PersistentFlags().StringVarP(&natHoleLocalAddr, "nat_hole_local_addr", "l", "", "local address to connect STUN server")
|
2023-03-30 20:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var natholeCmd = &cobra.Command{
|
|
|
|
Use: "nathole",
|
|
|
|
Short: "Actions about nathole",
|
|
|
|
}
|
|
|
|
|
|
|
|
var natholeDiscoveryCmd = &cobra.Command{
|
|
|
|
Use: "discover",
|
2023-05-28 16:50:43 +08:00
|
|
|
Short: "Discover nathole information from stun server",
|
2023-03-30 20:28:15 +08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-04-10 21:45:00 +08:00
|
|
|
// ignore error here, because we can use command line pameters
|
2023-11-16 21:03:36 +08:00
|
|
|
cfg, _, _, _, err := config.LoadClientConfig(cfgFile, strictConfigMode)
|
2023-05-28 16:50:43 +08:00
|
|
|
if err != nil {
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg = &v1.ClientCommonConfig{}
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
2023-03-30 22:02:00 +08:00
|
|
|
if natHoleSTUNServer != "" {
|
|
|
|
cfg.NatHoleSTUNServer = natHoleSTUNServer
|
|
|
|
}
|
2023-03-30 20:28:15 +08:00
|
|
|
|
|
|
|
if err := validateForNatHoleDiscovery(cfg); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
addrs, localAddr, err := nathole.Discover([]string{cfg.NatHoleSTUNServer}, natHoleLocalAddr)
|
2023-03-30 20:28:15 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("discover error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
if len(addrs) < 2 {
|
|
|
|
fmt.Printf("discover error: can not get enough addresses, need 2, got: %v\n", addrs)
|
2023-03-30 21:49:12 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-03-30 20:28:15 +08:00
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
localIPs, _ := nathole.ListLocalIPsForNatHole(10)
|
|
|
|
|
|
|
|
natFeature, err := nathole.ClassifyNATFeature(addrs, localIPs)
|
2023-03-30 20:28:15 +08:00
|
|
|
if err != nil {
|
2023-05-28 16:50:43 +08:00
|
|
|
fmt.Println("classify nat feature error:", err)
|
2023-03-30 20:28:15 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
fmt.Println("STUN server:", cfg.NatHoleSTUNServer)
|
|
|
|
fmt.Println("Your NAT type is:", natFeature.NatType)
|
|
|
|
fmt.Println("Behavior is:", natFeature.Behavior)
|
|
|
|
fmt.Println("External address is:", addrs)
|
|
|
|
fmt.Println("Local address is:", localAddr.String())
|
|
|
|
fmt.Println("Public Network:", natFeature.PublicNetwork)
|
2023-03-30 20:28:15 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func validateForNatHoleDiscovery(cfg *v1.ClientCommonConfig) error {
|
2023-03-30 20:28:15 +08:00
|
|
|
if cfg.NatHoleSTUNServer == "" {
|
|
|
|
return fmt.Errorf("nat_hole_stun_server can not be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|