Repository navigation
Expand file tree
/
Copy pathredirect.go
More file actions
69 lines (60 loc) · 2.03 KB
/
Copy pathredirect.go
File metadata and controls
69 lines (60 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tun
import (
"context"
"net/netip"
"runtime"
"github.com/sagernet/sing/common/control"
"github.com/sagernet/sing/common/logger"
N "github.com/sagernet/sing/common/network"
)
const (
DefaultAutoRedirectInputMark = 0x2023
DefaultAutoRedirectOutputMark = 0x2024
DefaultAutoRedirectResetMark = 0x2025
DefaultAutoRedirectTProxyMark = 0x2026
DefaultAutoRedirectNFQueue = 100
DefaultAutoRedirectInputMarkAndroid = 0x400000
DefaultAutoRedirectOutputMarkAndroid = 0x200000
DefaultAutoRedirectResetMarkAndroid = 0x600000
DefaultAutoRedirectTProxyMarkAndroid = 0x800000
)
type AutoRedirect interface {
Start() error
Close() error
UpdateRouteAddressSet() error
}
type AutoRedirectHandler interface {
JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) FlowVerdict
N.TCPConnectionHandlerEx
}
type AutoRedirectOptions struct {
TunOptions *Options
Context context.Context
Handler AutoRedirectHandler
Logger logger.Logger
NetworkMonitor NetworkUpdateMonitor
InterfaceFinder control.InterfaceFinder
TableName string
DisableNFTables bool
CustomRedirectPort func() int
CustomRedirectListenerFD func() (int, error)
RouteAddressSet func() (include []netip.Prefix, exclude []netip.Prefix, err error)
AndroidVPNService bool
}
// netd's Fwmark owns bits 0-19 (netId, explicitlySelected, protectedFromVpn,
// permission) and bit 20 (uidBillingDone, written by the bw_* chains); bits
// 29-30 are the vendor field and bit 31 is ingress_cpu_wakeup. The mark is also
// mirrored into the connmark, where netd's StrictController owns bits 24-25.
const androidReservedMarkMask = 0xE31FFFFF
func effectiveMark(value uint32, defaultValue uint32, androidDefaultValue uint32) uint32 {
if runtime.GOOS == "android" {
if value != 0 && value&androidReservedMarkMask == 0 {
return value
}
return androidDefaultValue
}
if value != 0 {
return value
}
return defaultValue
}