diff --git a/pkg/core/client.go b/pkg/core/client.go index fc099d8..333184c 100644 --- a/pkg/core/client.go +++ b/pkg/core/client.go @@ -86,7 +86,7 @@ func (c *Client) connectToServer() error { return err } if c.obfuscator != nil { - pktConn = newObfsPacketConn(ftcpConn, c.obfuscator) + pktConn = newObfsFakeTCPConn(ftcpConn, c.obfuscator) } else { pktConn = ftcpConn } diff --git a/pkg/core/obfs.go b/pkg/core/obfs.go index d67b2b9..ece418d 100644 --- a/pkg/core/obfs.go +++ b/pkg/core/obfs.go @@ -1,6 +1,7 @@ package core import ( + "github.com/tobyxdd/hysteria/pkg/faketcp" "net" "os" "sync" @@ -100,8 +101,8 @@ func (c *obfsUDPConn) File() (f *os.File, err error) { return c.orig.File() } -type obfsPacketConn struct { - orig net.PacketConn +type obfsFakeTCPConn struct { + orig *faketcp.TCPConn obfs Obfuscator readBuf []byte @@ -110,8 +111,8 @@ type obfsPacketConn struct { writeMutex sync.Mutex } -func newObfsPacketConn(orig net.PacketConn, obfs Obfuscator) *obfsPacketConn { - return &obfsPacketConn{ +func newObfsFakeTCPConn(orig *faketcp.TCPConn, obfs Obfuscator) *obfsFakeTCPConn { + return &obfsFakeTCPConn{ orig: orig, obfs: obfs, 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 { c.readMutex.Lock() 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() bn := c.obfs.Obfuscate(p, c.writeBuf) _, 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() } -func (c *obfsPacketConn) LocalAddr() net.Addr { +func (c *obfsFakeTCPConn) LocalAddr() net.Addr { 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) } -func (c *obfsPacketConn) SetReadDeadline(t time.Time) error { +func (c *obfsFakeTCPConn) SetReadDeadline(t time.Time) error { 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) } + +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() +} diff --git a/pkg/core/server.go b/pkg/core/server.go index 4887be5..ce373fa 100644 --- a/pkg/core/server.go +++ b/pkg/core/server.go @@ -63,7 +63,7 @@ func NewServer(addr string, protocol string, tlsConfig *tls.Config, quicConfig * return nil, err } if obfuscator != nil { - pktConn = newObfsPacketConn(ftcpConn, obfuscator) + pktConn = newObfsFakeTCPConn(ftcpConn, obfuscator) } else { pktConn = ftcpConn } diff --git a/pkg/faketcp/tcp_linux.go b/pkg/faketcp/tcp_linux.go index e16b478..dadb091 100644 --- a/pkg/faketcp/tcp_linux.go +++ b/pkg/faketcp/tcp_linux.go @@ -381,6 +381,14 @@ func (conn *TCPConn) SetWriteBuffer(bytes int) error { 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, // and returns a single packet-oriented connection func Dial(network, address string) (*TCPConn, error) {