소스 검색

Add ip_version rule item

世界 3 년 전
부모
커밋
f51128f772
10개의 변경된 파일43개의 추가작업 그리고 6개의 파일을 삭제
  1. 1 0
      adapter/inbound.go
  2. 1 0
      constant/proxy.go
  3. 7 0
      docs/configuration/dns/rule.md
  4. 7 0
      docs/configuration/route/rule.md
  5. 0 5
      go.mod
  6. 4 0
      go.sum
  7. 1 0
      option/dns.go
  8. 10 0
      route/router_dns.go
  9. 10 0
      route/rule_dns.go
  10. 2 1
      route/rule_ipversion.go

+ 1 - 0
adapter/inbound.go

@@ -18,6 +18,7 @@ type Inbound interface {
 type InboundContext struct {
 	Inbound     string
 	InboundType string
+	IPVersion   int
 	Network     string
 	Source      M.Socksaddr
 	Destination M.Socksaddr

+ 1 - 0
constant/proxy.go

@@ -14,6 +14,7 @@ const (
 	TypeVMess       = "vmess"
 	TypeTrojan      = "trojan"
 	TypeNaive       = "naive"
+	TypeWireGuard   = "wireguard"
 )
 
 const (

+ 7 - 0
docs/configuration/dns/rule.md

@@ -8,6 +8,7 @@
         "inbound": [
           "mixed-in"
         ],
+        "ip_version": 6,
         "network": "tcp",
         "auth_user": [
           "usera",
@@ -105,6 +106,12 @@
 
 Tags of [inbound](../inbound).
 
+#### ip_version
+
+4 (A query) or 6 (AAAA dns query).
+
+Not limited if empty.
+
 #### network
 
 `tcp` or `udp`.

+ 7 - 0
docs/configuration/route/rule.md

@@ -8,6 +8,7 @@
         "inbound": [
           "mixed-in"
         ],
+        "ip_version": 6,
         "network": "tcp",
         "auth_user": [
           "usera",
@@ -107,6 +108,12 @@
 
 Tags of [inbound](../inbound).
 
+#### ip_version
+
+4 or 6.
+
+Not limited if empty.
+
 #### auth_user
 
 Username, see each inbound for details.

+ 0 - 5
go.mod

@@ -31,11 +31,6 @@ require (
 	gvisor.dev/gvisor v0.0.0-20220801010827-addd1f7b3e97
 )
 
-replace (
-	github.com/sagernet/netlink => ../../GolandProjects/netlink
-	github.com/sagernet/sing-tun => ../sing-tun
-)
-
 require (
 	github.com/ajg/form v1.5.1 // indirect
 	github.com/cheekybits/genny v1.0.0 // indirect

+ 4 - 0
go.sum

@@ -150,6 +150,8 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q
 github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
 github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sagernet/netlink v0.0.0-20220816152750-7a75378bd31a h1:iNtsfGMenajBUGZ/1yAzl1v3p+t/7IJ/ilQXq9haRZ8=
+github.com/sagernet/netlink v0.0.0-20220816152750-7a75378bd31a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
 github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
 github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08 h1:Z5UMSxFO+c2GtJqDlU7SF4OqzEV76KNYktTyzhofL9o=
 github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
@@ -157,6 +159,8 @@ github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae h1:xOpbvgizvIbKK
 github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48 h1:NlcTFKldteZvYBDyr+V9MjZEI0rAWCSFCyLgPvc5n/Y=
 github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM=
+github.com/sagernet/sing-tun v0.0.0-20220816152948-85c649d9a3e8 h1:ttjzAzTkVnjGlmw32tGjEdgykSl/qT1i94gxJ1tPDcA=
+github.com/sagernet/sing-tun v0.0.0-20220816152948-85c649d9a3e8/go.mod h1:q8vcsDiAbvfepmYBxGcYfyHY7AcLCKW3WewC+zE8++Q=
 github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4 h1:2hLETh97+S4WnfMR27XyC7QVU1SH7FTNoCznP229YJU=
 github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps=
 github.com/sagernet/smux v0.0.0-20220812084127-e2d085ee3939 h1:pB1Dh1NbwVrLhQhotr4O4Hs3yhiBzmg3AvnUyYjL4x4=

+ 1 - 0
option/dns.go

@@ -77,6 +77,7 @@ func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
 
 type DefaultDNSRule struct {
 	Inbound         Listable[string] `json:"inbound,omitempty"`
+	IPVersion       int              `json:"ip_version,omitempty"`
 	Network         string           `json:"network,omitempty"`
 	AuthUser        Listable[string] `json:"auth_user,omitempty"`
 	Protocol        Listable[string] `json:"protocol,omitempty"`

+ 10 - 0
route/router_dns.go

@@ -5,6 +5,7 @@ import (
 	"net/netip"
 	"strings"
 
+	"github.com/sagernet/sing-box/adapter"
 	C "github.com/sagernet/sing-box/constant"
 	"github.com/sagernet/sing-box/log"
 	"github.com/sagernet/sing-dns"
@@ -18,6 +19,15 @@ func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dn
 	if len(message.Questions) > 0 {
 		r.dnsLogger.DebugContext(ctx, "exchange ", formatDNSQuestion(message.Questions[0]))
 	}
+	ctx, metadata := adapter.AppendContext(ctx)
+	if len(message.Questions) > 0 {
+		switch message.Questions[0].Type {
+		case dnsmessage.TypeA:
+			metadata.IPVersion = 4
+		case dnsmessage.TypeAAAA:
+			metadata.IPVersion = 6
+		}
+	}
 	ctx, transport := r.matchDNS(ctx)
 	ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
 	defer cancel()

+ 10 - 0
route/rule_dns.go

@@ -58,6 +58,16 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
 		rule.items = append(rule.items, item)
 		rule.allItems = append(rule.allItems, item)
 	}
+	if options.IPVersion > 0 {
+		switch options.IPVersion {
+		case 4, 6:
+			item := NewIPVersionItem(options.IPVersion == 6)
+			rule.items = append(rule.items, item)
+			rule.allItems = append(rule.allItems, item)
+		default:
+			return nil, E.New("invalid ip version: ", options.IPVersion)
+		}
+	}
 	if options.Network != "" {
 		switch options.Network {
 		case N.NetworkTCP, N.NetworkUDP:

+ 2 - 1
route/rule_ipversion.go

@@ -15,7 +15,8 @@ func NewIPVersionItem(isIPv6 bool) *IPVersionItem {
 }
 
 func (r *IPVersionItem) Match(metadata *adapter.InboundContext) bool {
-	return metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
+	return metadata.IPVersion != 0 && metadata.IPVersion == 6 == r.isIPv6 ||
+		metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
 }
 
 func (r *IPVersionItem) String() string {