Add ip_version rule item

This commit is contained in:
世界 2022-08-16 23:46:05 +08:00
parent d6a0aa7ccf
commit f51128f772
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
10 changed files with 43 additions and 6 deletions

View file

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

View file

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

View file

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

View file

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

5
go.mod
View file

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

4
go.sum
View file

@ -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/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 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/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-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 h1:Z5UMSxFO+c2GtJqDlU7SF4OqzEV76KNYktTyzhofL9o=
github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= 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-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 h1:NlcTFKldteZvYBDyr+V9MjZEI0rAWCSFCyLgPvc5n/Y=
github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM= 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 h1:2hLETh97+S4WnfMR27XyC7QVU1SH7FTNoCznP229YJU=
github.com/sagernet/sing-vmess v0.0.0-20220811135656-4f3f07acf9c4/go.mod h1:82O6gzbxLha/W/jxSVQbsqf2lVdRTjMIgyLug0lpJps= 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= github.com/sagernet/smux v0.0.0-20220812084127-e2d085ee3939 h1:pB1Dh1NbwVrLhQhotr4O4Hs3yhiBzmg3AvnUyYjL4x4=

View file

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

View file

@ -5,6 +5,7 @@ import (
"net/netip" "net/netip"
"strings" "strings"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-dns" "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 { if len(message.Questions) > 0 {
r.dnsLogger.DebugContext(ctx, "exchange ", formatDNSQuestion(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, transport := r.matchDNS(ctx)
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout) ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
defer cancel() defer cancel()

View file

@ -58,6 +58,16 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, 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 != "" { if options.Network != "" {
switch options.Network { switch options.Network {
case N.NetworkTCP, N.NetworkUDP: case N.NetworkTCP, N.NetworkUDP:

View file

@ -15,7 +15,8 @@ func NewIPVersionItem(isIPv6 bool) *IPVersionItem {
} }
func (r *IPVersionItem) Match(metadata *adapter.InboundContext) bool { 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 { func (r *IPVersionItem) String() string {