@@ -4,13 +4,12 @@ import (
44 "context"
55 "crypto/rand"
66 goerrors "errors"
7+ "github.com/xtls/xray-core/common/dice"
78 "io"
89 "math/big"
910 gonet "net"
1011 "os"
1112
12- "github.com/xtls/xray-core/common/dice"
13-
1413 "github.com/xtls/xray-core/app/proxyman"
1514 "github.com/xtls/xray-core/common"
1615 "github.com/xtls/xray-core/common/buf"
@@ -181,11 +180,7 @@ func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
181180 ob := outbounds [len (outbounds )- 1 ]
182181 content := session .ContentFromContext (ctx )
183182 if h .senderSettings != nil && h .senderSettings .TargetStrategy .HasStrategy () && ob .Target .Address .Family ().IsDomain () && (content == nil || ! content .SkipDNSResolve ) {
184- strategy := h .senderSettings .TargetStrategy
185- if ob .Target .Network == net .Network_UDP && ob .OriginalTarget .Address != nil {
186- strategy = strategy .GetDynamicStrategy (ob .OriginalTarget .Address .Family ())
187- }
188- ips , err := internet .LookupForIP (ob .Target .Address .Domain (), strategy , nil )
183+ ips , err := internet .LookupForIP (ob .Target .Address .Domain (), h .senderSettings .TargetStrategy , nil )
189184 if err != nil {
190185 errors .LogInfoInner (ctx , err , "failed to resolve ip for target " , ob .Target .Address .Domain ())
191186 if h .senderSettings .TargetStrategy .ForceIP () {
@@ -256,6 +251,14 @@ out:
256251 common .Interrupt (link .Reader )
257252}
258253
254+ // Address implements internet.Dialer.
255+ func (h * Handler ) Address () net.Address {
256+ if h .senderSettings == nil || h .senderSettings .Via == nil {
257+ return nil
258+ }
259+ return h .senderSettings .Via .AsAddress ()
260+ }
261+
259262func (h * Handler ) DestIpAddress () net.IP {
260263 return internet .DestIpAddress ()
261264}
@@ -290,16 +293,41 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connecti
290293 return h .getStatCouterConnection (conn ), nil
291294 }
292295
293- errors .LogError (ctx , "failed to get outbound handler with tag: " , tag )
294- return nil , errors .New ("failed to get outbound handler with tag: " + tag )
296+ errors .LogWarning (ctx , "failed to get outbound handler with tag: " , tag )
295297 }
296298
297299 if h .senderSettings .Via != nil {
300+
298301 outbounds := session .OutboundsFromContext (ctx )
299302 ob := outbounds [len (outbounds )- 1 ]
300- h .SetOutboundGateway (ctx , ob )
301- }
303+ var domain string
304+ addr := h .senderSettings .Via .AsAddress ()
305+ domain = h .senderSettings .Via .GetDomain ()
306+ switch {
307+ case h .senderSettings .ViaCidr != "" :
308+ ob .Gateway = ParseRandomIP (addr , h .senderSettings .ViaCidr )
309+
310+ case domain == "origin" :
311+ if inbound := session .InboundFromContext (ctx ); inbound != nil {
312+ if inbound .Local .IsValid () && inbound .Local .Address .Family ().IsIP () {
313+ ob .Gateway = inbound .Local .Address
314+ errors .LogDebug (ctx , "use inbound local ip as sendthrough: " , inbound .Local .Address .String ())
315+ }
316+ }
317+ case domain == "srcip" :
318+ if inbound := session .InboundFromContext (ctx ); inbound != nil {
319+ if inbound .Source .IsValid () && inbound .Source .Address .Family ().IsIP () {
320+ ob .Gateway = inbound .Source .Address
321+ errors .LogDebug (ctx , "use inbound source ip as sendthrough: " , inbound .Source .Address .String ())
322+ }
323+ }
324+ //case addr.Family().IsDomain():
325+ default :
326+ ob .Gateway = addr
327+
328+ }
302329
330+ }
303331 }
304332
305333 if conn , err := h .getUoTConnection (ctx , dest ); err != os .ErrInvalid {
@@ -314,38 +342,6 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connecti
314342 return conn , err
315343}
316344
317- func (h * Handler ) SetOutboundGateway (ctx context.Context , ob * session.Outbound ) {
318- if ob .Gateway == nil && h .senderSettings != nil && h .senderSettings .Via != nil && ! h .senderSettings .ProxySettings .HasTag () && (h .streamSettings .SocketSettings == nil || len (h .streamSettings .SocketSettings .DialerProxy ) == 0 ) {
319- var domain string
320- addr := h .senderSettings .Via .AsAddress ()
321- domain = h .senderSettings .Via .GetDomain ()
322- switch {
323- case h .senderSettings .ViaCidr != "" :
324- ob .Gateway = ParseRandomIP (addr , h .senderSettings .ViaCidr )
325-
326- case domain == "origin" :
327- if inbound := session .InboundFromContext (ctx ); inbound != nil {
328- if inbound .Local .IsValid () && inbound .Local .Address .Family ().IsIP () {
329- ob .Gateway = inbound .Local .Address
330- errors .LogDebug (ctx , "use inbound local ip as sendthrough: " , inbound .Local .Address .String ())
331- }
332- }
333- case domain == "srcip" :
334- if inbound := session .InboundFromContext (ctx ); inbound != nil {
335- if inbound .Source .IsValid () && inbound .Source .Address .Family ().IsIP () {
336- ob .Gateway = inbound .Source .Address
337- errors .LogDebug (ctx , "use inbound source ip as sendthrough: " , inbound .Source .Address .String ())
338- }
339- }
340- //case addr.Family().IsDomain():
341- default :
342- ob .Gateway = addr
343-
344- }
345-
346- }
347- }
348-
349345func (h * Handler ) getStatCouterConnection (conn stat.Connection ) stat.Connection {
350346 if h .uplinkCounter != nil || h .downlinkCounter != nil {
351347 return & stat.CounterConnection {
0 commit comments