mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-26 11:31:53 +00:00
As requested in https://github.com/XTLS/Xray-core/pull/7460#issuecomment-7131171930: proxy/tun goes from 36 files to 29. No behaviour change: the merged code is moved as it is. Merged: - guid.go into config.go - tun_windows_outbound.go into tun_windows.go - tun_windows_wfp_test.go and guid_windows_test.go into tun_windows_test.go - tun_freebsd_dns_test.go into tun_freebsd_test.go - tun_linux_dns_test.go and tun_linux_dns_route_test.go into tun_linux_test.go Renamed wfp_leak.go and wfp_leak_test.go to wfp_block.go and wfp_block_test.go. Removed: - guid_test.go: TestTunConfigGUID in infra/conf covers what ParseGUID accepts, and TestAdapterGUID the bytes it returns. The one case they lacked, a hyphen out of place, is now in TestTunConfigGUID. - TestLeakCovers: covers is only ever asked about port 53, which TestExpandWfpBlockLeak checks through BlocksSystemDNS. - udp_queue_bench_test.go and BenchmarkUDPNewSession: benchmarks, which CI does not run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package tun
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
)
|
|
|
|
type InterfaceUpdater struct {
|
|
sync.Mutex
|
|
|
|
tunIndex int
|
|
fixedName string
|
|
iface *net.Interface
|
|
}
|
|
|
|
var updater *InterfaceUpdater
|
|
|
|
func (updater *InterfaceUpdater) Get() *net.Interface {
|
|
updater.Lock()
|
|
defer updater.Unlock()
|
|
|
|
return updater.iface
|
|
}
|
|
|
|
func (updater *InterfaceUpdater) Update() {
|
|
updater.Lock()
|
|
defer updater.Unlock()
|
|
|
|
got, err := findOutboundInterface(updater.tunIndex, updater.fixedName)
|
|
if err != nil {
|
|
errors.LogInfoInner(context.Background(), err, "[tun] failed to update interface")
|
|
updater.iface = nil
|
|
return
|
|
}
|
|
|
|
if got == nil {
|
|
errors.LogInfo(context.Background(), "[tun] failed to update interface > got == nil")
|
|
updater.iface = nil
|
|
return
|
|
}
|
|
|
|
if updater.iface != nil && updater.iface.Index == got.Index && updater.iface.Name == got.Name {
|
|
return
|
|
}
|
|
|
|
updater.iface = got
|
|
errors.LogInfo(context.Background(), "[tun] update interface ", got.Name, " ", got.Index)
|
|
}
|
|
|
|
// ParseGUID parses a canonical textual GUID, as in
|
|
// "00000000-0000-0000-0000-000000000000", into the 16 bytes it denotes.
|
|
// The form is all that is accepted, not the other ones uuid.Parse takes,
|
|
// such as one without hyphens; the length check is what rules those out.
|
|
func ParseGUID(s string) ([16]byte, error) {
|
|
if len(s) != 36 {
|
|
return [16]byte{}, errors.New("invalid guid: ", s, ", want xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
|
|
}
|
|
id, err := uuid.Parse(s)
|
|
if err != nil {
|
|
return [16]byte{}, errors.New("invalid guid: ", s, ", want xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").Base(err)
|
|
}
|
|
return id, nil
|
|
}
|