@@ -209,6 +209,22 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
209
209
platformInterface : service.FromContext [platform.Interface ](ctx ),
210
210
platformOptions : common .PtrValueOrDefault (options .Platform ),
211
211
}
212
+ for _ , routeAddressSet := range options .RouteAddressSet {
213
+ ruleSet , loaded := router .RuleSet (routeAddressSet )
214
+ if ! loaded {
215
+ return nil , E .New ("parse route_address_set: rule-set not found: " , routeAddressSet )
216
+ }
217
+ ruleSet .IncRef ()
218
+ inbound .routeRuleSet = append (inbound .routeRuleSet , ruleSet )
219
+ }
220
+ for _ , routeExcludeAddressSet := range options .RouteExcludeAddressSet {
221
+ ruleSet , loaded := router .RuleSet (routeExcludeAddressSet )
222
+ if ! loaded {
223
+ return nil , E .New ("parse route_exclude_address_set: rule-set not found: " , routeExcludeAddressSet )
224
+ }
225
+ ruleSet .IncRef ()
226
+ inbound .routeExcludeRuleSet = append (inbound .routeExcludeRuleSet , ruleSet )
227
+ }
212
228
if options .AutoRedirect {
213
229
if ! options .AutoRoute {
214
230
return nil , E .New ("`auto_route` is required by `auto_redirect`" )
@@ -229,32 +245,11 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
229
245
if err != nil {
230
246
return nil , E .Cause (err , "initialize auto-redirect" )
231
247
}
232
- if runtime .GOOS != "android" {
233
- var markMode bool
234
- for _ , routeAddressSet := range options .RouteAddressSet {
235
- ruleSet , loaded := router .RuleSet (routeAddressSet )
236
- if ! loaded {
237
- return nil , E .New ("parse route_address_set: rule-set not found: " , routeAddressSet )
238
- }
239
- ruleSet .IncRef ()
240
- inbound .routeRuleSet = append (inbound .routeRuleSet , ruleSet )
241
- markMode = true
242
- }
243
- for _ , routeExcludeAddressSet := range options .RouteExcludeAddressSet {
244
- ruleSet , loaded := router .RuleSet (routeExcludeAddressSet )
245
- if ! loaded {
246
- return nil , E .New ("parse route_exclude_address_set: rule-set not found: " , routeExcludeAddressSet )
247
- }
248
- ruleSet .IncRef ()
249
- inbound .routeExcludeRuleSet = append (inbound .routeExcludeRuleSet , ruleSet )
250
- markMode = true
251
- }
252
- if markMode {
253
- inbound .tunOptions .AutoRedirectMarkMode = true
254
- err = networkManager .RegisterAutoRedirectOutputMark (inbound .tunOptions .AutoRedirectOutputMark )
255
- if err != nil {
256
- return nil , err
257
- }
248
+ if runtime .GOOS != "android" && len (inbound .routeAddressSet ) > 0 || len (inbound .routeExcludeAddressSet ) > 0 {
249
+ inbound .tunOptions .AutoRedirectMarkMode = true
250
+ err = networkManager .RegisterAutoRedirectOutputMark (inbound .tunOptions .AutoRedirectOutputMark )
251
+ if err != nil {
252
+ return nil , err
258
253
}
259
254
}
260
255
}
@@ -310,18 +305,62 @@ func (t *Inbound) Start(stage adapter.StartStage) error {
310
305
if t .tunOptions .Name == "" {
311
306
t .tunOptions .Name = tun .CalculateInterfaceName ("" )
312
307
}
308
+ if t .platformInterface == nil || runtime .GOOS != "android" {
309
+ t .routeAddressSet = common .FlatMap (t .routeRuleSet , adapter .RuleSet .ExtractIPSet )
310
+ for _ , routeRuleSet := range t .routeRuleSet {
311
+ ipSets := routeRuleSet .ExtractIPSet ()
312
+ if len (ipSets ) == 0 {
313
+ t .logger .Warn ("route_address_set: no destination IP CIDR rules found in rule-set: " , routeRuleSet .Name ())
314
+ }
315
+ t .routeRuleSetCallback = append (t .routeRuleSetCallback , routeRuleSet .RegisterCallback (t .updateRouteAddressSet ))
316
+ routeRuleSet .DecRef ()
317
+ t .routeAddressSet = append (t .routeAddressSet , ipSets ... )
318
+ }
319
+ t .routeExcludeAddressSet = common .FlatMap (t .routeExcludeRuleSet , adapter .RuleSet .ExtractIPSet )
320
+ for _ , routeExcludeRuleSet := range t .routeExcludeRuleSet {
321
+ ipSets := routeExcludeRuleSet .ExtractIPSet ()
322
+ if len (ipSets ) == 0 {
323
+ t .logger .Warn ("route_address_set: no destination IP CIDR rules found in rule-set: " , routeExcludeRuleSet .Name ())
324
+ }
325
+ t .routeExcludeRuleSetCallback = append (t .routeExcludeRuleSetCallback , routeExcludeRuleSet .RegisterCallback (t .updateRouteAddressSet ))
326
+ routeExcludeRuleSet .DecRef ()
327
+ t .routeExcludeAddressSet = append (t .routeExcludeAddressSet , ipSets ... )
328
+ }
329
+ }
313
330
var (
314
331
tunInterface tun.Tun
315
332
err error
316
333
)
317
334
monitor := taskmonitor .New (t .logger , C .StartTimeout )
318
- monitor .Start ("open tun interface" )
335
+ tunOptions := t .tunOptions
336
+ if t .autoRedirect == nil && ! (runtime .GOOS == "android" && t .platformInterface != nil ) {
337
+ for _ , ipSet := range t .routeAddressSet {
338
+ for _ , prefix := range ipSet .Prefixes () {
339
+ if prefix .Addr ().Is4 () {
340
+ tunOptions .Inet4RouteAddress = append (tunOptions .Inet4RouteAddress , prefix )
341
+ } else {
342
+ tunOptions .Inet6RouteAddress = append (tunOptions .Inet6RouteAddress , prefix )
343
+ }
344
+ }
345
+ }
346
+ for _ , ipSet := range t .routeExcludeAddressSet {
347
+ for _ , prefix := range ipSet .Prefixes () {
348
+ if prefix .Addr ().Is4 () {
349
+ tunOptions .Inet4RouteExcludeAddress = append (tunOptions .Inet4RouteExcludeAddress , prefix )
350
+ } else {
351
+ tunOptions .Inet6RouteExcludeAddress = append (tunOptions .Inet6RouteExcludeAddress , prefix )
352
+ }
353
+ }
354
+ }
355
+ }
356
+ monitor .Start ("open interface" )
319
357
if t .platformInterface != nil {
320
- tunInterface , err = t .platformInterface .OpenTun (& t . tunOptions , t .platformOptions )
358
+ tunInterface , err = t .platformInterface .OpenTun (& tunOptions , t .platformOptions )
321
359
} else {
322
- tunInterface , err = tun .New (t . tunOptions )
360
+ tunInterface , err = tun .New (tunOptions )
323
361
}
324
362
monitor .Finish ()
363
+ t .tunOptions .Name = tunOptions .Name
325
364
if err != nil {
326
365
return E .Cause (err , "configure tun interface" )
327
366
}
@@ -366,47 +405,57 @@ func (t *Inbound) Start(stage adapter.StartStage) error {
366
405
return E .Cause (err , "starting TUN interface" )
367
406
}
368
407
if t .autoRedirect != nil {
369
- t .routeAddressSet = common .FlatMap (t .routeRuleSet , adapter .RuleSet .ExtractIPSet )
370
- for _ , routeRuleSet := range t .routeRuleSet {
371
- ipSets := routeRuleSet .ExtractIPSet ()
372
- if len (ipSets ) == 0 {
373
- t .logger .Warn ("route_address_set: no destination IP CIDR rules found in rule-set: " , routeRuleSet .Name ())
374
- }
375
- t .routeAddressSet = append (t .routeAddressSet , ipSets ... )
376
- }
377
- t .routeExcludeAddressSet = common .FlatMap (t .routeExcludeRuleSet , adapter .RuleSet .ExtractIPSet )
378
- for _ , routeExcludeRuleSet := range t .routeExcludeRuleSet {
379
- ipSets := routeExcludeRuleSet .ExtractIPSet ()
380
- if len (ipSets ) == 0 {
381
- t .logger .Warn ("route_address_set: no destination IP CIDR rules found in rule-set: " , routeExcludeRuleSet .Name ())
382
- }
383
- t .routeExcludeAddressSet = append (t .routeExcludeAddressSet , ipSets ... )
384
- }
385
408
monitor .Start ("initialize auto-redirect" )
386
409
err := t .autoRedirect .Start ()
387
410
monitor .Finish ()
388
411
if err != nil {
389
412
return E .Cause (err , "auto-redirect" )
390
413
}
391
- for _ , routeRuleSet := range t .routeRuleSet {
392
- t .routeRuleSetCallback = append (t .routeRuleSetCallback , routeRuleSet .RegisterCallback (t .updateRouteAddressSet ))
393
- routeRuleSet .DecRef ()
394
- }
395
- for _ , routeExcludeRuleSet := range t .routeExcludeRuleSet {
396
- t .routeExcludeRuleSetCallback = append (t .routeExcludeRuleSetCallback , routeExcludeRuleSet .RegisterCallback (t .updateRouteAddressSet ))
397
- routeExcludeRuleSet .DecRef ()
398
- }
399
- t .routeAddressSet = nil
400
- t .routeExcludeAddressSet = nil
401
414
}
415
+ t .routeAddressSet = nil
416
+ t .routeExcludeAddressSet = nil
402
417
}
403
418
return nil
404
419
}
405
420
406
421
func (t * Inbound ) updateRouteAddressSet (it adapter.RuleSet ) {
407
422
t .routeAddressSet = common .FlatMap (t .routeRuleSet , adapter .RuleSet .ExtractIPSet )
408
423
t .routeExcludeAddressSet = common .FlatMap (t .routeExcludeRuleSet , adapter .RuleSet .ExtractIPSet )
409
- t .autoRedirect .UpdateRouteAddressSet ()
424
+ if t .autoRedirect != nil {
425
+ t .autoRedirect .UpdateRouteAddressSet ()
426
+ } else {
427
+ tunOptions := t .tunOptions
428
+ for _ , ipSet := range t .routeAddressSet {
429
+ for _ , prefix := range ipSet .Prefixes () {
430
+ if prefix .Addr ().Is4 () {
431
+ tunOptions .Inet4RouteAddress = append (tunOptions .Inet4RouteAddress , prefix )
432
+ } else {
433
+ tunOptions .Inet6RouteAddress = append (tunOptions .Inet6RouteAddress , prefix )
434
+ }
435
+ }
436
+ }
437
+ for _ , ipSet := range t .routeExcludeAddressSet {
438
+ for _ , prefix := range ipSet .Prefixes () {
439
+ if prefix .Addr ().Is4 () {
440
+ tunOptions .Inet4RouteExcludeAddress = append (tunOptions .Inet4RouteExcludeAddress , prefix )
441
+ } else {
442
+ tunOptions .Inet6RouteExcludeAddress = append (tunOptions .Inet6RouteExcludeAddress , prefix )
443
+ }
444
+ }
445
+ }
446
+ if t .platformInterface != nil {
447
+ err := t .platformInterface .UpdateRouteOptions (& tunOptions , t .platformOptions )
448
+ if err != nil {
449
+ t .logger .Error ("update route addresses: " , err )
450
+ }
451
+ } else {
452
+ err := t .tunIf .UpdateRouteOptions (tunOptions )
453
+ if err != nil {
454
+ t .logger .Error ("update route addresses: " , err )
455
+ }
456
+ }
457
+ t .logger .Info ("updated route addresses" )
458
+ }
410
459
t .routeAddressSet = nil
411
460
t .routeExcludeAddressSet = nil
412
461
}
0 commit comments