@@ -4,7 +4,6 @@ package dispatcher
4
4
5
5
import (
6
6
"context"
7
- "fmt"
8
7
"strings"
9
8
"sync"
10
9
"time"
@@ -135,77 +134,10 @@ func (*DefaultDispatcher) Start() error {
135
134
// Close implements common.Closable.
136
135
func (* DefaultDispatcher ) Close () error { return nil }
137
136
138
- func (d * DefaultDispatcher ) getLink (ctx context.Context , network net.Network , sniffing session.SniffingRequest ) (* transport.Link , * transport.Link ) {
139
- downOpt := pipe .OptionsFromContext (ctx )
140
- upOpt := downOpt
141
-
142
- if network == net .Network_UDP {
143
- var ip2domain * sync.Map // net.IP.String() => domain, this map is used by server side when client turn on fakedns
144
- // Client will send domain address in the buffer.UDP.Address, server record all possible target IP addrs.
145
- // When target replies, server will restore the domain and send back to client.
146
- // Note: this map is not global but per connection context
147
- upOpt = append (upOpt , pipe .OnTransmission (func (mb buf.MultiBuffer ) buf.MultiBuffer {
148
- for i , buffer := range mb {
149
- if buffer .UDP == nil {
150
- continue
151
- }
152
- addr := buffer .UDP .Address
153
- if addr .Family ().IsIP () {
154
- if fkr0 , ok := d .fdns .(dns.FakeDNSEngineRev0 ); ok && fkr0 .IsIPInIPPool (addr ) && sniffing .Enabled {
155
- domain := fkr0 .GetDomainFromFakeDNS (addr )
156
- if len (domain ) > 0 {
157
- buffer .UDP .Address = net .DomainAddress (domain )
158
- newError ("[fakedns client] override with domain: " , domain , " for xUDP buffer at " , i ).WriteToLog (session .ExportIDToError (ctx ))
159
- } else {
160
- newError ("[fakedns client] failed to find domain! :" , addr .String (), " for xUDP buffer at " , i ).AtWarning ().WriteToLog (session .ExportIDToError (ctx ))
161
- }
162
- }
163
- } else {
164
- if ip2domain == nil {
165
- ip2domain = new (sync.Map )
166
- newError ("[fakedns client] create a new map" ).WriteToLog (session .ExportIDToError (ctx ))
167
- }
168
- domain := addr .Domain ()
169
- ips , err := d .dns .LookupIP (domain , dns.IPOption {true , true , false })
170
- if err == nil {
171
- for _ , ip := range ips {
172
- ip2domain .Store (ip .String (), domain )
173
- }
174
- newError ("[fakedns client] candidate ip: " + fmt .Sprintf ("%v" , ips ), " for xUDP buffer at " , i ).WriteToLog (session .ExportIDToError (ctx ))
175
- } else {
176
- newError ("[fakedns client] failed to look up IP for " , domain , " for xUDP buffer at " , i ).Base (err ).WriteToLog (session .ExportIDToError (ctx ))
177
- }
178
- }
179
- }
180
- return mb
181
- }))
182
- downOpt = append (downOpt , pipe .OnTransmission (func (mb buf.MultiBuffer ) buf.MultiBuffer {
183
- for i , buffer := range mb {
184
- if buffer .UDP == nil {
185
- continue
186
- }
187
- addr := buffer .UDP .Address
188
- if addr .Family ().IsIP () {
189
- if ip2domain == nil {
190
- continue
191
- }
192
- if domain , found := ip2domain .Load (addr .IP ().String ()); found {
193
- buffer .UDP .Address = net .DomainAddress (domain .(string ))
194
- newError ("[fakedns client] restore domain: " , domain .(string ), " for xUDP buffer at " , i ).WriteToLog (session .ExportIDToError (ctx ))
195
- }
196
- } else {
197
- if fkr0 , ok := d .fdns .(dns.FakeDNSEngineRev0 ); ok {
198
- fakeIp := fkr0 .GetFakeIPForDomain (addr .Domain ())
199
- buffer .UDP .Address = fakeIp [0 ]
200
- newError ("[fakedns client] restore FakeIP: " , buffer .UDP , fmt .Sprintf ("%v" , fakeIp ), " for xUDP buffer at " , i ).WriteToLog (session .ExportIDToError (ctx ))
201
- }
202
- }
203
- }
204
- return mb
205
- }))
206
- }
207
- uplinkReader , uplinkWriter := pipe .New (upOpt ... )
208
- downlinkReader , downlinkWriter := pipe .New (downOpt ... )
137
+ func (d * DefaultDispatcher ) getLink (ctx context.Context ) (* transport.Link , * transport.Link ) {
138
+ opt := pipe .OptionsFromContext (ctx )
139
+ uplinkReader , uplinkWriter := pipe .New (opt ... )
140
+ downlinkReader , downlinkWriter := pipe .New (opt ... )
209
141
210
142
inboundLink := & transport.Link {
211
143
Reader : downlinkReader ,
@@ -263,7 +195,7 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu
263
195
protocolString = resComp .ProtocolForDomainResult ()
264
196
}
265
197
for _ , p := range request .OverrideDestinationForProtocol {
266
- if strings .HasPrefix (protocolString , p ) {
198
+ if strings .HasPrefix (protocolString , p ) || strings . HasPrefix ( protocolString , p ) {
267
199
return true
268
200
}
269
201
if fkr0 , ok := d .fdns .(dns.FakeDNSEngineRev0 ); ok && protocolString != "bittorrent" && p == "fakedns" &&
@@ -287,17 +219,17 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
287
219
panic ("Dispatcher: Invalid destination." )
288
220
}
289
221
ob := & session.Outbound {
290
- Target : destination ,
222
+ OriginalTarget : destination ,
223
+ Target : destination ,
291
224
}
292
225
ctx = session .ContextWithOutbound (ctx , ob )
293
226
content := session .ContentFromContext (ctx )
294
227
if content == nil {
295
228
content = new (session.Content )
296
229
ctx = session .ContextWithContent (ctx , content )
297
230
}
298
-
299
231
sniffingRequest := content .SniffingRequest
300
- inbound , outbound := d .getLink (ctx , destination . Network , sniffingRequest )
232
+ inbound , outbound := d .getLink (ctx )
301
233
if ! sniffingRequest .Enabled {
302
234
go d .routedDispatch (ctx , outbound , destination )
303
235
} else {
@@ -314,7 +246,15 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
314
246
domain := result .Domain ()
315
247
newError ("sniffed domain: " , domain ).WriteToLog (session .ExportIDToError (ctx ))
316
248
destination .Address = net .ParseAddress (domain )
317
- if sniffingRequest .RouteOnly && result .Protocol () != "fakedns" {
249
+ protocol := result .Protocol ()
250
+ if resComp , ok := result .(SnifferResultComposite ); ok {
251
+ protocol = resComp .ProtocolForDomainResult ()
252
+ }
253
+ isFakeIP := false
254
+ if fkr0 , ok := d .fdns .(dns.FakeDNSEngineRev0 ); ok && ob .Target .Address .Family ().IsIP () && fkr0 .IsIPInIPPool (ob .Target .Address ) {
255
+ isFakeIP = true
256
+ }
257
+ if sniffingRequest .RouteOnly && protocol != "fakedns" && protocol != "fakedns+others" && ! isFakeIP {
318
258
ob .RouteTarget = destination
319
259
} else {
320
260
ob .Target = destination
@@ -332,7 +272,8 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
332
272
return newError ("Dispatcher: Invalid destination." )
333
273
}
334
274
ob := & session.Outbound {
335
- Target : destination ,
275
+ OriginalTarget : destination ,
276
+ Target : destination ,
336
277
}
337
278
ctx = session .ContextWithOutbound (ctx , ob )
338
279
content := session .ContentFromContext (ctx )
@@ -356,7 +297,15 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
356
297
domain := result .Domain ()
357
298
newError ("sniffed domain: " , domain ).WriteToLog (session .ExportIDToError (ctx ))
358
299
destination .Address = net .ParseAddress (domain )
359
- if sniffingRequest .RouteOnly && result .Protocol () != "fakedns" {
300
+ protocol := result .Protocol ()
301
+ if resComp , ok := result .(SnifferResultComposite ); ok {
302
+ protocol = resComp .ProtocolForDomainResult ()
303
+ }
304
+ isFakeIP := false
305
+ if fkr0 , ok := d .fdns .(dns.FakeDNSEngineRev0 ); ok && ob .Target .Address .Family ().IsIP () && fkr0 .IsIPInIPPool (ob .Target .Address ) {
306
+ isFakeIP = true
307
+ }
308
+ if sniffingRequest .RouteOnly && protocol != "fakedns" && protocol != "fakedns+others" && ! isFakeIP {
360
309
ob .RouteTarget = destination
361
310
} else {
362
311
ob .Target = destination
0 commit comments