diff --git a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go index bdbbef8ec..046c86b23 100644 --- a/app/proxyman/outbound/handler.go +++ b/app/proxyman/outbound/handler.go @@ -202,7 +202,7 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Conn outbound.Gateway = h.senderSettings.Via.AsAddress() } } - var enablePacketAddrCapture = true + enablePacketAddrCapture := true if h.senderSettings != nil && h.senderSettings.ProxySettings != nil && h.senderSettings.ProxySettings.HasTag() && h.senderSettings.ProxySettings.TransportLayerProxy { tag := h.senderSettings.ProxySettings.Tag newError("transport layer proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx)) diff --git a/common/net/packetaddr/connection_adaptor.go b/common/net/packetaddr/connection_adaptor.go index a95b1b12f..4364a6d33 100644 --- a/common/net/packetaddr/connection_adaptor.go +++ b/common/net/packetaddr/connection_adaptor.go @@ -6,16 +6,18 @@ import ( "sync" "time" - "github.com/v2fly/v2ray-core/v4/common" - "github.com/v2fly/v2ray-core/v4/common/buf" - "github.com/v2fly/v2ray-core/v4/common/errors" - "github.com/v2fly/v2ray-core/v4/common/net" - "github.com/v2fly/v2ray-core/v4/features/routing" - "github.com/v2fly/v2ray-core/v4/transport" + "github.com/v2fly/v2ray-core/v5/common" + "github.com/v2fly/v2ray-core/v5/common/buf" + "github.com/v2fly/v2ray-core/v5/common/errors" + "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/features/routing" + "github.com/v2fly/v2ray-core/v5/transport" ) -var errNotPacketConn = errors.New("not a packet connection") -var errUnsupported = errors.New("unsupported action") +var ( + errNotPacketConn = errors.New("not a packet connection") + errUnsupported = errors.New("unsupported action") +) func ToPacketAddrConn(link *transport.Link, dest net.Destination) (net.PacketConn, error) { if !dest.Address.Family().IsDomain() { @@ -80,6 +82,9 @@ func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err payloadLen := len(p) var buffer *buf.Buffer buffer, err = AttachAddressToPacket(buf.FromBytes(p), addr) + if err != nil { + return 0, err + } mb := buf.MultiBuffer{buffer} err = c.link.Writer.WriteMultiBuffer(mb) if err != nil { diff --git a/common/net/packetaddr/packetaddr.go b/common/net/packetaddr/packetaddr.go index b1add37ff..58e58675c 100644 --- a/common/net/packetaddr/packetaddr.go +++ b/common/net/packetaddr/packetaddr.go @@ -2,10 +2,11 @@ package packetaddr import ( "bytes" - "github.com/v2fly/v2ray-core/v4/common/buf" - "github.com/v2fly/v2ray-core/v4/common/net" - "github.com/v2fly/v2ray-core/v4/common/protocol" gonet "net" + + "github.com/v2fly/v2ray-core/v5/common/buf" + "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/common/protocol" ) var addrParser = protocol.NewAddressParser( @@ -44,7 +45,7 @@ func ExtractAddressFromPacket(data *buf.Buffer) (*buf.Buffer, gonet.Addr, error) if err != nil { return nil, nil, err } - var addr = &gonet.UDPAddr{ + addr := &gonet.UDPAddr{ IP: address.IP(), Port: int(port.Value()), Zone: "", diff --git a/common/net/packetaddr/packetaddr_test.go b/common/net/packetaddr/packetaddr_test.go index e324ce2fe..e3cc8c0f2 100644 --- a/common/net/packetaddr/packetaddr_test.go +++ b/common/net/packetaddr/packetaddr_test.go @@ -1,10 +1,12 @@ package packetaddr import ( - "github.com/stretchr/testify/assert" - "github.com/v2fly/v2ray-core/v4/common/buf" sysnet "net" "testing" + + "github.com/stretchr/testify/assert" + + "github.com/v2fly/v2ray-core/v4/common/buf" ) func TestPacketEncodingIPv4(t *testing.T) { diff --git a/proxy/socks/client.go b/proxy/socks/client.go index bf1662378..3e2c38e99 100644 --- a/proxy/socks/client.go +++ b/proxy/socks/client.go @@ -2,9 +2,10 @@ package socks import ( "context" + "time" + "github.com/v2fly/v2ray-core/v4/common/net/packetaddr" "github.com/v2fly/v2ray-core/v4/transport/internet/udp" - "time" core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/common" diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 4bfdbef5d..32c8c690b 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -390,9 +390,9 @@ func (r *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return buf.MultiBuffer{b}, nil } -func (v *UDPReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { +func (r *UDPReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { buffer := buf.New() - _, err = buffer.ReadFrom(v.reader) + _, err = buffer.ReadFrom(r.reader) if err != nil { buffer.Release() return 0, nil, err diff --git a/proxy/socks/server.go b/proxy/socks/server.go index a7937179d..f333e648d 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -2,7 +2,6 @@ package socks import ( "context" - "github.com/v2fly/v2ray-core/v4/common/net/packetaddr" "io" "time" @@ -11,6 +10,7 @@ import ( "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/log" "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" "github.com/v2fly/v2ray-core/v5/common/protocol" udp_proto "github.com/v2fly/v2ray-core/v5/common/protocol/udp" "github.com/v2fly/v2ray-core/v5/common/session" diff --git a/transport/internet/udp/copy.go b/transport/internet/udp/copy.go index f5f83e38d..fdc3ce58c 100644 --- a/transport/internet/udp/copy.go +++ b/transport/internet/udp/copy.go @@ -3,8 +3,8 @@ package udp import ( gonet "net" - "github.com/v2fly/v2ray-core/v4/common/signal" - "github.com/v2fly/v2ray-core/v4/transport/internet" + "github.com/v2fly/v2ray-core/v5/common/signal" + "github.com/v2fly/v2ray-core/v5/transport/internet" ) type dataHandler func(content []byte, address gonet.Addr) @@ -31,7 +31,7 @@ func CopyPacketConn(dst internet.AbstractPacketConnWriter, src internet.Abstract handler(buffer[:n], addr) } - n, err = dst.WriteTo(buffer[:n], addr) + _, err = dst.WriteTo(buffer[:n], addr) if err != nil { return err } diff --git a/transport/internet/udp/dispatcher.go b/transport/internet/udp/dispatcher.go index 26ae09fa0..c52021c6f 100644 --- a/transport/internet/udp/dispatcher.go +++ b/transport/internet/udp/dispatcher.go @@ -2,6 +2,7 @@ package udp import ( "context" + "github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/net" diff --git a/transport/internet/udp/dispatcher_packetaddr.go b/transport/internet/udp/dispatcher_packetaddr.go index 0785eb1f9..ac1a5595f 100644 --- a/transport/internet/udp/dispatcher_packetaddr.go +++ b/transport/internet/udp/dispatcher_packetaddr.go @@ -2,11 +2,12 @@ package udp import ( "context" - "github.com/v2fly/v2ray-core/v4/common/buf" - "github.com/v2fly/v2ray-core/v4/common/net" - "github.com/v2fly/v2ray-core/v4/common/net/packetaddr" - "github.com/v2fly/v2ray-core/v4/common/protocol/udp" - "github.com/v2fly/v2ray-core/v4/features/routing" + + "github.com/v2fly/v2ray-core/v5/common/buf" + "github.com/v2fly/v2ray-core/v5/common/net" + "github.com/v2fly/v2ray-core/v5/common/net/packetaddr" + "github.com/v2fly/v2ray-core/v5/common/protocol/udp" + "github.com/v2fly/v2ray-core/v5/features/routing" ) type PacketAddrDispatcher struct { @@ -25,6 +26,7 @@ func (p PacketAddrDispatcher) Dispatch(ctx context.Context, destination net.Dest } p.conn.WriteTo(payload.Bytes(), &net.UDPAddr{IP: destination.Address.IP(), Port: int(destination.Port.Value())}) } + func (p PacketAddrDispatcher) readWorker() { for { readBuf := buf.New()