1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-29 05:27:02 -05:00

unify NewPacketReader

This commit is contained in:
Darien Raymond 2019-02-07 19:14:37 +01:00
parent 4f08b7c795
commit b234cfb4c4
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 17 additions and 6 deletions
common/buf
proxy
dns
dokodemo
freedom
shadowsocks
socks

View File

@ -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 { func isPacketWriter(writer io.Writer) bool {
if _, ok := writer.(net.PacketConn); ok { if _, ok := writer.(net.PacketConn); ok {
return true return true

View File

@ -131,7 +131,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
} }
} else { } else {
connReader = &dns_proto.UDPReader{ connReader = &dns_proto.UDPReader{
Reader: &buf.PacketReader{Reader: conn}, Reader: buf.NewPacketReader(conn),
} }
connWriter = &dns_proto.UDPWriter{ connWriter = &dns_proto.UDPWriter{
Writer: buf.NewWriter(conn), Writer: buf.NewWriter(conn),

View File

@ -119,7 +119,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
var reader buf.Reader var reader buf.Reader
if dest.Network == net.Network_UDP { if dest.Network == net.Network_UDP {
reader = &buf.PacketReader{Reader: conn} reader = buf.NewPacketReader(conn)
} else { } else {
reader = buf.NewReader(conn) reader = buf.NewReader(conn)
} }
@ -156,7 +156,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
defer tConn.Close() defer tConn.Close()
writer = &buf.SequentialWriter{Writer: tConn} writer = &buf.SequentialWriter{Writer: tConn}
tReader := &buf.PacketReader{Reader: tConn} tReader := buf.NewPacketReader(tConn)
requestCount++ requestCount++
tproxyRequest = func() error { tproxyRequest = func() error {
defer func() { defer func() {

View File

@ -166,7 +166,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 {
reader = buf.NewReader(conn) reader = buf.NewReader(conn)
} else { } else {
reader = &buf.PacketReader{Reader: conn} reader = buf.NewPacketReader(conn)
} }
if err := buf.Copy(reader, output, buf.UpdateActivity(timer)); err != nil { if err := buf.Copy(reader, output, buf.UpdateActivity(timer)); err != nil {
return newError("failed to process response").Base(err) return newError("failed to process response").Base(err)

View File

@ -97,7 +97,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
} }
inbound.User = s.user inbound.User = s.user
reader := &buf.PacketReader{Reader: conn} reader := buf.NewPacketReader(conn)
for { for {
mpayload, err := reader.ReadMultiBuffer() mpayload, err := reader.ReadMultiBuffer()
if err != nil { if err != nil {

View File

@ -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)) newError("client UDP connection from ", inbound.Source).WriteToLog(session.ExportIDToError(ctx))
} }
reader := &buf.PacketReader{Reader: conn} reader := buf.NewPacketReader(conn)
for { for {
mpayload, err := reader.ReadMultiBuffer() mpayload, err := reader.ReadMultiBuffer()
if err != nil { if err != nil {