From fed8610d3f778242635ce0f1e528620b8eae85f0 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Wed, 17 Feb 2021 13:37:55 +0000 Subject: [PATCH] Fix Freedom Outbound UDP redirect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已检查 b.UDP 各源头:Mux(VLESS、VMess)、Trojan、Shadowsocks、Socks、TPROXY(Dokodemo-door) --- proxy/freedom/freedom.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index fc955501..7a8c336a 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -97,13 +97,16 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte return newError("target not specified.") } destination := outbound.Target + UDPOverride := net.UDPDestination(nil, 0) if h.config.DestinationOverride != nil { server := h.config.DestinationOverride.Server if isValidAddress(server.Address) { destination.Address = server.Address.AsAddress() + UDPOverride.Address = destination.Address } if server.Port != 0 { destination.Port = net.Port(server.Port) + UDPOverride.Port = destination.Port } } newError("opening connection to ", destination).WriteToLog(session.ExportIDToError(ctx)) @@ -149,7 +152,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte if destination.Network == net.Network_TCP { writer = buf.NewWriter(conn) } else { - writer = NewPacketWriter(conn, h, ctx) + writer = NewPacketWriter(conn, h, ctx, UDPOverride) } if err := buf.Copy(input, writer, buf.UpdateActivity(timer)); err != nil { @@ -226,7 +229,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return buf.MultiBuffer{b}, nil } -func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context) buf.Writer { +func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride net.Destination) buf.Writer { iConn := conn statConn, ok := iConn.(*internet.StatCouterConnection) if ok { @@ -242,6 +245,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context) buf.Writer Counter: counter, Handler: h, Context: ctx, + UDPOverride: UDPOverride, } } return &buf.SequentialWriter{Writer: conn} @@ -252,6 +256,7 @@ type PacketWriter struct { stats.Counter *Handler context.Context + UDPOverride net.Destination } func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { @@ -264,6 +269,12 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { var n int var err error if b.UDP != nil { + if w.UDPOverride.Address != nil { + b.UDP.Address = w.UDPOverride.Address + } + if w.UDPOverride.Port != 0 { + b.UDP.Port = w.UDPOverride.Port + } if w.Handler.config.useIP() && b.UDP.Address.Family().IsDomain() { ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) if ip != nil {