Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy optimizations and wireguard tweaks #89

Merged
merged 4 commits into from
May 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
proxy: some socks5 optimizations
Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
markpash committed May 5, 2024
commit 9eb6d4ea3990677b4120ab91974301c0d2fa4aa5
24 changes: 9 additions & 15 deletions proxy/pkg/mixed/proxy.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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
@@ -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)
17 changes: 8 additions & 9 deletions proxy/pkg/socks5/common.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"math"
"net"
"strconv"
"strings"
@@ -20,7 +19,7 @@ var (
)

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

const (
@@ -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{
@@ -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 {