Commit 83c66775 authored by Lei Li's avatar Lei Li Committed by Lei Li
Browse files

feat: 设置dot环境变量

parent 6b8155cd
package main
import (
"os"
)
func SetDnsServerEnv(isDot bool) error {
if isDot {
err := os.Setenv("DOT_NAMESERVER", "0.0.0.0:5300")
if err != nil {
return err
}
} else {
err := os.Setenv("DOT_NAMESERVER", "223.5.5.5:53")
if err != nil {
return err
}
}
return nil
}
......@@ -7,5 +7,5 @@ const (
nat3 = "Port Restricted"
nat4 = "Symmetric"
Version = "v4.11.11"
Version = "v4.11.15"
)
module nat-detect
go 1.23.2
go 1.22.3
require github.com/ccding/go-stun/stun v0.0.0-20200514191101-4dc67bcdb029
......@@ -2,9 +2,11 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"net"
"os"
"os/exec"
"strconv"
"strings"
......@@ -21,6 +23,9 @@ var (
servers []string
ports []string
version bool
debug bool
)
type NATInfo struct {
......@@ -33,6 +38,10 @@ type NATInfo struct {
}
func init() {
flag.BoolVar(&version, "v", false, "print version and exit")
flag.BoolVar(&debug, "d", false, "debug")
flag.Parse()
initMapServer()
initSlicePorts()
......@@ -41,20 +50,47 @@ func init() {
natTable[nat2] = 2
natTable[nat3] = 3
natTable[nat4] = 4
err := SetDnsServerEnv(true)
if err != nil {
fmt.Println(err)
return
}
}
func main() {
if version {
fmt.Println(Version)
os.Exit(0)
}
c := stun.NewClient()
c.SetServerAddr("newstun.bkdomain.cn:3478")
nat, _, err := c.Discover()
if err != nil {
fmt.Println("check nat type failed:", err)
return
if debug {
fmt.Println("fail to discover nat[5300]:", err)
}
if strings.Contains(err.Error(), "dial tcp: lookup") && strings.Contains(err.Error(), "connection refused") {
err = SetDnsServerEnv(false)
if err != nil {
fmt.Println(err)
return
}
nat, _, err = c.Discover()
if err != nil {
fmt.Println("check nat type failed[53]:", err)
return
}
}
}
natType := nat.String()
natType = strings.ToLower(natType)
if debug {
fmt.Println("natType:", natType)
}
if strings.Contains(natType, "full cone") {
NATType = nat1
} else if strings.Contains(natType, "symmetric") {
......@@ -83,6 +119,7 @@ func main() {
}
data, err := json.Marshal(info)
if err != nil {
fmt.Println(err)
return
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment