From 89792aee9d9bcad42b6fd70a703632902f6c25f4 Mon Sep 17 00:00:00 2001 From: lastrise <54949052+lastrise@users.noreply.github.com> Date: Fri, 21 Feb 2025 00:15:59 +0400 Subject: [PATCH] Outbound: Add outbound sendThrough origin behavior (#4349) * added support of sending through origin for outbounds * added strings package import * usage of net.SplitHostPort instead of manual splitting --------- Co-authored-by: poly --- app/proxyman/outbound/handler.go | 11 ++++++++++- infra/conf/xray.go | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go index df7d52a4..3819f52b 100644 --- a/app/proxyman/outbound/handler.go +++ b/app/proxyman/outbound/handler.go @@ -273,7 +273,16 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connecti outbounds := session.OutboundsFromContext(ctx) ob := outbounds[len(outbounds)-1] if h.senderSettings.ViaCidr == "" { - ob.Gateway = h.senderSettings.Via.AsAddress() + if h.senderSettings.Via.AsAddress().Family().IsDomain() && h.senderSettings.Via.AsAddress().Domain() == "origin" { + if inbound := session.InboundFromContext(ctx); inbound != nil { + origin, _, err := net.SplitHostPort(inbound.Conn.LocalAddr().String()) + if err == nil { + ob.Gateway = net.ParseAddress(origin) + } + } + } else { + ob.Gateway = h.senderSettings.Via.AsAddress() + } } else { //Get a random address. ob.Gateway = ParseRandomIPv6(h.senderSettings.Via.AsAddress(), h.senderSettings.ViaCidr) } diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 4de55bad..a9cc88bc 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -292,7 +292,9 @@ func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) { senderSettings.ViaCidr = strings.Split(*c.SendThrough, "/")[1] } else { if address.Family().IsDomain() { - return nil, errors.New("unable to send through: " + address.String()) + if address.Address.Domain() != "origin" { + return nil, errors.New("unable to send through: " + address.String()) + } } } senderSettings.Via = address.Build()