Skip to content

Commit f17fabf

Browse files
authored
Sniffing: domainsExcluded supports "geosite:" (#5927)
#5927 (comment) #5927 (comment)
1 parent 05a1191 commit f17fabf

11 files changed

Lines changed: 157 additions & 130 deletions

File tree

app/dispatcher/default.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dispatcher
22

33
import (
44
"context"
5-
"regexp"
65
"strings"
76
"sync"
87
"time"
@@ -235,22 +234,8 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu
235234
if domain == "" {
236235
return false
237236
}
238-
for _, d := range request.ExcludeForDomain {
239-
if strings.HasPrefix(d, "regexp:") {
240-
pattern := d[7:]
241-
re, err := regexp.Compile(pattern)
242-
if err != nil {
243-
errors.LogInfo(ctx, "Unable to compile regex")
244-
continue
245-
}
246-
if re.MatchString(domain) {
247-
return false
248-
}
249-
} else {
250-
if strings.ToLower(domain) == d {
251-
return false
252-
}
253-
}
237+
if request.ExcludeForDomain != nil && request.ExcludeForDomain.MatchAny(strings.ToLower(domain)) {
238+
return false
254239
}
255240
protocolString := result.Protocol()
256241
if resComp, ok := result.(SnifferResultComposite); ok {

app/proxyman/config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package proxyman
2+
3+
import (
4+
"github.com/xtls/xray-core/common/geodata"
5+
"github.com/xtls/xray-core/common/session"
6+
)
7+
8+
func BuildSniffingRequest(config *SniffingConfig) (session.SniffingRequest, error) {
9+
if config == nil {
10+
return session.SniffingRequest{}, nil
11+
}
12+
13+
request := session.SniffingRequest{
14+
Enabled: config.Enabled,
15+
OverrideDestinationForProtocol: config.DestinationOverride,
16+
MetadataOnly: config.MetadataOnly,
17+
RouteOnly: config.RouteOnly,
18+
}
19+
if len(config.DomainsExcluded) > 0 {
20+
excludeForDomain, err := geodata.DomainReg.BuildDomainMatcher(config.DomainsExcluded)
21+
if err != nil {
22+
return session.SniffingRequest{}, err
23+
}
24+
request.ExcludeForDomain = excludeForDomain
25+
}
26+
return request, nil
27+
}

app/proxyman/config.pb.go

Lines changed: 32 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/proxyman/config.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "common/net/address.proto";
1010
import "common/net/port.proto";
1111
import "transport/internet/config.proto";
1212
import "common/serial/typed_message.proto";
13+
import "common/geodata/geodat.proto";
1314

1415
message InboundConfig {}
1516

@@ -20,7 +21,8 @@ message SniffingConfig {
2021
// Override target destination if sniff'ed protocol is in the given list.
2122
// Supported values are "http", "tls", "fakedns".
2223
repeated string destination_override = 2;
23-
repeated string domains_excluded = 3;
24+
25+
repeated xray.common.geodata.DomainRule domains_excluded = 3;
2426

2527
// Whether should only try to sniff metadata without waiting for client input.
2628
// Can be used to support SMTP like protocol where server send the first

app/proxyman/inbound/always.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,17 @@ type AlwaysOnInboundHandler struct {
5353
}
5454

5555
func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
56+
sniffingRequest, err := proxyman.BuildSniffingRequest(receiverConfig.SniffingSettings)
57+
if err != nil {
58+
return nil, err
59+
}
60+
5661
// Set tag and sniffing config in context before creating proxy
5762
// This allows proxies like TUN to access these settings
5863
ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: tag})
5964
if receiverConfig.SniffingSettings != nil {
6065
ctx = session.ContextWithContent(ctx, &session.Content{
61-
SniffingRequest: session.SniffingRequest{
62-
Enabled: receiverConfig.SniffingSettings.Enabled,
63-
OverrideDestinationForProtocol: receiverConfig.SniffingSettings.DestinationOverride,
64-
ExcludeForDomain: receiverConfig.SniffingSettings.DomainsExcluded,
65-
MetadataOnly: receiverConfig.SniffingSettings.MetadataOnly,
66-
RouteOnly: receiverConfig.SniffingSettings.RouteOnly,
67-
},
66+
SniffingRequest: sniffingRequest,
6867
})
6968
}
7069
rawProxy, err := common.CreateObject(ctx, proxyConfig)
@@ -117,7 +116,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
117116
stream: mss,
118117
tag: tag,
119118
dispatcher: h.mux,
120-
sniffingConfig: receiverConfig.SniffingSettings,
119+
sniffingRequest: sniffingRequest,
121120
uplinkCounter: uplinkCounter,
122121
downlinkCounter: downlinkCounter,
123122
ctx: ctx,
@@ -139,7 +138,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
139138
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
140139
tag: tag,
141140
dispatcher: h.mux,
142-
sniffingConfig: receiverConfig.SniffingSettings,
141+
sniffingRequest: sniffingRequest,
143142
uplinkCounter: uplinkCounter,
144143
downlinkCounter: downlinkCounter,
145144
ctx: ctx,
@@ -154,7 +153,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
154153
address: address,
155154
port: net.Port(port),
156155
dispatcher: h.mux,
157-
sniffingConfig: receiverConfig.SniffingSettings,
156+
sniffingRequest: sniffingRequest,
158157
uplinkCounter: uplinkCounter,
159158
downlinkCounter: downlinkCounter,
160159
stream: mss,

app/proxyman/inbound/worker.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync/atomic"
77
"time"
88

9-
"github.com/xtls/xray-core/app/proxyman"
109
"github.com/xtls/xray-core/common"
1110
"github.com/xtls/xray-core/common/buf"
1211
c "github.com/xtls/xray-core/common/ctx"
@@ -43,7 +42,7 @@ type tcpWorker struct {
4342
recvOrigDest bool
4443
tag string
4544
dispatcher routing.Dispatcher
46-
sniffingConfig *proxyman.SniffingConfig
45+
sniffingRequest session.SniffingRequest
4746
uplinkCounter stats.Counter
4847
downlinkCounter stats.Counter
4948

@@ -118,13 +117,7 @@ func (w *tcpWorker) callback(conn stat.Connection) {
118117
})
119118

120119
content := new(session.Content)
121-
if w.sniffingConfig != nil {
122-
content.SniffingRequest.Enabled = w.sniffingConfig.Enabled
123-
content.SniffingRequest.OverrideDestinationForProtocol = w.sniffingConfig.DestinationOverride
124-
content.SniffingRequest.ExcludeForDomain = w.sniffingConfig.DomainsExcluded
125-
content.SniffingRequest.MetadataOnly = w.sniffingConfig.MetadataOnly
126-
content.SniffingRequest.RouteOnly = w.sniffingConfig.RouteOnly
127-
}
120+
content.SniffingRequest = w.sniffingRequest
128121
ctx = session.ContextWithContent(ctx, content)
129122

130123
if err := w.proxy.Process(ctx, net.Network_TCP, conn, w.dispatcher); err != nil {
@@ -275,7 +268,7 @@ type udpWorker struct {
275268
tag string
276269
stream *internet.MemoryStreamConfig
277270
dispatcher routing.Dispatcher
278-
sniffingConfig *proxyman.SniffingConfig
271+
sniffingRequest session.SniffingRequest
279272
uplinkCounter stats.Counter
280273
downlinkCounter stats.Counter
281274

@@ -365,13 +358,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
365358
Tag: w.tag,
366359
})
367360
content := new(session.Content)
368-
if w.sniffingConfig != nil {
369-
content.SniffingRequest.Enabled = w.sniffingConfig.Enabled
370-
content.SniffingRequest.OverrideDestinationForProtocol = w.sniffingConfig.DestinationOverride
371-
content.SniffingRequest.ExcludeForDomain = w.sniffingConfig.DomainsExcluded
372-
content.SniffingRequest.MetadataOnly = w.sniffingConfig.MetadataOnly
373-
content.SniffingRequest.RouteOnly = w.sniffingConfig.RouteOnly
374-
}
361+
content.SniffingRequest = w.sniffingRequest
375362
ctx = session.ContextWithContent(ctx, content)
376363
if err := w.proxy.Process(ctx, net.Network_UDP, conn, w.dispatcher); err != nil {
377364
errors.LogInfoInner(ctx, err, "connection ends")
@@ -487,7 +474,7 @@ type dsWorker struct {
487474
stream *internet.MemoryStreamConfig
488475
tag string
489476
dispatcher routing.Dispatcher
490-
sniffingConfig *proxyman.SniffingConfig
477+
sniffingRequest session.SniffingRequest
491478
uplinkCounter stats.Counter
492479
downlinkCounter stats.Counter
493480

@@ -517,13 +504,7 @@ func (w *dsWorker) callback(conn stat.Connection) {
517504
})
518505

519506
content := new(session.Content)
520-
if w.sniffingConfig != nil {
521-
content.SniffingRequest.Enabled = w.sniffingConfig.Enabled
522-
content.SniffingRequest.OverrideDestinationForProtocol = w.sniffingConfig.DestinationOverride
523-
content.SniffingRequest.ExcludeForDomain = w.sniffingConfig.DomainsExcluded
524-
content.SniffingRequest.MetadataOnly = w.sniffingConfig.MetadataOnly
525-
content.SniffingRequest.RouteOnly = w.sniffingConfig.RouteOnly
526-
}
507+
content.SniffingRequest = w.sniffingRequest
527508
ctx = session.ContextWithContent(ctx, content)
528509

529510
if err := w.proxy.Process(ctx, net.Network_UNIX, conn, w.dispatcher); err != nil {

common/session/session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
c "github.com/xtls/xray-core/common/ctx"
99
"github.com/xtls/xray-core/common/errors"
10+
"github.com/xtls/xray-core/common/geodata"
1011
"github.com/xtls/xray-core/common/net"
1112
"github.com/xtls/xray-core/common/protocol"
1213
"github.com/xtls/xray-core/common/signal"
@@ -78,7 +79,7 @@ type Outbound struct {
7879

7980
// SniffingRequest controls the behavior of content sniffing. They are from inbound config. Read-only
8081
type SniffingRequest struct {
81-
ExcludeForDomain []string
82+
ExcludeForDomain geodata.DomainMatcher
8283
OverrideDestinationForProtocol []string
8384
Enabled bool
8485
MetadataOnly bool

infra/conf/fakedns.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,17 @@ func (FakeDNSPostProcessingStage) Process(config *Config) error {
118118
}
119119
}
120120

121-
found := false
122-
// Check if there is a Outbound with necessary sniffer on
123-
var inbounds []InboundDetourConfig
124-
125-
if len(config.InboundConfigs) > 0 {
126-
inbounds = append(inbounds, config.InboundConfigs...)
127-
}
128-
for _, v := range inbounds {
129-
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
130-
for _, dov := range *v.SniffingConfig.DestOverride {
131-
if strings.EqualFold(dov, "fakedns") || strings.EqualFold(dov, "fakedns+others") {
132-
found = true
133-
break
121+
// Check if there is a Inbound with necessary sniffer on
122+
for _, v := range config.InboundConfigs {
123+
if v.SniffingConfig != nil && v.SniffingConfig.Enabled {
124+
for _, d := range v.SniffingConfig.DestOverride {
125+
if strings.EqualFold(d, "fakedns") || strings.EqualFold(d, "fakedns+others") {
126+
return nil
134127
}
135128
}
136129
}
137130
}
138-
if !found {
139-
errors.LogWarning(context.Background(), "Defined FakeDNS but haven't enabled FakeDNS destOverride at any inbound.")
140-
}
131+
errors.LogWarning(context.Background(), "Defined FakeDNS but haven't enabled FakeDNS destOverride at any inbound.")
141132
}
142133

143134
return nil

0 commit comments

Comments
 (0)