feat: make quic-go happy about buffer stuff

This commit is contained in:
Toby 2021-12-27 16:42:10 -08:00
parent 1484c3585b
commit 4cb4ca5e96
4 changed files with 34 additions and 13 deletions

View File

@ -86,7 +86,7 @@ func (c *Client) connectToServer() error {
return err return err
} }
if c.obfuscator != nil { if c.obfuscator != nil {
pktConn = newObfsPacketConn(ftcpConn, c.obfuscator) pktConn = newObfsFakeTCPConn(ftcpConn, c.obfuscator)
} else { } else {
pktConn = ftcpConn pktConn = ftcpConn
} }

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"github.com/tobyxdd/hysteria/pkg/faketcp"
"net" "net"
"os" "os"
"sync" "sync"
@ -100,8 +101,8 @@ func (c *obfsUDPConn) File() (f *os.File, err error) {
return c.orig.File() return c.orig.File()
} }
type obfsPacketConn struct { type obfsFakeTCPConn struct {
orig net.PacketConn orig *faketcp.TCPConn
obfs Obfuscator obfs Obfuscator
readBuf []byte readBuf []byte
@ -110,8 +111,8 @@ type obfsPacketConn struct {
writeMutex sync.Mutex writeMutex sync.Mutex
} }
func newObfsPacketConn(orig net.PacketConn, obfs Obfuscator) *obfsPacketConn { func newObfsFakeTCPConn(orig *faketcp.TCPConn, obfs Obfuscator) *obfsFakeTCPConn {
return &obfsPacketConn{ return &obfsFakeTCPConn{
orig: orig, orig: orig,
obfs: obfs, obfs: obfs,
readBuf: make([]byte, udpBufferSize), readBuf: make([]byte, udpBufferSize),
@ -119,7 +120,7 @@ func newObfsPacketConn(orig net.PacketConn, obfs Obfuscator) *obfsPacketConn {
} }
} }
func (c *obfsPacketConn) ReadFrom(p []byte) (int, net.Addr, error) { func (c *obfsFakeTCPConn) ReadFrom(p []byte) (int, net.Addr, error) {
for { for {
c.readMutex.Lock() c.readMutex.Lock()
n, addr, err := c.orig.ReadFrom(c.readBuf) n, addr, err := c.orig.ReadFrom(c.readBuf)
@ -139,7 +140,7 @@ func (c *obfsPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
} }
} }
func (c *obfsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { func (c *obfsFakeTCPConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
c.writeMutex.Lock() c.writeMutex.Lock()
bn := c.obfs.Obfuscate(p, c.writeBuf) bn := c.obfs.Obfuscate(p, c.writeBuf)
_, err = c.orig.WriteTo(c.writeBuf[:bn], addr) _, err = c.orig.WriteTo(c.writeBuf[:bn], addr)
@ -151,22 +152,34 @@ func (c *obfsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
} }
} }
func (c *obfsPacketConn) Close() error { func (c *obfsFakeTCPConn) Close() error {
return c.orig.Close() return c.orig.Close()
} }
func (c *obfsPacketConn) LocalAddr() net.Addr { func (c *obfsFakeTCPConn) LocalAddr() net.Addr {
return c.orig.LocalAddr() return c.orig.LocalAddr()
} }
func (c *obfsPacketConn) SetDeadline(t time.Time) error { func (c *obfsFakeTCPConn) SetDeadline(t time.Time) error {
return c.orig.SetDeadline(t) return c.orig.SetDeadline(t)
} }
func (c *obfsPacketConn) SetReadDeadline(t time.Time) error { func (c *obfsFakeTCPConn) SetReadDeadline(t time.Time) error {
return c.orig.SetReadDeadline(t) return c.orig.SetReadDeadline(t)
} }
func (c *obfsPacketConn) SetWriteDeadline(t time.Time) error { func (c *obfsFakeTCPConn) SetWriteDeadline(t time.Time) error {
return c.orig.SetWriteDeadline(t) return c.orig.SetWriteDeadline(t)
} }
func (c *obfsFakeTCPConn) SetReadBuffer(bytes int) error {
return c.orig.SetReadBuffer(bytes)
}
func (c *obfsFakeTCPConn) SetWriteBuffer(bytes int) error {
return c.orig.SetWriteBuffer(bytes)
}
func (c *obfsFakeTCPConn) SyscallConn() (syscall.RawConn, error) {
return c.orig.SyscallConn()
}

View File

@ -63,7 +63,7 @@ func NewServer(addr string, protocol string, tlsConfig *tls.Config, quicConfig *
return nil, err return nil, err
} }
if obfuscator != nil { if obfuscator != nil {
pktConn = newObfsPacketConn(ftcpConn, obfuscator) pktConn = newObfsFakeTCPConn(ftcpConn, obfuscator)
} else { } else {
pktConn = ftcpConn pktConn = ftcpConn
} }

View File

@ -381,6 +381,14 @@ func (conn *TCPConn) SetWriteBuffer(bytes int) error {
return err return err
} }
func (conn *TCPConn) SyscallConn() (syscall.RawConn, error) {
if len(conn.handles) == 0 {
return nil, errors.New("no handles")
// How is it possible?
}
return conn.handles[0].SyscallConn()
}
// Dial connects to the remote TCP port, // Dial connects to the remote TCP port,
// and returns a single packet-oriented connection // and returns a single packet-oriented connection
func Dial(network, address string) (*TCPConn, error) { func Dial(network, address string) (*TCPConn, error) {