From 57966adf49a4a0f4194f17ce622d83d7e05ad7e2 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 17 Aug 2018 20:54:25 +0200 Subject: [PATCH] revert reader creation based on per-connection buffer --- proxy/dokodemo/dokodemo.go | 8 +------- proxy/freedom/freedom.go | 8 +------- proxy/http/server.go | 8 +------- proxy/shadowsocks/server.go | 11 +---------- proxy/socks/client.go | 8 +------- proxy/socks/server.go | 12 +----------- proxy/vmess/inbound/inbound.go | 5 +---- proxy/vmess/outbound/outbound.go | 11 +---------- 8 files changed, 8 insertions(+), 63 deletions(-) diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index 609a453e3..cf382fcfe 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -92,13 +92,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in requestDone := func() error { defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly) - var reader buf.Reader - if plcy.Buffer.PerConnection == 0 { - reader = &buf.SingleReader{Reader: conn} - } else { - reader = buf.NewReader(conn) - } - + reader := buf.NewReader(conn) if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil { return newError("failed to transport request").Base(err) } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index e5a449891..7f1044e93 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -141,13 +141,7 @@ func (h *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia responseDone := func() error { defer timer.SetTimeout(plcy.Timeouts.UplinkOnly) - var reader buf.Reader - if plcy.Buffer.PerConnection == 0 { - reader = &buf.SingleReader{Reader: conn} - } else { - reader = buf.NewReader(conn) - } - if err := buf.Copy(reader, output, buf.UpdateActivity(timer)); err != nil { + if err := buf.Copy(buf.NewReader(conn), output, buf.UpdateActivity(timer)); err != nil { return newError("failed to process response").Base(err) } diff --git a/proxy/http/server.go b/proxy/http/server.go index ad2938bf9..a3e1e77d4 100755 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -196,13 +196,7 @@ func (s *Server) handleConnect(ctx context.Context, request *http.Request, reade requestDone := func() error { defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly) - var reader buf.Reader - if plcy.Buffer.PerConnection == 0 { - reader = &buf.SingleReader{Reader: conn} - } else { - reader = buf.NewReader(conn) - } - return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)) + return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer)) } responseDone := func() error { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 0c390b06d..401f188e9 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -149,16 +149,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, sessionPolicy := s.v.PolicyManager().ForLevel(s.user.Level) conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)) - var bufferedReader buf.BufferedReader - { - var reader buf.Reader - if sessionPolicy.Buffer.PerConnection == 0 { - reader = &buf.SingleReader{Reader: conn} - } else { - reader = buf.NewReader(conn) - } - bufferedReader = buf.BufferedReader{Reader: reader} - } + bufferedReader := buf.BufferedReader{Reader: buf.NewReader(conn)} request, bodyReader, err := ReadTCPSession(s.user, &bufferedReader) if err != nil { log.Record(&log.AccessMessage{ diff --git a/proxy/socks/client.go b/proxy/socks/client.go index 97169a972..e837b53c2 100644 --- a/proxy/socks/client.go +++ b/proxy/socks/client.go @@ -113,13 +113,7 @@ func (c *Client) Process(ctx context.Context, link *core.Link, dialer proxy.Dial } responseFunc = func() error { defer timer.SetTimeout(p.Timeouts.UplinkOnly) - var reader buf.Reader - if p.Buffer.PerConnection == 0 { - reader = &buf.SingleReader{Reader: conn} - } else { - reader = buf.NewReader(conn) - } - return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)) + return buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer)) } } else if request.Command == protocol.RequestCommandUDP { udpConn, err := dialer.Dial(ctx, udpRequest.Destination()) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 794c5fb3e..bb5d1f661 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -73,17 +73,6 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa newError("failed to set deadline").Base(err).WriteToLog(session.ExportIDToError(ctx)) } - var reader *buf.BufferedReader - { - var r buf.Reader - if plcy.Buffer.PerConnection == 0 { - r = &buf.SingleReader{Reader: conn} - } else { - r = buf.NewReader(conn) - } - reader = &buf.BufferedReader{Reader: r} - } - inboundDest, ok := proxy.InboundEntryPointFromContext(ctx) if !ok { return newError("inbound entry point not specified") @@ -93,6 +82,7 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa port: inboundDest.Port, } + reader := &buf.BufferedReader{Reader: buf.NewReader(conn)} request, err := svrSession.Handshake(reader, conn) if err != nil { if source, ok := proxy.SourceFromContext(ctx); ok { diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 0065219a0..e3220f35c 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -224,7 +224,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i return newError("unable to set read deadline").Base(err).AtWarning() } - reader := &buf.BufferedReader{Reader: &buf.SingleReader{Reader: connection}} + reader := &buf.BufferedReader{Reader: buf.NewReader(connection)} svrSession := encoding.NewServerSession(h.clients, h.sessionHistory) request, err := svrSession.DecodeRequestHeader(reader) @@ -280,9 +280,6 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i requestDone := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly) - if sessionPolicy.Buffer.PerConnection > 0 { - reader.Reader = buf.NewReader(connection) - } return transferRequest(timer, svrSession, request, reader, link.Writer) } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 1612f7035..ce6bd7735 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -144,16 +144,7 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia responseDone := func() error { defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) - var reader *buf.BufferedReader - { - var r buf.Reader - if sessionPolicy.Buffer.PerConnection == 0 { - r = &buf.SingleReader{Reader: conn} - } else { - r = buf.NewReader(conn) - } - reader = &buf.BufferedReader{Reader: r} - } + reader := &buf.BufferedReader{Reader: buf.NewReader(conn)} header, err := session.DecodeResponseHeader(reader) if err != nil { return newError("failed to read header").Base(err)