Fix gateway & FIx prefix check

This commit is contained in:
世界 2024-10-26 14:54:08 +08:00
parent 07278fb470
commit 10f73346a0
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 111 additions and 45 deletions

30
tun.go
View file

@ -53,6 +53,8 @@ type Options struct {
MTU uint32
GSO bool
AutoRoute bool
Inet4Gateway netip.Addr
Inet6Gateway netip.Addr
DNSServers []netip.Addr
IPRoute2TableIndex int
IPRoute2RuleIndex int
@ -82,6 +84,34 @@ type Options struct {
EXP_DisableDNSHijack bool
}
func (o *Options) Inet4GatewayAddr() netip.Addr {
if o.Inet4Gateway.IsValid() {
return o.Inet4Gateway
}
if len(o.Inet4Address) > 0 {
if HasNextAddress(o.Inet4Address[0], 1) {
return o.Inet4Address[0].Addr().Next()
} else if runtime.GOOS != "linux" {
return o.Inet4Address[0].Addr()
}
}
return netip.IPv4Unspecified()
}
func (o *Options) Inet6GatewayAddr() netip.Addr {
if o.Inet6Gateway.IsValid() {
return o.Inet6Gateway
}
if len(o.Inet6Address) > 0 {
if HasNextAddress(o.Inet6Address[0], 1) {
return o.Inet6Address[0].Addr().Next()
} else if runtime.GOOS != "linux" {
return o.Inet6Address[0].Addr()
}
}
return netip.IPv6Unspecified()
}
func CalculateInterfaceName(name string) (tunName string) {
if runtime.GOOS == "darwin" {
tunName = "utun"