diff --git a/adapter/inbound.go b/adapter/inbound.go index c8d77dcc..8119a37f 100644 --- a/adapter/inbound.go +++ b/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 diff --git a/constant/proxy.go b/constant/proxy.go index 6dbee726..f5024059 100644 --- a/constant/proxy.go +++ b/constant/proxy.go @@ -14,6 +14,7 @@ const ( TypeVMess = "vmess" TypeTrojan = "trojan" TypeNaive = "naive" + TypeWireGuard = "wireguard" ) const ( diff --git a/docs/configuration/dns/rule.md b/docs/configuration/dns/rule.md index ff1e6b60..91cb669b 100644 --- a/docs/configuration/dns/rule.md +++ b/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`. diff --git a/docs/configuration/route/rule.md b/docs/configuration/route/rule.md index fd6159bc..6ef4e398 100644 --- a/docs/configuration/route/rule.md +++ b/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. diff --git a/go.mod b/go.mod index 618eaad2..0bde0876 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 485c7e27..f36e5b80 100644 --- a/go.sum +++ b/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= diff --git a/option/dns.go b/option/dns.go index 20e08c5a..1f68388f 100644 --- a/option/dns.go +++ b/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"` diff --git a/route/router_dns.go b/route/router_dns.go index cbdb3997..1e689132 100644 --- a/route/router_dns.go +++ b/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() diff --git a/route/rule_dns.go b/route/rule_dns.go index 6dd81c10..053bf777 100644 --- a/route/rule_dns.go +++ b/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: diff --git a/route/rule_ipversion.go b/route/rule_ipversion.go index ba95ee0b..3d8762b4 100644 --- a/route/rule_ipversion.go +++ b/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 {