Fix auto_redirect fallback rule
This commit is contained in:
parent
e88ed52dbc
commit
1d02d635b9
2 changed files with 43 additions and 53 deletions
2
tun.go
2
tun.go
|
|
@ -65,6 +65,7 @@ type DarwinTUN interface {
|
||||||
const (
|
const (
|
||||||
DefaultIPRoute2TableIndex = 2022
|
DefaultIPRoute2TableIndex = 2022
|
||||||
DefaultIPRoute2RuleIndex = 9000
|
DefaultIPRoute2RuleIndex = 9000
|
||||||
|
DefaultIPRoute2AutoRedirectFallbackRuleIndex = 32768
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
|
@ -80,6 +81,7 @@ type Options struct {
|
||||||
DNSServers []netip.Addr
|
DNSServers []netip.Addr
|
||||||
IPRoute2TableIndex int
|
IPRoute2TableIndex int
|
||||||
IPRoute2RuleIndex int
|
IPRoute2RuleIndex int
|
||||||
|
IPRoute2AutoRedirectFallbackRuleIndex int
|
||||||
AutoRedirectMarkMode bool
|
AutoRedirectMarkMode bool
|
||||||
AutoRedirectInputMark uint32
|
AutoRedirectInputMark uint32
|
||||||
AutoRedirectOutputMark uint32
|
AutoRedirectOutputMark uint32
|
||||||
|
|
|
||||||
18
tun_linux.go
18
tun_linux.go
|
|
@ -3,7 +3,6 @@ package tun
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -284,16 +283,6 @@ func (t *NativeTun) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.options.IPRoute2TableIndex == 0 {
|
|
||||||
for {
|
|
||||||
t.options.IPRoute2TableIndex = int(rand.Uint32())
|
|
||||||
routeList, fErr := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: t.options.IPRoute2TableIndex}, netlink.RT_FILTER_TABLE)
|
|
||||||
if len(routeList) == 0 || fErr != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = t.setRoute(tunLink)
|
err = t.setRoute(tunLink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = t.unsetRoute0(tunLink)
|
_ = t.unsetRoute0(tunLink)
|
||||||
|
|
@ -632,17 +621,16 @@ func (t *NativeTun) rules() []*netlink.Rule {
|
||||||
}
|
}
|
||||||
// Fallback rules after system default rules (32766: main, 32767: default)
|
// Fallback rules after system default rules (32766: main, 32767: default)
|
||||||
// Only reached when main and default tables have no route
|
// Only reached when main and default tables have no route
|
||||||
const fallbackPriority = 32768
|
|
||||||
if p4 {
|
if p4 {
|
||||||
it = netlink.NewRule()
|
it = netlink.NewRule()
|
||||||
it.Priority = fallbackPriority
|
it.Priority = t.options.IPRoute2AutoRedirectFallbackRuleIndex
|
||||||
it.Table = t.options.IPRoute2TableIndex
|
it.Table = t.options.IPRoute2TableIndex
|
||||||
it.Family = unix.AF_INET
|
it.Family = unix.AF_INET
|
||||||
rules = append(rules, it)
|
rules = append(rules, it)
|
||||||
}
|
}
|
||||||
if p6 {
|
if p6 {
|
||||||
it = netlink.NewRule()
|
it = netlink.NewRule()
|
||||||
it.Priority = fallbackPriority
|
it.Priority = t.options.IPRoute2AutoRedirectFallbackRuleIndex
|
||||||
it.Table = t.options.IPRoute2TableIndex
|
it.Table = t.options.IPRoute2TableIndex
|
||||||
it.Family = unix.AF_INET6
|
it.Family = unix.AF_INET6
|
||||||
rules = append(rules, it)
|
rules = append(rules, it)
|
||||||
|
|
@ -1020,7 +1008,7 @@ func (t *NativeTun) unsetRules() error {
|
||||||
for _, rule := range ruleList {
|
for _, rule := range ruleList {
|
||||||
ruleStart := t.options.IPRoute2RuleIndex
|
ruleStart := t.options.IPRoute2RuleIndex
|
||||||
ruleEnd := ruleStart + 10
|
ruleEnd := ruleStart + 10
|
||||||
if rule.Priority >= ruleStart && rule.Priority <= ruleEnd {
|
if rule.Priority >= ruleStart && rule.Priority <= ruleEnd || (t.options.AutoRedirectMarkMode && rule.Priority == t.options.IPRoute2AutoRedirectFallbackRuleIndex) {
|
||||||
ruleToDel := netlink.NewRule()
|
ruleToDel := netlink.NewRule()
|
||||||
ruleToDel.Family = rule.Family
|
ruleToDel.Family = rule.Family
|
||||||
ruleToDel.Priority = rule.Priority
|
ruleToDel.Priority = rule.Priority
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue