-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit_routing.go
More file actions
269 lines (242 loc) · 7.5 KB
/
init_routing.go
File metadata and controls
269 lines (242 loc) · 7.5 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
File: init_routing.go
Version: 1.4.0
Updated: 11-May-2026 14:31 CEST
Description:
Parses and maps client-based and domain-based routing rules for sdproxy.
Translates YAML configurations (MAC, IP, CIDR, ASN, SNI, Path, Name) into
highly optimized lookup tables used by the routing engine.
Changes:
1.4.0 - [REFACTOR] Inherited `ParsePrefixUnmapped` from `process_helpers.go`.
Enforces uniform CIDR extraction boundaries cleanly natively.
1.3.0 - [SECURITY/FIX] Embedded `Unmap()` normalization algorithms directly
into `rkCIDR` network evaluations. Neutralizes routing bypasses where
IPv4-mapped IPv6 identifiers failed to intercept standard IPv4 stubs natively.
*/
package main
import (
"log"
"net"
"net/netip"
"sort"
"strings"
"github.com/miekg/dns"
)
// initClientRoutes parses the `routes:` and `routes_files:` configurations and populates
// the global mapping tables for MAC, IP, CIDR, ASN, SNI, Path, and Client-Name routing.
func initClientRoutes() {
macRoutes = make(map[string]ParsedRoute)
ipRoutes = make(map[string]ParsedRoute)
asnRoutes = make(map[string]ParsedRoute)
clientNameRoutes = make(map[string]ParsedRoute)
sniRoutes = make(map[string]ParsedRoute)
pathRoutes = make(map[string]ParsedRoute)
macWildRoutes = nil
cidrRoutes = nil
processRoute := func(rawKey string, route RouteConfig) {
pr := ParsedRoute{
Upstream: route.Upstream,
ClientName: route.ClientName,
BypassLocal: route.BypassLocal,
Force: route.Force,
}
targetStr := route.Upstream
// RCODE (Return Code) Validation - Absolute highest priority override
if route.Rcode != "" {
actionStr := strings.ToUpper(strings.TrimSpace(route.Rcode))
targetStr = "RCODE:" + actionStr
if actionStr == "DROP" {
pr.HasRcode = true
pr.Rcode = PolicyActionDrop
} else if actionStr == "BLOCK" {
pr.HasRcode = true
pr.Rcode = PolicyActionBlock
} else {
rcode, ok := dns.StringToRcode[actionStr]
if !ok {
if logRouting {
log.Printf("[WARN] routes: unknown RCODE %q for %q — ignored", route.Rcode, rawKey)
}
targetStr = route.Upstream
} else {
pr.HasRcode = true
pr.Rcode = rcode
}
}
}
// Fallbacks mapping (RCODE -> UPSTREAM -> GROUP)
if pr.Upstream == "" && route.Group != "" && !pr.HasRcode {
if grp, ok := cfg.Groups[route.Group]; ok && grp.Upstream != "" {
pr.Upstream = grp.Upstream
if !pr.HasRcode {
targetStr = pr.Upstream
}
}
if !pr.BypassLocal && route.Group != "" {
if grp, ok := cfg.Groups[route.Group]; ok {
pr.BypassLocal = grp.BypassLocal
}
}
}
// Purely cosmetic resolution for the initialization logs if nothing was explicitly set
if targetStr == "" && !pr.HasRcode {
targetStr = "default"
}
var props []string
if pr.BypassLocal {
props = append(props, "bypass_local=true")
}
if pr.Force {
props = append(props, "force=true")
}
propStr := ""
if len(props) > 0 {
propStr = " (" + strings.Join(props, ", ") + ")"
}
// Support grouping multiple identifiers via comma-separated syntax.
for _, key := range strings.Split(rawKey, ",") {
key = strings.TrimSpace(key)
if key == "" {
continue
}
switch classifyRouteKey(key) {
case rkMAC:
if mac, err := net.ParseMAC(key); err == nil {
macRoutes[mac.String()] = pr
if logRouting {
log.Printf("[INIT] MAC Route: %s -> %s%s", mac, targetStr, propStr)
}
}
case rkMACGlob:
macWildRoutes = append(macWildRoutes, macWildRoute{pattern: normaliseMACGlob(key), route: pr})
if logRouting {
log.Printf("[INIT] MAC Glob Route: %s -> %s%s", key, targetStr, propStr)
}
case rkIP:
if ip, err := netip.ParseAddr(key); err == nil {
ipRoutes[ip.Unmap().String()] = pr
if logRouting {
log.Printf("[INIT] IP Route: %s -> %s%s", ip.Unmap().String(), targetStr, propStr)
}
}
case rkCIDR:
if prefix, err := ParsePrefixUnmapped(key); err == nil {
cidrRoutes = append(cidrRoutes, cidrRouteEntry{net: prefix, route: pr})
if logRouting {
log.Printf("[INIT] CIDR Route: %s -> %s%s", prefix.String(), targetStr, propStr)
}
}
case rkASN:
asn := strings.ToUpper(key)
asnRoutes[asn] = pr
if logRouting {
log.Printf("[INIT] ASN Route: %s -> %s%s", asn, targetStr, propStr)
}
case rkSNI:
sni := strings.ToLower(strings.TrimPrefix(key, "sni:"))
sniRoutes[sni] = pr
if logRouting {
log.Printf("[INIT] SNI Route: %s -> %s%s", sni, targetStr, propStr)
}
case rkPath:
p := key
if strings.HasPrefix(p, "path:") {
p = strings.TrimPrefix(p, "path:")
}
p = strings.ToLower(strings.TrimSuffix(p, "/"))
pathRoutes[p] = pr
if logRouting {
log.Printf("[INIT] Path Route: %s -> %s%s", p, targetStr, propStr)
}
case rkClientName:
clientNameRoutes[strings.ToLower(key)] = pr
if logRouting {
log.Printf("[INIT] Client Name Route: %s -> %s%s", key, targetStr, propStr)
}
}
}
}
// 1. Process inline routes map
for rawKey, route := range cfg.Routes {
processRoute(rawKey, route)
}
// 2. Process file-backed routes maps
for filePath, route := range cfg.RoutesFiles {
lines, err := readConfigListFile(filePath)
if err != nil {
if logRouting {
log.Printf("[WARN] routes_files: cannot read %q: %v", filePath, err)
}
continue
}
if logRouting {
log.Printf("[INIT] Loaded %d route(s) from file: %s", len(lines), filePath)
}
for _, line := range lines {
processRoute(line, route)
}
}
hasMACRoutes = len(macRoutes) > 0
hasMACWildRoutes = len(macWildRoutes) > 0
hasIPRoutes = len(ipRoutes) > 0
hasCIDRRoutes = len(cidrRoutes) > 0
hasASNRoutes = len(asnRoutes) > 0
hasClientNameRoutes = len(clientNameRoutes) > 0
hasSNIRoutes = len(sniRoutes) > 0
hasPathRoutes = len(pathRoutes) > 0
// [SECURITY/FIX] Sort CIDR Routes natively by prefix length descending (most specific wins)
if len(cidrRoutes) > 0 {
sort.SliceStable(cidrRoutes, func(i, j int) bool {
return cidrRoutes[i].net.Bits() > cidrRoutes[j].net.Bits()
})
}
}
// initDomainRoutes parses `domain_routes:` and `domain_routes_files:` mapping suffix routes to targets.
func initDomainRoutes() {
domainRoutes = make(map[string]domainRouteEntry, len(cfg.DomainRoutes))
processDomainRoute := func(rawDomain string, dr DomainRouteConfig) {
if dr.Upstream == "" {
if logRouting {
log.Printf("[WARN] domain_routes: entry %q has no upstream — skipped", rawDomain)
}
return
}
for _, domain := range strings.Split(rawDomain, ",") {
domain = strings.TrimSpace(domain)
if domain == "" {
continue
}
clean := strings.ToLower(strings.TrimSuffix(domain, "."))
domainRoutes[clean] = domainRouteEntry{
upstream: dr.Upstream,
bypassLocal: dr.BypassLocal,
}
if logRouting {
if dr.BypassLocal {
log.Printf("[INIT] Domain Route: *.%s -> %s (bypass_local=true)", clean, dr.Upstream)
} else {
log.Printf("[INIT] Domain Route: *.%s -> %s", clean, dr.Upstream)
}
}
}
}
for rawDomain, dr := range cfg.DomainRoutes {
processDomainRoute(rawDomain, dr)
}
for filePath, dr := range cfg.DomainRoutesFiles {
lines, err := readConfigListFile(filePath)
if err != nil {
if logRouting {
log.Printf("[WARN] domain_routes_files: cannot read %q: %v", filePath, err)
}
continue
}
if logRouting {
log.Printf("[INIT] Loaded %d domain route(s) from file: %s", len(lines), filePath)
}
for _, line := range lines {
processDomainRoute(line, dr)
}
}
hasDomainRoutes = len(domainRoutes) > 0
}