From 87161e053899cafa8b99a5caa956551ab935f05b Mon Sep 17 00:00:00 2001 From: Ledorub Date: Wed, 21 Aug 2024 11:19:59 +0300 Subject: [PATCH] fix: IPv6 support (#177) * Fix can't get network w/o IPv4 addr. * Fix broken URL when an IPv6 addr is provided to DoH client. --- cmd/spoof-dpi/main.go | 6 +++--- dns/resolver/doh.go | 6 +++++- util/os.go | 30 +++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cmd/spoof-dpi/main.go b/cmd/spoof-dpi/main.go index 7f1cd0e..82bb290 100644 --- a/cmd/spoof-dpi/main.go +++ b/cmd/spoof-dpi/main.go @@ -13,14 +13,14 @@ import ( ) func main() { - args := util.ParseArgs() + args := util.ParseArgs() if *args.Version { version.PrintVersion() os.Exit(0) } config := util.GetConfig() - config.Load(args) + config.Load(args) pxy := proxy.New(config) if *config.Debug { @@ -41,7 +41,7 @@ func main() { if *config.SystemProxy { if err := util.SetOsProxy(*config.Port); err != nil { - log.Fatal("error while changing proxy settings") + log.Fatalf("error while changing proxy settings: %s", err) } } diff --git a/dns/resolver/doh.go b/dns/resolver/doh.go index c9350a5..79e59dc 100644 --- a/dns/resolver/doh.go +++ b/dns/resolver/doh.go @@ -33,7 +33,11 @@ func NewDOHResolver(host string) *DOHResolver { }, } - host = regexp.MustCompile(`^https:\/\/|\/dns-query$`).ReplaceAllString(host, "") + host = regexp.MustCompile(`^https://|/dns-query$`).ReplaceAllString(host, "") + if ip := net.ParseIP(host); ip != nil && ip.To4() == nil { + host = fmt.Sprintf("[%s]", ip) + } + return &DOHResolver{ upstream: "https://" + host + "/dns-query", client: c, diff --git a/util/os.go b/util/os.go index 9c402dc..4c2ecae 100644 --- a/util/os.go +++ b/util/os.go @@ -1,29 +1,35 @@ package util import ( + "errors" "fmt" "os/exec" "runtime" "strings" ) +const getDefaultNetworkCMD = "networksetup -listnetworkserviceorder | grep" + + " `(route -n get default | grep 'interface' || route -n get -inet6 default | grep 'interface') | cut -d ':' -f2`" + + " -B 1 | head -n 1 | cut -d ' ' -f 2-" + func SetOsProxy(port int) error { if runtime.GOOS != "darwin" { return nil } - network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() - + network, err := getDefaultNetwork() if err != nil { return err } - _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() + args := fmt.Sprintf("'%s' 127.0.0.1 %d", network, port) + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+args).Output() if err != nil { return err } - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output() + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+args).Output() if err != nil { return err } @@ -36,20 +42,30 @@ func UnsetOsProxy() error { return nil } - network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f 2-").Output() + network, err := getDefaultNetwork() if err != nil { return err } - _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() + _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+network+"'"+" off").Output() if err != nil { return err } - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output() + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+network+"'"+" off").Output() if err != nil { return err } return nil } + +func getDefaultNetwork() (string, error) { + network, err := exec.Command("sh", "-c", getDefaultNetworkCMD).Output() + if err != nil { + return "", err + } else if len(network) == 0 { + return "", errors.New("no available networks") + } + return strings.TrimSpace(string(network)), nil +}