Files
GoTunnel/pkg/relay/relay.go
Flik 4623a7f031
All checks were successful
Build Multi-Platform Binaries / build (push) Successful in 11m9s
add plugins
2025-12-26 11:24:23 +08:00

30 lines
473 B
Go

package relay
import (
"io"
"net"
"sync"
)
const bufferSize = 32 * 1024
// Relay 双向数据转发
func Relay(c1, c2 net.Conn) {
var wg sync.WaitGroup
wg.Add(2)
copyConn := func(dst, src net.Conn) {
defer wg.Done()
buf := make([]byte, bufferSize)
_, _ = io.CopyBuffer(dst, src, buf)
// 关闭写端,通知对方数据传输完成
if tc, ok := dst.(*net.TCPConn); ok {
tc.CloseWrite()
}
}
go copyConn(c1, c2)
go copyConn(c2, c1)
wg.Wait()
}