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 ( ...@@ -7,5 +7,5 @@ const (
nat3 = "Port Restricted" nat3 = "Port Restricted"
nat4 = "Symmetric" nat4 = "Symmetric"
Version = "v4.11.11" Version = "v4.11.15"
) )
module nat-detect module nat-detect
go 1.23.2 go 1.22.3
require github.com/ccding/go-stun/stun v0.0.0-20200514191101-4dc67bcdb029 require github.com/ccding/go-stun/stun v0.0.0-20200514191101-4dc67bcdb029
...@@ -2,9 +2,11 @@ package main ...@@ -2,9 +2,11 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"os"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
...@@ -21,6 +23,9 @@ var ( ...@@ -21,6 +23,9 @@ var (
servers []string servers []string
ports []string ports []string
version bool
debug bool
) )
type NATInfo struct { type NATInfo struct {
...@@ -33,6 +38,10 @@ type NATInfo struct { ...@@ -33,6 +38,10 @@ type NATInfo struct {
} }
func init() { func init() {
flag.BoolVar(&version, "v", false, "print version and exit")
flag.BoolVar(&debug, "d", false, "debug")
flag.Parse()
initMapServer() initMapServer()
initSlicePorts() initSlicePorts()
...@@ -41,20 +50,47 @@ func init() { ...@@ -41,20 +50,47 @@ func init() {
natTable[nat2] = 2 natTable[nat2] = 2
natTable[nat3] = 3 natTable[nat3] = 3
natTable[nat4] = 4 natTable[nat4] = 4
err := SetDnsServerEnv(true)
if err != nil {
fmt.Println(err)
return
}
} }
func main() { func main() {
if version {
fmt.Println(Version)
os.Exit(0)
}
c := stun.NewClient() c := stun.NewClient()
c.SetServerAddr("newstun.bkdomain.cn:3478") c.SetServerAddr("newstun.bkdomain.cn:3478")
nat, _, err := c.Discover() nat, _, err := c.Discover()
if err != nil { if err != nil {
fmt.Println("check nat type failed:", err) 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 return
} }
}
}
natType := nat.String() natType := nat.String()
natType = strings.ToLower(natType) natType = strings.ToLower(natType)
if debug {
fmt.Println("natType:", natType)
}
if strings.Contains(natType, "full cone") { if strings.Contains(natType, "full cone") {
NATType = nat1 NATType = nat1
} else if strings.Contains(natType, "symmetric") { } else if strings.Contains(natType, "symmetric") {
...@@ -83,6 +119,7 @@ func main() { ...@@ -83,6 +119,7 @@ func main() {
} }
data, err := json.Marshal(info) data, err := json.Marshal(info)
if err != nil { if err != nil {
fmt.Println(err)
return 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