Skip to content

Commit a67f5db

Browse files
committed
Clean up bufConn type.
1 parent 6cd6227 commit a67f5db

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

net/buffered_conn.go renamed to net/bufio.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ import (
1919
"net"
2020
)
2121

22-
type BufferedConn struct {
23-
R *bufio.Reader
22+
// BufConn wraps an original [net.Conn] and a [bufio.Reader] to allow reads
23+
// without losing bytes in the buffer.
24+
type BufConn struct {
25+
*bufio.Reader
2426
net.Conn
2527
}
2628

27-
func NewBufferedConn(c net.Conn) BufferedConn {
28-
return BufferedConn{bufio.NewReader(c), c}
29+
func NewBufConn(conn net.Conn) BufConn {
30+
return BufConn{Reader: bufio.NewReader(conn), Conn: conn}
2931
}
3032

31-
func (b BufferedConn) Peek(n int) ([]byte, error) {
32-
return b.R.Peek(n)
33+
func (c BufConn) Peek(n int) ([]byte, error) {
34+
return c.Reader.Peek(n)
3335
}
3436

35-
func (b BufferedConn) Read(p []byte) (int, error) {
36-
return b.R.Read(p)
37+
func (c BufConn) Read(p []byte) (int, error) {
38+
return c.Reader.Read(p)
3739
}

service/proxy/proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func ParseSocks(rw io.ReadWriter) (string, error) {
3939
return tgtAddr.String(), nil
4040
}
4141

42-
func ParseHTTP(conn onet.BufferedConn) (string, error) {
43-
req, err := http.ReadRequest(conn.R)
42+
func ParseHTTP(conn onet.BufConn) (string, error) {
43+
req, err := http.ReadRequest(conn.Reader)
4444
if err != nil {
4545
return "", err
4646
}

service/tcp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ type ShadowsocksTCPMetrics interface {
129129
// TODO(fortuna): Offer alternative transports.
130130
func NewShadowsocksStreamAuthenticator(ciphers CipherList, replayCache *ReplayCache, metrics ShadowsocksTCPMetrics) StreamAuthenticateFunc {
131131
return func(clientConn transport.StreamConn) (string, transport.StreamConn, *onet.ConnectionError) {
132-
bufConn := onet.NewBufferedConn(clientConn)
132+
bufConn := onet.NewBufConn(clientConn)
133133
// Find the cipher and acess key id.
134-
cipherEntry, clientSalt, timeToCipher, keyErr := findAccessKey(bufConn.R, remoteIP(bufConn), ciphers)
134+
cipherEntry, clientSalt, timeToCipher, keyErr := findAccessKey(bufConn.Reader, remoteIP(bufConn), ciphers)
135135
metrics.AddTCPCipherSearch(keyErr == nil, timeToCipher)
136136
if keyErr != nil {
137137
const status = "ERR_CIPHER"
@@ -279,7 +279,7 @@ func (h *tcpHandler) Handle(ctx context.Context, clientConn transport.StreamConn
279279
logger.Debugf("Done with status %v, duration %v", status, connDuration)
280280
}
281281

282-
func getProxyRequest(bufConn onet.BufferedConn) (string, error) {
282+
func getProxyRequest(bufConn onet.BufConn) (string, error) {
283283
firstByte, err := bufConn.Peek(1)
284284
if err != nil {
285285
return "", fmt.Errorf("reading header failed: %w", err)
@@ -365,7 +365,7 @@ func (h *tcpHandler) handleConnection(ctx context.Context, listenerPort int, cli
365365
}
366366
h.m.AddAuthenticatedTCPConnection(outerConn.RemoteAddr(), id)
367367

368-
bufConn := onet.NewBufferedConn(innerConn)
368+
bufConn := onet.NewBufConn(innerConn)
369369
// Read target address and dial it.
370370
tgtAddr, err := getProxyRequest(bufConn)
371371
// Clear the deadline for the target address

0 commit comments

Comments
 (0)