Skip to content

Commit b28634f

Browse files
committed
proxy: general tweaks
Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
1 parent 29dd4bf commit b28634f

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

proxy/pkg/mixed/proxy.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ type Option func(*Proxy)
6363
// SwitchConn wraps a net.Conn and a bufio.Reader
6464
type SwitchConn struct {
6565
net.Conn
66-
reader *bufio.Reader
66+
*bufio.Reader
6767
}
6868

6969
// NewSwitchConn creates a new SwitchConn
7070
func NewSwitchConn(conn net.Conn) *SwitchConn {
7171
return &SwitchConn{
7272
Conn: conn,
73-
reader: bufio.NewReader(conn),
73+
Reader: bufio.NewReader(conn),
7474
}
7575
}
7676

7777
// Read reads data into p, first from the bufio.Reader, then from the net.Conn
7878
func (c *SwitchConn) Read(p []byte) (n int, err error) {
79-
return c.reader.Read(p)
79+
return c.Reader.Read(p)
8080
}
8181

8282
func (p *Proxy) ListenAndServe() error {
@@ -116,6 +116,7 @@ func (p *Proxy) ListenAndServe() error {
116116
// Start a new goroutine to handle each connection
117117
// This way, the server can handle multiple connections concurrently
118118
go func() {
119+
defer conn.Close()
119120
err := p.handleConnection(conn)
120121
if err != nil {
121122
p.logger.Error(err.Error()) // Log errors from ServeConn
@@ -129,23 +130,16 @@ func (p *Proxy) handleConnection(conn net.Conn) error {
129130
// Create a SwitchConn
130131
switchConn := NewSwitchConn(conn)
131132

132-
// Read one byte to determine the protocol
133-
buf := make([]byte, 1)
134-
_, err := switchConn.Read(buf)
133+
// Peek one byte to determine the protocol
134+
buf, err := switchConn.Peek(1)
135135
if err != nil {
136136
return err
137137
}
138138

139-
// Unread the byte so it's available for the next read
140-
err = switchConn.reader.UnreadByte()
141-
if err != nil {
142-
return err
143-
}
144-
145-
switch {
146-
case buf[0] == 5:
139+
switch buf[0] {
140+
case 5:
147141
err = p.socks5Proxy.ServeConn(switchConn)
148-
case buf[0] == 4:
142+
case 4:
149143
err = p.socks4Proxy.ServeConn(switchConn)
150144
default:
151145
err = p.httpProxy.ServeConn(switchConn)

0 commit comments

Comments
 (0)