Skip to content

Commit

Permalink
Clean up bufConn type.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed May 1, 2024
1 parent 6cd6227 commit a67f5db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
18 changes: 10 additions & 8 deletions net/buffered_conn.go → net/bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ import (
"net"
)

type BufferedConn struct {
R *bufio.Reader
// BufConn wraps an original [net.Conn] and a [bufio.Reader] to allow reads
// without losing bytes in the buffer.
type BufConn struct {
*bufio.Reader
net.Conn
}

func NewBufferedConn(c net.Conn) BufferedConn {
return BufferedConn{bufio.NewReader(c), c}
func NewBufConn(conn net.Conn) BufConn {
return BufConn{Reader: bufio.NewReader(conn), Conn: conn}
}

func (b BufferedConn) Peek(n int) ([]byte, error) {
return b.R.Peek(n)
func (c BufConn) Peek(n int) ([]byte, error) {
return c.Reader.Peek(n)
}

func (b BufferedConn) Read(p []byte) (int, error) {
return b.R.Read(p)
func (c BufConn) Read(p []byte) (int, error) {
return c.Reader.Read(p)
}
4 changes: 2 additions & 2 deletions service/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func ParseSocks(rw io.ReadWriter) (string, error) {
return tgtAddr.String(), nil
}

func ParseHTTP(conn onet.BufferedConn) (string, error) {
req, err := http.ReadRequest(conn.R)
func ParseHTTP(conn onet.BufConn) (string, error) {
req, err := http.ReadRequest(conn.Reader)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions service/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ type ShadowsocksTCPMetrics interface {
// TODO(fortuna): Offer alternative transports.
func NewShadowsocksStreamAuthenticator(ciphers CipherList, replayCache *ReplayCache, metrics ShadowsocksTCPMetrics) StreamAuthenticateFunc {
return func(clientConn transport.StreamConn) (string, transport.StreamConn, *onet.ConnectionError) {
bufConn := onet.NewBufferedConn(clientConn)
bufConn := onet.NewBufConn(clientConn)
// Find the cipher and acess key id.
cipherEntry, clientSalt, timeToCipher, keyErr := findAccessKey(bufConn.R, remoteIP(bufConn), ciphers)
cipherEntry, clientSalt, timeToCipher, keyErr := findAccessKey(bufConn.Reader, remoteIP(bufConn), ciphers)
metrics.AddTCPCipherSearch(keyErr == nil, timeToCipher)
if keyErr != nil {
const status = "ERR_CIPHER"
Expand Down Expand Up @@ -279,7 +279,7 @@ func (h *tcpHandler) Handle(ctx context.Context, clientConn transport.StreamConn
logger.Debugf("Done with status %v, duration %v", status, connDuration)
}

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

bufConn := onet.NewBufferedConn(innerConn)
bufConn := onet.NewBufConn(innerConn)
// Read target address and dial it.
tgtAddr, err := getProxyRequest(bufConn)
// Clear the deadline for the target address
Expand Down

0 comments on commit a67f5db

Please sign in to comment.