Skip to content

Commit

Permalink
proxy: some socks5 optimizations
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed May 5, 2024
1 parent 2c6cc3b commit 5668350
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
24 changes: 9 additions & 15 deletions proxy/pkg/mixed/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ type Option func(*Proxy)
// SwitchConn wraps a net.Conn and a bufio.Reader
type SwitchConn struct {
net.Conn
reader *bufio.Reader
*bufio.Reader
}

// NewSwitchConn creates a new SwitchConn
func NewSwitchConn(conn net.Conn) *SwitchConn {
return &SwitchConn{
Conn: conn,
reader: bufio.NewReader(conn),
Reader: bufio.NewReaderSize(conn, 2048),
}
}

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

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

// Read one byte to determine the protocol
buf := make([]byte, 1)
_, err := switchConn.Read(buf)
// Peek one byte to determine the protocol
buf, err := switchConn.Peek(1)
if err != nil {
return err
}

// Unread the byte so it's available for the next read
err = switchConn.reader.UnreadByte()
if err != nil {
return err
}

switch {
case buf[0] == 5:
switch buf[0] {
case 5:
err = p.socks5Proxy.ServeConn(switchConn)
case buf[0] == 4:
case 4:
err = p.socks4Proxy.ServeConn(switchConn)
default:
err = p.httpProxy.ServeConn(switchConn)
Expand Down
17 changes: 8 additions & 9 deletions proxy/pkg/socks5/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"math"
"net"
"strconv"
"strings"
Expand All @@ -20,7 +19,7 @@ var (
)

const (
maxUdpPacket = math.MaxUint16 - 28
maxUdpPacket = 2048
)

const (
Expand Down Expand Up @@ -321,8 +320,8 @@ func (cc *udpCustomConn) RemoteAddr() net.Addr {

func (cc *udpCustomConn) asyncReadPackets() {
go func() {
tempBuf := make([]byte, maxUdpPacket)
for {
tempBuf := make([]byte, maxUdpPacket)
n, addr, err := cc.ReadFrom(tempBuf)
if err != nil {
cc.packetQueue <- &readStruct{
Expand All @@ -331,18 +330,18 @@ func (cc *udpCustomConn) asyncReadPackets() {
}
break
}
if cc.sourceAddr == nil {
cc.sourceAddr = addr
}
packetData := tempBuf[:n]
if len(packetData) < 3 {
if n < 3 {
cc.packetQueue <- &readStruct{
data: nil,
err: err,
}
break
}
reader := bytes.NewBuffer(packetData[3:])
if cc.sourceAddr == nil {
cc.sourceAddr = addr
}

reader := bytes.NewBuffer(tempBuf[3:n])
targetAddr, err := readAddr(reader)

if err != nil {
Expand Down

0 comments on commit 5668350

Please sign in to comment.