mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-08 14:13:22 +00:00
TUN inbound: Better "autoOutboundsInterface": "auto" (#6035)
https://github.com/XTLS/Xray-core/pull/6035#issuecomment-4336755860 Fixes https://github.com/XTLS/Xray-core/issues/6030
This commit is contained in:
@@ -3,6 +3,8 @@ package tun
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
@@ -45,26 +47,53 @@ func (updater *InterfaceUpdater) Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var got *net.Interface
|
var got *net.Interface
|
||||||
for _, iface := range interfaces {
|
if updater.fixedName != "" {
|
||||||
if iface.Index == updater.tunIndex {
|
for _, iface := range interfaces {
|
||||||
continue
|
if iface.Index == updater.tunIndex {
|
||||||
}
|
continue
|
||||||
if updater.fixedName != "" {
|
}
|
||||||
if iface.Name == updater.fixedName {
|
if iface.Name == updater.fixedName {
|
||||||
got = &iface
|
got = &iface
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
addrs, err := iface.Addrs()
|
} else {
|
||||||
if err != nil {
|
var ifs []struct {
|
||||||
|
index int
|
||||||
|
score int
|
||||||
|
}
|
||||||
|
for i, iface := range interfaces {
|
||||||
|
if iface.Index == updater.tunIndex {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (iface.Flags&net.FlagUp != 0) &&
|
if strings.Contains(iface.Name, "vEthernet") {
|
||||||
(iface.Flags&net.FlagLoopback == 0) &&
|
continue
|
||||||
len(addrs) > 0 {
|
|
||||||
got = &iface
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
if iface.Flags&net.FlagUp == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if iface.Flags&net.FlagLoopback != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil || len(addrs) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ifs = append(ifs, struct {
|
||||||
|
index int
|
||||||
|
score int
|
||||||
|
}{i, score(&iface, addrs)})
|
||||||
|
}
|
||||||
|
sort.Slice(ifs, func(i, j int) bool {
|
||||||
|
if ifs[i].score != ifs[j].score {
|
||||||
|
return ifs[i].score > ifs[j].score
|
||||||
|
}
|
||||||
|
|
||||||
|
return interfaces[ifs[i].index].Name < interfaces[ifs[j].index].Name
|
||||||
|
})
|
||||||
|
if len(ifs) > 0 {
|
||||||
|
iface := interfaces[ifs[0].index]
|
||||||
|
got = &iface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,3 +105,21 @@ func (updater *InterfaceUpdater) Update() {
|
|||||||
updater.iface = got
|
updater.iface = got
|
||||||
errors.LogInfo(context.Background(), "[tun] update interface ", got.Name, " ", got.Index)
|
errors.LogInfo(context.Background(), "[tun] update interface ", got.Name, " ", got.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func score(iface *net.Interface, addrs []net.Addr) int {
|
||||||
|
score := 0
|
||||||
|
|
||||||
|
name := strings.ToLower(iface.Name)
|
||||||
|
if strings.Contains(name, "wlan") || strings.Contains(name, "wi-fi") {
|
||||||
|
score += 2
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if strings.HasPrefix(addr.String(), "192.168.") {
|
||||||
|
score += 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return score
|
||||||
|
}
|
||||||
|
|||||||
@@ -177,9 +177,6 @@ func (t *WindowsTun) Start() error {
|
|||||||
|
|
||||||
if updater != nil {
|
if updater != nil {
|
||||||
t.changeCallback, err = winipcfg.RegisterInterfaceChangeCallback(func(notificationType winipcfg.MibNotificationType, iface *winipcfg.MibIPInterfaceRow) {
|
t.changeCallback, err = winipcfg.RegisterInterfaceChangeCallback(func(notificationType winipcfg.MibNotificationType, iface *winipcfg.MibIPInterfaceRow) {
|
||||||
if notificationType != winipcfg.MibDeleteInstance {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
updater.Update()
|
updater.Update()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user