Skip to content

Commit 72bba44

Browse files
committed
Revert "Some refines related to direct/freedom and targetStrategy; More intelligent "useIP"/"ForceIP", enhance "origin" functionality (XTLS#5030)"
This reverts commit 6d1695a.
1 parent 33272a0 commit 72bba44

File tree

11 files changed

+347
-245
lines changed

11 files changed

+347
-245
lines changed

app/proxyman/inbound/worker.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,10 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
322322
outbounds[0].Target = originalDest
323323
}
324324
ctx = session.ContextWithOutbounds(ctx, outbounds)
325-
local := net.DestinationFromAddr(w.hub.Addr())
326-
if local.Address == net.AnyIP || local.Address == net.AnyIPv6 {
327-
if source.Address.Family().IsIPv4() {
328-
local.Address = net.AnyIP
329-
} else if source.Address.Family().IsIPv6() {
330-
local.Address = net.AnyIPv6
331-
}
332-
}
333325

334326
ctx = session.ContextWithInbound(ctx, &session.Inbound{
335327
Source: source,
336-
Local: local, // Due to some limitations, in UDP connections, localIP is always equal to listen interface IP
328+
Local: net.DestinationFromAddr(w.hub.Addr()), // Due to some limitations, in UDP connections, localIP is always equal to listen interface IP
337329
Gateway: net.UDPDestination(w.address, w.port),
338330
Tag: w.tag,
339331
})

app/proxyman/outbound/handler.go

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
259262
func (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-
349345
func (h *Handler) getStatCouterConnection(conn stat.Connection) stat.Connection {
350346
if h.uplinkCounter != nil || h.downlinkCounter != nil {
351347
return &stat.CounterConnection{

infra/conf/freedom.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/xtls/xray-core/common/protocol"
1212
"github.com/xtls/xray-core/proxy/freedom"
1313
"google.golang.org/protobuf/proto"
14-
"github.com/xtls/xray-core/transport/internet"
1514
)
1615

1716
type FreedomConfig struct {
@@ -48,27 +47,27 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
4847
}
4948
switch strings.ToLower(targetStrategy) {
5049
case "asis", "":
51-
config.DomainStrategy = internet.DomainStrategy_AS_IS
50+
config.DomainStrategy = freedom.Config_AS_IS
5251
case "useip":
53-
config.DomainStrategy = internet.DomainStrategy_USE_IP
52+
config.DomainStrategy = freedom.Config_USE_IP
5453
case "useipv4":
55-
config.DomainStrategy = internet.DomainStrategy_USE_IP4
54+
config.DomainStrategy = freedom.Config_USE_IP4
5655
case "useipv6":
57-
config.DomainStrategy = internet.DomainStrategy_USE_IP6
56+
config.DomainStrategy = freedom.Config_USE_IP6
5857
case "useipv4v6":
59-
config.DomainStrategy = internet.DomainStrategy_USE_IP46
58+
config.DomainStrategy = freedom.Config_USE_IP46
6059
case "useipv6v4":
61-
config.DomainStrategy = internet.DomainStrategy_USE_IP64
60+
config.DomainStrategy = freedom.Config_USE_IP64
6261
case "forceip":
63-
config.DomainStrategy = internet.DomainStrategy_FORCE_IP
62+
config.DomainStrategy = freedom.Config_FORCE_IP
6463
case "forceipv4":
65-
config.DomainStrategy = internet.DomainStrategy_FORCE_IP4
64+
config.DomainStrategy = freedom.Config_FORCE_IP4
6665
case "forceipv6":
67-
config.DomainStrategy = internet.DomainStrategy_FORCE_IP6
66+
config.DomainStrategy = freedom.Config_FORCE_IP6
6867
case "forceipv4v6":
69-
config.DomainStrategy = internet.DomainStrategy_FORCE_IP46
68+
config.DomainStrategy = freedom.Config_FORCE_IP46
7069
case "forceipv6v4":
71-
config.DomainStrategy = internet.DomainStrategy_FORCE_IP64
70+
config.DomainStrategy = freedom.Config_FORCE_IP64
7271
default:
7372
return nil, errors.New("unsupported domain strategy: ", targetStrategy)
7473
}

infra/conf/freedom_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/xtls/xray-core/common/protocol"
88
. "github.com/xtls/xray-core/infra/conf"
99
"github.com/xtls/xray-core/proxy/freedom"
10-
"github.com/xtls/xray-core/transport/internet"
1110
)
1211

1312
func TestFreedomConfig(t *testing.T) {
@@ -24,7 +23,7 @@ func TestFreedomConfig(t *testing.T) {
2423
}`,
2524
Parser: loadJSON(creator),
2625
Output: &freedom.Config{
27-
DomainStrategy: internet.DomainStrategy_AS_IS,
26+
DomainStrategy: freedom.Config_AS_IS,
2827
DestinationOverride: &freedom.DestinationOverride{
2928
Server: &protocol.ServerEndpoint{
3029
Address: &net.IPOrDomain{

proxy/freedom/config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
11
package freedom
2+
3+
var strategy = [][]byte{
4+
// name strategy, prefer, fallback
5+
{0, 0, 0}, // AsIs none, /, /
6+
{1, 0, 0}, // UseIP use, both, none
7+
{1, 4, 0}, // UseIPv4 use, 4, none
8+
{1, 6, 0}, // UseIPv6 use, 6, none
9+
{1, 4, 6}, // UseIPv4v6 use, 4, 6
10+
{1, 6, 4}, // UseIPv6v4 use, 6, 4
11+
{2, 0, 0}, // ForceIP force, both, none
12+
{2, 4, 0}, // ForceIPv4 force, 4, none
13+
{2, 6, 0}, // ForceIPv6 force, 6, none
14+
{2, 4, 6}, // ForceIPv4v6 force, 4, 6
15+
{2, 6, 4}, // ForceIPv6v4 force, 6, 4
16+
}
17+
18+
func (c *Config) hasStrategy() bool {
19+
return strategy[c.DomainStrategy][0] != 0
20+
}
21+
22+
func (c *Config) forceIP() bool {
23+
return strategy[c.DomainStrategy][0] == 2
24+
}
25+
26+
func (c *Config) preferIP4() bool {
27+
return strategy[c.DomainStrategy][1] == 4 || strategy[c.DomainStrategy][1] == 0
28+
}
29+
30+
func (c *Config) preferIP6() bool {
31+
return strategy[c.DomainStrategy][1] == 6 || strategy[c.DomainStrategy][1] == 0
32+
}
33+
34+
func (c *Config) hasFallback() bool {
35+
return strategy[c.DomainStrategy][2] != 0
36+
}
37+
38+
func (c *Config) fallbackIP4() bool {
39+
return strategy[c.DomainStrategy][2] == 4
40+
}
41+
42+
func (c *Config) fallbackIP6() bool {
43+
return strategy[c.DomainStrategy][2] == 6
44+
}

0 commit comments

Comments
 (0)