From 347e5e960693ab94721c9d8e317fb10b01351179 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 5 Oct 2022 13:29:10 +0800 Subject: [PATCH] fix: dns tcp hijack not working --- listener/tun/ipstack/gvisor/handler.go | 8 +++++++- listener/tun/ipstack/system/stack.go | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/listener/tun/ipstack/gvisor/handler.go b/listener/tun/ipstack/gvisor/handler.go index ed119fc3..5d7d84f4 100644 --- a/listener/tun/ipstack/gvisor/handler.go +++ b/listener/tun/ipstack/gvisor/handler.go @@ -4,6 +4,7 @@ package gvisor import ( "encoding/binary" + "io" "net" "net/netip" "time" @@ -60,7 +61,7 @@ func (gh *gvHandler) HandleTCP(tunConn adapter.TCPConn) { break } - n, err := tunConn.Read(buf[:length]) + n, err := io.ReadFull(tunConn, buf[:length]) if err != nil { break } @@ -70,6 +71,11 @@ func (gh *gvHandler) HandleTCP(tunConn adapter.TCPConn) { break } + err = binary.Write(tunConn, binary.BigEndian, uint16(len(msg))) + if err != nil { + break + } + _, _ = tunConn.Write(msg) } }() diff --git a/listener/tun/ipstack/system/stack.go b/listener/tun/ipstack/system/stack.go index b4668293..405eeb71 100644 --- a/listener/tun/ipstack/system/stack.go +++ b/listener/tun/ipstack/system/stack.go @@ -113,7 +113,7 @@ func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Pref break } - n, err := conn.Read(buf[:length]) + n, err := io.ReadFull(conn, buf[:length]) if err != nil { break } @@ -123,6 +123,11 @@ func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Pref break } + err = binary.Write(conn, binary.BigEndian, uint16(len(msg))) + if err != nil { + break + } + _, _ = conn.Write(msg) } }()