diff --git a/common/buf/io.go b/common/buf/io.go index 1a56bf75f..938e347bd 100644 --- a/common/buf/io.go +++ b/common/buf/io.go @@ -73,6 +73,17 @@ func NewReader(reader io.Reader) Reader { } } +// NewPacketReader creates a new PacketReader based on the given reader. +func NewPacketReader(reader io.Reader) Reader { + if mr, ok := reader.(Reader); ok { + return mr + } + + return &PacketReader{ + Reader: reader, + } +} + func isPacketWriter(writer io.Writer) bool { if _, ok := writer.(net.PacketConn); ok { return true diff --git a/proxy/dns/dns.go b/proxy/dns/dns.go index 25ef76906..741f128f8 100644 --- a/proxy/dns/dns.go +++ b/proxy/dns/dns.go @@ -131,7 +131,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet. } } else { connReader = &dns_proto.UDPReader{ - Reader: &buf.PacketReader{Reader: conn}, + Reader: buf.NewPacketReader(conn), } connWriter = &dns_proto.UDPWriter{ Writer: buf.NewWriter(conn), diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index 350c0e179..d4302e39c 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -119,7 +119,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in var reader buf.Reader if dest.Network == net.Network_UDP { - reader = &buf.PacketReader{Reader: conn} + reader = buf.NewPacketReader(conn) } else { reader = buf.NewReader(conn) } @@ -156,7 +156,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in defer tConn.Close() writer = &buf.SequentialWriter{Writer: tConn} - tReader := &buf.PacketReader{Reader: tConn} + tReader := buf.NewPacketReader(tConn) requestCount++ tproxyRequest = func() error { defer func() { diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index ee21dd5a6..31397b543 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -166,7 +166,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte if destination.Network == net.Network_TCP { reader = buf.NewReader(conn) } else { - reader = &buf.PacketReader{Reader: conn} + reader = buf.NewPacketReader(conn) } if err := buf.Copy(reader, output, buf.UpdateActivity(timer)); err != nil { return newError("failed to process response").Base(err) diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index cbafb0f94..14cee87a7 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -97,7 +97,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection } inbound.User = s.user - reader := &buf.PacketReader{Reader: conn} + reader := buf.NewPacketReader(conn) for { mpayload, err := reader.ReadMultiBuffer() if err != nil { diff --git a/proxy/socks/server.go b/proxy/socks/server.go index de111b180..ae92b049c 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -200,7 +200,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, newError("client UDP connection from ", inbound.Source).WriteToLog(session.ExportIDToError(ctx)) } - reader := &buf.PacketReader{Reader: conn} + reader := buf.NewPacketReader(conn) for { mpayload, err := reader.ReadMultiBuffer() if err != nil {