浏览代码

Add route.default_mark option

世界 3 年之前
父节点
当前提交
fe8e984608
共有 5 个文件被更改,包括 18 次插入2 次删除
  1. 1 0
      adapter/router.go
  2. 3 0
      common/dialer/default.go
  3. 1 1
      common/process/searcher_without_name.go
  4. 7 1
      option/route.go
  5. 6 0
      route/router.go

+ 1 - 0
adapter/router.go

@@ -36,6 +36,7 @@ type Router interface {
 	AutoDetectInterface() bool
 	AutoDetectInterfaceName() string
 	AutoDetectInterfaceIndex() int
+	DefaultMark() int
 
 	Rules() []Rule
 	SetTrafficController(controller TrafficController)

+ 3 - 0
common/dialer/default.go

@@ -49,6 +49,9 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
 	if options.RoutingMark != 0 {
 		dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
 		listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
+	} else if router.DefaultMark() != 0 {
+		dialer.Control = control.Append(dialer.Control, control.RoutingMark(router.DefaultMark()))
+		listener.Control = control.Append(listener.Control, control.RoutingMark(router.DefaultMark()))
 	}
 	if options.ReuseAddr {
 		listener.Control = control.Append(listener.Control, control.ReuseAddr())

+ 1 - 1
common/process/searcher_without_name.go

@@ -1,4 +1,4 @@
-//go:build !cgo || !linux || android
+//go:build !(cgo && linux && !android)
 
 package process
 

+ 7 - 1
option/route.go

@@ -16,12 +16,18 @@ type RouteOptions struct {
 	FindProcess         bool            `json:"find_process,omitempty"`
 	AutoDetectInterface bool            `json:"auto_detect_interface,omitempty"`
 	DefaultInterface    string          `json:"default_interface,omitempty"`
+	DefaultMark         int             `json:"default_mark,omitempty"`
 }
 
 func (o RouteOptions) Equals(other RouteOptions) bool {
 	return common.ComparablePtrEquals(o.GeoIP, other.GeoIP) &&
 		common.ComparablePtrEquals(o.Geosite, other.Geosite) &&
-		common.SliceEquals(o.Rules, other.Rules)
+		common.SliceEquals(o.Rules, other.Rules) &&
+		o.Final == other.Final &&
+		o.FindProcess == other.FindProcess &&
+		o.AutoDetectInterface == other.AutoDetectInterface &&
+		o.DefaultInterface == other.DefaultInterface &&
+		o.DefaultMark == other.DefaultMark
 }
 
 type GeoIPOptions struct {

+ 6 - 0
route/router.go

@@ -68,6 +68,7 @@ type Router struct {
 	autoDetectInterface                bool
 	defaultInterface                   string
 	interfaceMonitor                   DefaultInterfaceMonitor
+	defaultMark                        int
 	trafficController                  adapter.TrafficController
 	urlTestHistoryStorage              *urltest.HistoryStorage
 	processSearcher                    process.Searcher
@@ -92,6 +93,7 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
 		interfaceBindManager:  control.NewBindManager(),
 		autoDetectInterface:   options.AutoDetectInterface,
 		defaultInterface:      options.DefaultInterface,
+		defaultMark:           options.DefaultMark,
 	}
 	for i, ruleOptions := range options.Rules {
 		routeRule, err := NewRule(router, logger, ruleOptions)
@@ -637,6 +639,10 @@ func (r *Router) AutoDetectInterfaceIndex() int {
 	return r.interfaceMonitor.DefaultInterfaceIndex()
 }
 
+func (r *Router) DefaultMark() int {
+	return r.defaultMark
+}
+
 func (r *Router) Rules() []adapter.Rule {
 	return r.rules
 }