Fix Freedom Outbound UDP redirect

已检查 b.UDP 各源头:Mux(VLESS、VMess)、Trojan、Shadowsocks、Socks、TPROXY(Dokodemo-door)
This commit is contained in:
RPRX 2021-02-17 13:37:55 +00:00 committed by GitHub
parent d22c2d034c
commit fed8610d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,13 +97,16 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
return newError("target not specified.") return newError("target not specified.")
} }
destination := outbound.Target destination := outbound.Target
UDPOverride := net.UDPDestination(nil, 0)
if h.config.DestinationOverride != nil { if h.config.DestinationOverride != nil {
server := h.config.DestinationOverride.Server server := h.config.DestinationOverride.Server
if isValidAddress(server.Address) { if isValidAddress(server.Address) {
destination.Address = server.Address.AsAddress() destination.Address = server.Address.AsAddress()
UDPOverride.Address = destination.Address
} }
if server.Port != 0 { if server.Port != 0 {
destination.Port = net.Port(server.Port) destination.Port = net.Port(server.Port)
UDPOverride.Port = destination.Port
} }
} }
newError("opening connection to ", destination).WriteToLog(session.ExportIDToError(ctx)) 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 { if destination.Network == net.Network_TCP {
writer = buf.NewWriter(conn) writer = buf.NewWriter(conn)
} else { } else {
writer = NewPacketWriter(conn, h, ctx) writer = NewPacketWriter(conn, h, ctx, UDPOverride)
} }
if err := buf.Copy(input, writer, buf.UpdateActivity(timer)); err != nil { 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 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 iConn := conn
statConn, ok := iConn.(*internet.StatCouterConnection) statConn, ok := iConn.(*internet.StatCouterConnection)
if ok { if ok {
@ -242,6 +245,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context) buf.Writer
Counter: counter, Counter: counter,
Handler: h, Handler: h,
Context: ctx, Context: ctx,
UDPOverride: UDPOverride,
} }
} }
return &buf.SequentialWriter{Writer: conn} return &buf.SequentialWriter{Writer: conn}
@ -252,6 +256,7 @@ type PacketWriter struct {
stats.Counter stats.Counter
*Handler *Handler
context.Context context.Context
UDPOverride net.Destination
} }
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
@ -264,6 +269,12 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
var n int var n int
var err error var err error
if b.UDP != nil { 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() { if w.Handler.config.useIP() && b.UDP.Address.Family().IsDomain() {
ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil)
if ip != nil { if ip != nil {