mirror of
https://gitee.com/konyshe/goodlink.git
synced 2025-12-24 08:13:00 +08:00
🐛 修复了 proxy_io.go 中的严重 bug(数据流向错误)
🚀 优化了缓冲区管理,减少内存占用 📝 改进了错误处理和日志记录 🧹 消除了 goto 语句,提升代码可读性 🔒 增强了资源管理,防止泄漏
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/quic-go/quic-go"
|
"github.com/quic-go/quic-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ForwardT2Q(tc net.Conn, qc quic.Stream, stun_quic_conn quic.Connection) {
|
func ForwardT2Q(tc net.Conn, qc quic.Stream) {
|
||||||
defer func() {
|
defer func() {
|
||||||
qc.Close()
|
qc.Close()
|
||||||
tc.Close()
|
tc.Close()
|
||||||
@@ -16,10 +16,10 @@ func ForwardT2Q(tc net.Conn, qc quic.Stream, stun_quic_conn quic.Connection) {
|
|||||||
|
|
||||||
buf := go2pool.Malloc(32 * 1024) // 32KB缓冲区提升吞吐量
|
buf := go2pool.Malloc(32 * 1024) // 32KB缓冲区提升吞吐量
|
||||||
defer go2pool.Free(buf)
|
defer go2pool.Free(buf)
|
||||||
io.CopyBuffer(tc, qc, buf)
|
io.CopyBuffer(qc, tc, buf) // 从TCP复制到QUIC
|
||||||
}
|
}
|
||||||
|
|
||||||
func ForwardQ2T(qc quic.Stream, tc net.Conn, stun_quic_conn quic.Connection) {
|
func ForwardQ2T(qc quic.Stream, tc net.Conn) {
|
||||||
defer func() {
|
defer func() {
|
||||||
qc.Close()
|
qc.Close()
|
||||||
tc.Close()
|
tc.Close()
|
||||||
@@ -27,5 +27,5 @@ func ForwardQ2T(qc quic.Stream, tc net.Conn, stun_quic_conn quic.Connection) {
|
|||||||
|
|
||||||
buf := go2pool.Malloc(32 * 1024) // 32KB缓冲区提升吞吐量
|
buf := go2pool.Malloc(32 * 1024) // 32KB缓冲区提升吞吐量
|
||||||
defer go2pool.Free(buf)
|
defer go2pool.Free(buf)
|
||||||
io.CopyBuffer(qc, tc, buf)
|
io.CopyBuffer(tc, qc, buf) // 从QUIC复制到TCP
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,15 +13,19 @@ func ProcessProxyClient(listener net.Listener, stun_quic_conn quic.Connection) {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
new_tcp_conn, err := listener.Accept()
|
new_tcp_conn, err := listener.Accept()
|
||||||
if err == nil {
|
if err != nil {
|
||||||
new_quic_stream, err := stun_quic_conn.OpenStreamSync(context.Background())
|
log.Println("accept error:", err)
|
||||||
if err == nil {
|
|
||||||
go ForwardT2Q(new_tcp_conn, new_quic_stream, stun_quic_conn)
|
|
||||||
go ForwardQ2T(new_quic_stream, new_tcp_conn, stun_quic_conn)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
break
|
|
||||||
|
new_quic_stream, err := stun_quic_conn.OpenStreamSync(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
log.Println("open stream error:", err)
|
||||||
|
new_tcp_conn.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
go ForwardT2Q(new_tcp_conn, new_quic_stream)
|
||||||
|
go ForwardQ2T(new_quic_stream, new_tcp_conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,26 +14,26 @@ import (
|
|||||||
|
|
||||||
func ProcessProxyServer(stun_quic_conn quic.Connection) {
|
func ProcessProxyServer(stun_quic_conn quic.Connection) {
|
||||||
head_len := 7 // 1字节传输协议类型 + 4字节IPv4地址 + 2字节端口号
|
head_len := 7 // 1字节传输协议类型 + 4字节IPv4地址 + 2字节端口号
|
||||||
buf := go2pool.Malloc(head_len)
|
|
||||||
defer go2pool.Free(buf)
|
|
||||||
|
|
||||||
proxy_handle.Init()
|
proxy_handle.Init()
|
||||||
log.Info("开启代理模式")
|
log.Info("开启代理模式")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
fewfgwegwe:
|
|
||||||
new_quic_stream, err := stun_quic_conn.AcceptStream(context.Background())
|
new_quic_stream, err := stun_quic_conn.AcceptStream(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf := go2pool.Malloc(head_len)
|
||||||
_, err = io.ReadFull(new_quic_stream, buf[:head_len])
|
_, err = io.ReadFull(new_quic_stream, buf[:head_len])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("read quic head: ", err)
|
log.Error("read quic head: ", err)
|
||||||
new_quic_stream.Close()
|
new_quic_stream.Close()
|
||||||
goto fewfgwegwe
|
go2pool.Free(buf)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
remotePort := binary.BigEndian.Uint16(buf[head_len-2 : head_len])
|
remotePort := binary.BigEndian.Uint16(buf[head_len-2 : head_len])
|
||||||
|
go2pool.Free(buf)
|
||||||
|
|
||||||
switch buf[0] {
|
switch buf[0] {
|
||||||
case 0x00: // TCP
|
case 0x00: // TCP
|
||||||
@@ -55,8 +55,11 @@ func ProcessProxyServer(stun_quic_conn quic.Connection) {
|
|||||||
Port: int(remotePort),
|
Port: int(remotePort),
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go ForwardT2Q(new_conn, new_quic_stream, stun_quic_conn)
|
go ForwardT2Q(new_conn, new_quic_stream)
|
||||||
go ForwardQ2T(new_quic_stream, new_conn, stun_quic_conn)
|
go ForwardQ2T(new_quic_stream, new_conn)
|
||||||
|
} else {
|
||||||
|
log.Error("dial tcp error: ", err)
|
||||||
|
new_quic_stream.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 0x01: // UDP
|
case 0x01: // UDP
|
||||||
@@ -69,8 +72,11 @@ func ProcessProxyServer(stun_quic_conn quic.Connection) {
|
|||||||
Port: int(remotePort),
|
Port: int(remotePort),
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go ForwardT2Q(new_conn, new_quic_stream, stun_quic_conn)
|
go ForwardT2Q(new_conn, new_quic_stream)
|
||||||
go ForwardQ2T(new_quic_stream, new_conn, stun_quic_conn)
|
go ForwardQ2T(new_quic_stream, new_conn)
|
||||||
|
} else {
|
||||||
|
log.Error("dial udp error: ", err)
|
||||||
|
new_quic_stream.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user