diff --git a/adapter/inbound/http.go b/adapter/inbound/http.go index 94040078..b1b881ce 100644 --- a/adapter/inbound/http.go +++ b/adapter/inbound/http.go @@ -16,11 +16,11 @@ func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Ad for _, addition := range additions { addition.Apply(metadata) } - if ip, port, err := parseAddr(source.String()); err == nil { + if ip, port, err := parseAddr(source); err == nil { metadata.SrcIP = ip metadata.SrcPort = port } - if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil { + if ip, port, err := parseAddr(conn.LocalAddr()); err == nil { metadata.InIP = ip metadata.InPort = port } diff --git a/adapter/inbound/https.go b/adapter/inbound/https.go index 3fff2371..485e72bb 100644 --- a/adapter/inbound/https.go +++ b/adapter/inbound/https.go @@ -15,11 +15,11 @@ func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) *cont for _, addition := range additions { addition.Apply(metadata) } - if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil { + if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil { metadata.SrcIP = ip metadata.SrcPort = port } - if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil { + if ip, port, err := parseAddr(conn.LocalAddr()); err == nil { metadata.InIP = ip metadata.InPort = port } diff --git a/adapter/inbound/packet.go b/adapter/inbound/packet.go index d1fcb05d..44e5e1a7 100644 --- a/adapter/inbound/packet.go +++ b/adapter/inbound/packet.go @@ -24,12 +24,12 @@ func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions for _, addition := range additions { addition.Apply(metadata) } - if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil { + if ip, port, err := parseAddr(packet.LocalAddr()); err == nil { metadata.SrcIP = ip metadata.SrcPort = port } if p, ok := packet.(C.UDPPacketInAddr); ok { - if ip, port, err := parseAddr(p.InAddr().String()); err == nil { + if ip, port, err := parseAddr(p.InAddr()); err == nil { metadata.InIP = ip metadata.InPort = port } diff --git a/adapter/inbound/socket.go b/adapter/inbound/socket.go index 557b22ab..590f64d7 100644 --- a/adapter/inbound/socket.go +++ b/adapter/inbound/socket.go @@ -18,22 +18,13 @@ func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Ad addition.Apply(metadata) } - remoteAddr := conn.RemoteAddr() - - // Filter when net.Addr interface is nil - if remoteAddr != nil { - if ip, port, err := parseAddr(remoteAddr.String()); err == nil { - metadata.SrcIP = ip - metadata.SrcPort = port - } + if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil { + metadata.SrcIP = ip + metadata.SrcPort = port } - localAddr := conn.LocalAddr() - // Filter when net.Addr interface is nil - if localAddr != nil { - if ip, port, err := parseAddr(localAddr.String()); err == nil { - metadata.InIP = ip - metadata.InPort = port - } + if ip, port, err := parseAddr(conn.LocalAddr()); err == nil { + metadata.InIP = ip + metadata.InPort = port } return context.NewConnContext(conn, metadata) diff --git a/adapter/inbound/util.go b/adapter/inbound/util.go index cddbcf1f..88e989f9 100644 --- a/adapter/inbound/util.go +++ b/adapter/inbound/util.go @@ -1,13 +1,14 @@ package inbound import ( - "github.com/Dreamacro/clash/common/nnip" + "errors" "net" "net/http" "net/netip" "strconv" "strings" + "github.com/Dreamacro/clash/common/nnip" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/socks5" ) @@ -57,8 +58,19 @@ func parseHTTPAddr(request *http.Request) *C.Metadata { return metadata } -func parseAddr(addr string) (netip.Addr, string, error) { - host, port, err := net.SplitHostPort(addr) +func parseAddr(addr net.Addr) (netip.Addr, string, error) { + // Filter when net.Addr interface is nil + if addr == nil { + return netip.Addr{}, "", errors.New("nil addr") + } + if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok { + ip, port, err := parseAddr(rawAddr.RawAddr()) + if err == nil { + return ip, port, err + } + } + addrStr := addr.String() + host, port, err := net.SplitHostPort(addrStr) if err != nil { return netip.Addr{}, "", err } diff --git a/transport/tuic/client.go b/transport/tuic/client.go index 1d1c3d15..d3f511df 100644 --- a/transport/tuic/client.go +++ b/transport/tuic/client.go @@ -361,7 +361,6 @@ func (t *clientImpl) ListenPacketWithDialer(ctx context.Context, metadata *C.Met pc := &quicStreamPacketConn{ connId: connId, quicConn: quicConn, - lAddr: quicConn.LocalAddr(), inputConn: N.NewBufferedConn(pipe2), udpRelayMode: t.UdpRelayMode, maxUdpRelayPacketSize: t.MaxUdpRelayPacketSize, diff --git a/transport/tuic/conn.go b/transport/tuic/conn.go index a6baf451..7ecc3f0d 100644 --- a/transport/tuic/conn.go +++ b/transport/tuic/conn.go @@ -109,7 +109,6 @@ var _ net.Conn = &quicStreamConn{} type quicStreamPacketConn struct { connId uint32 quicConn quic.Connection - lAddr net.Addr inputConn *N.BufferedConn udpRelayMode string @@ -251,7 +250,7 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro } func (q *quicStreamPacketConn) LocalAddr() net.Addr { - return q.lAddr + return q.quicConn.LocalAddr() } var _ net.PacketConn = &quicStreamPacketConn{} diff --git a/transport/tuic/server.go b/transport/tuic/server.go index 5968e3e1..2830b324 100644 --- a/transport/tuic/server.go +++ b/transport/tuic/server.go @@ -140,7 +140,6 @@ func (s *serverHandler) parsePacket(packet Packet, udpRelayMode string) (err err pc := &quicStreamPacketConn{ connId: assocId, quicConn: s.quicConn, - lAddr: s.quicConn.LocalAddr(), inputConn: nil, udpRelayMode: udpRelayMode, maxUdpRelayPacketSize: s.MaxUdpRelayPacketSize, @@ -152,12 +151,12 @@ func (s *serverHandler) parsePacket(packet Packet, udpRelayMode string) (err err return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{ pc: pc, packet: &packet, - rAddr: s.genServerAssocIdAddr(assocId), + rAddr: s.genServerAssocIdAddr(assocId, s.quicConn.RemoteAddr()), }) } -func (s *serverHandler) genServerAssocIdAddr(assocId uint32) net.Addr { - return ServerAssocIdAddr(fmt.Sprintf("tuic-%s-%d", s.uuid.String(), assocId)) +func (s *serverHandler) genServerAssocIdAddr(assocId uint32, addr net.Addr) net.Addr { + return &ServerAssocIdAddr{assocId: fmt.Sprintf("tuic-%s-%d", s.uuid.String(), assocId), addr: addr} } func (s *serverHandler) handleStream() (err error) { @@ -274,14 +273,21 @@ func (s *serverHandler) handleUniStream() (err error) { } } -type ServerAssocIdAddr string +type ServerAssocIdAddr struct { + assocId string + addr net.Addr +} func (a ServerAssocIdAddr) Network() string { return "ServerAssocIdAddr" } func (a ServerAssocIdAddr) String() string { - return string(a) + return a.assocId +} + +func (a ServerAssocIdAddr) RawAddr() net.Addr { + return a.addr } type serverUDPPacket struct {