From 3e0aac799c69dc26527498b1626d7b9ea2ee3136 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Wed, 9 Nov 2022 15:16:18 +0900 Subject: [PATCH] fix: windows localhost dns --- go/cmd/nekoray_core/core_ray.go | 13 +++++++++++++ .../protect_bindinterface_windows.go | 17 +++++++++++++++-- go/cmd/nekoray_core/toolbox_windows.go | 1 + matsuri_commit.txt | 2 +- sys/ExternalProcess.cpp | 1 - 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/go/cmd/nekoray_core/core_ray.go b/go/cmd/nekoray_core/core_ray.go index d1e7c7a4f..32e9f5015 100644 --- a/go/cmd/nekoray_core/core_ray.go +++ b/go/cmd/nekoray_core/core_ray.go @@ -11,15 +11,28 @@ import ( "time" v2rayNet "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/features/dns/localdns" ) var instance *libcore.V2RayInstance +var getNekorayTunIndex = func() int { return 0 } func setupCore() { // TODO del device.IsNekoray = true libcore.SetConfig("", false, true) libcore.InitCore("", "", "", nil, ".", "moe.nekoray.pc:bg", true, 50) + // localdns setup + resolver_def := &net.Resolver{PreferGo: false} + resolver_go := &net.Resolver{PreferGo: true} + localdns.SetLookupFunc(func(network string, host string) ([]net.IP, error) { + // Normal mode use system resolver (go bug) + if getNekorayTunIndex() == 0 { + return resolver_def.LookupIP(context.Background(), network, host) + } + // VPN mode use Go resolver + return resolver_go.LookupIP(context.Background(), network, host) + }) // neko_common.GetProxyHttpClient = func() *http.Client { return getProxyHttpClient(instance) diff --git a/go/cmd/nekoray_core/protect_bindinterface_windows.go b/go/cmd/nekoray_core/protect_bindinterface_windows.go index 83ed48d33..a0c2e9719 100644 --- a/go/cmd/nekoray_core/protect_bindinterface_windows.go +++ b/go/cmd/nekoray_core/protect_bindinterface_windows.go @@ -47,11 +47,24 @@ func init() { } return nil }) + // updateRoutes() iphlpapi.RegisterNotifyRouteChange2(func(callerContext uintptr, row uintptr, notificationType uint32) uintptr { updateRoutes() return 0 - }, true) + }, false) + // + getNekorayTunIndex = func() (index int) { + lock.Lock() + defer lock.Unlock() + for _, intf := range interfaces { + if intf.Name == "nekoray-tun" { + index = intf.Index + return + } + } + return + } } func updateRoutes() { @@ -85,7 +98,7 @@ func getBindInterfaceIndex(address string) uint32 { var nextInterface int for i, intf := range interfaces { - if intf.Name == "nekoray-tun" || intf.Name == "wintun" || intf.Name == "TunMax" { + if intf.Name == "nekoray-tun" { if len(interfaces) > i+1 { nextInterface = interfaces[i+1].Index } diff --git a/go/cmd/nekoray_core/toolbox_windows.go b/go/cmd/nekoray_core/toolbox_windows.go index 86ce74744..a3bc86407 100644 --- a/go/cmd/nekoray_core/toolbox_windows.go +++ b/go/cmd/nekoray_core/toolbox_windows.go @@ -20,6 +20,7 @@ func ToolBox() { for _, route := range routes { log.Println(route) } + log.Println("TUN:", getNekorayTunIndex()) log.Println("Upstream:", getBindInterfaceIndex("8.8.8.8:8888")) } } diff --git a/matsuri_commit.txt b/matsuri_commit.txt index 82e76aebf..dd2b8eea9 100644 --- a/matsuri_commit.txt +++ b/matsuri_commit.txt @@ -1 +1 @@ -ac454d7b46306ea0c4eaedd95ac7ea54fd1afa73 +8b1a40059eee4eb7e3db559185aec224324f322c diff --git a/sys/ExternalProcess.cpp b/sys/ExternalProcess.cpp index a365ce389..7e7eda95d 100644 --- a/sys/ExternalProcess.cpp +++ b/sys/ExternalProcess.cpp @@ -118,7 +118,6 @@ namespace NekoRay::sys { v2ray_asset_dir = QFileInfo(v2ray_asset_dir).absolutePath(); env << "V2RAY_LOCATION_ASSET=" + v2ray_asset_dir; } - env << "GODEBUG=netdns=go"; ExternalProcess::Start(); write((dataStore->core_token + "\n").toUtf8()); }