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.
This commit is contained in:
Ledorub 2024-08-21 11:19:59 +03:00 committed by GitHub
parent af0af82213
commit 87161e0538
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 11 deletions

View File

@ -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)
}
}

View File

@ -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,

View File

@ -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
}