Skip to content

Commit

Permalink
Merge pull request #40 from tobyxdd/wip-udp-buf
Browse files Browse the repository at this point in the history
Passthrough required methods for quic-go to increase the buffer size
  • Loading branch information
tobyxdd authored Apr 2, 2021
2 parents ca82abc + ede94df commit da65c4c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
19 changes: 12 additions & 7 deletions pkg/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,25 @@ func (c *Client) connectToServer() error {
if err != nil {
return err
}
packetConn, err := net.ListenPacket("udp", "")
udpConn, err := net.ListenUDP("udp", nil)
if err != nil {
return err
}
var qs quic.Session
if c.obfuscator != nil {
// Wrap PacketConn with obfuscator
packetConn = &obfsPacketConn{
Orig: packetConn,
qs, err = quic.Dial(&obfsUDPConn{
Orig: udpConn,
Obfuscator: c.obfuscator,
}, serverUDPAddr, c.serverAddr, c.tlsConfig, c.quicConfig)
if err != nil {
return err
}
} else {
qs, err = quic.Dial(udpConn, serverUDPAddr, c.serverAddr, c.tlsConfig, c.quicConfig)
if err != nil {
return err
}
}
qs, err := quic.Dial(packetConn, serverUDPAddr, c.serverAddr, c.tlsConfig, c.quicConfig)
if err != nil {
return err
}
// Control stream
ctx, ctxCancel := context.WithTimeout(context.Background(), protocolTimeout)
Expand Down
36 changes: 27 additions & 9 deletions pkg/core/obfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"net"
"os"
"syscall"
"time"
)

Expand All @@ -10,12 +12,16 @@ type Obfuscator interface {
Obfuscate(p []byte) []byte
}

type obfsPacketConn struct {
Orig net.PacketConn
type obfsUDPConn struct {
Orig *net.UDPConn
Obfuscator Obfuscator
}

func (c *obfsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
func (c *obfsUDPConn) SyscallConn() (syscall.RawConn, error) {
return c.Orig.SyscallConn()
}

func (c *obfsUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
oldN, addr, err := c.Orig.ReadFrom(p)
if oldN > 0 {
newN := c.Obfuscator.Deobfuscate(p, oldN)
Expand All @@ -25,7 +31,7 @@ func (c *obfsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
}
}

func (c *obfsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
func (c *obfsUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
np := c.Obfuscator.Obfuscate(p)
_, err = c.Orig.WriteTo(np, addr)
if err != nil {
Expand All @@ -35,22 +41,34 @@ func (c *obfsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
}
}

func (c *obfsPacketConn) Close() error {
func (c *obfsUDPConn) Close() error {
return c.Orig.Close()
}

func (c *obfsPacketConn) LocalAddr() net.Addr {
func (c *obfsUDPConn) LocalAddr() net.Addr {
return c.Orig.LocalAddr()
}

func (c *obfsPacketConn) SetDeadline(t time.Time) error {
func (c *obfsUDPConn) SetDeadline(t time.Time) error {
return c.Orig.SetDeadline(t)
}

func (c *obfsPacketConn) SetReadDeadline(t time.Time) error {
func (c *obfsUDPConn) SetReadDeadline(t time.Time) error {
return c.Orig.SetReadDeadline(t)
}

func (c *obfsPacketConn) SetWriteDeadline(t time.Time) error {
func (c *obfsUDPConn) SetWriteDeadline(t time.Time) error {
return c.Orig.SetWriteDeadline(t)
}

func (c *obfsUDPConn) SetReadBuffer(bytes int) error {
return c.Orig.SetReadBuffer(bytes)
}

func (c *obfsUDPConn) SetWriteBuffer(bytes int) error {
return c.Orig.SetWriteBuffer(bytes)
}

func (c *obfsUDPConn) File() (f *os.File, err error) {
return c.Orig.File()
}
23 changes: 16 additions & 7 deletions pkg/core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,29 @@ func NewServer(addr string, tlsConfig *tls.Config, quicConfig *quic.Config,
sendBPS uint64, recvBPS uint64, congestionFactory CongestionFactory, disableUDP bool, aclEngine *acl.Engine,
obfuscator Obfuscator, authFunc AuthFunc, tcpRequestFunc TCPRequestFunc, tcpErrorFunc TCPErrorFunc,
udpRequestFunc UDPRequestFunc, udpErrorFunc UDPErrorFunc) (*Server, error) {
packetConn, err := net.ListenPacket("udp", addr)
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
udpConn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return nil, err
}
var listener quic.Listener
if obfuscator != nil {
// Wrap PacketConn with obfuscator
packetConn = &obfsPacketConn{
Orig: packetConn,
listener, err = quic.Listen(&obfsUDPConn{
Orig: udpConn,
Obfuscator: obfuscator,
}, tlsConfig, quicConfig)
if err != nil {
return nil, err
}
} else {
listener, err = quic.Listen(udpConn, tlsConfig, quicConfig)
if err != nil {
return nil, err
}
}
listener, err := quic.Listen(packetConn, tlsConfig, quicConfig)
if err != nil {
return nil, err
}
s := &Server{
listener: listener,
Expand Down

0 comments on commit da65c4c

Please sign in to comment.