2022-04-25 19:50:20 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Relay copies between left and right bidirectionally.
|
|
|
|
func Relay(leftConn, rightConn net.Conn) {
|
|
|
|
ch := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Wrapping to avoid using *net.TCPConn.(ReadFrom)
|
|
|
|
// See also https://github.com/Dreamacro/clash/pull/1209
|
2022-06-11 20:38:16 +08:00
|
|
|
_, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
|
2022-04-25 19:50:20 +08:00
|
|
|
leftConn.SetReadDeadline(time.Now())
|
|
|
|
ch <- err
|
|
|
|
}()
|
|
|
|
|
2022-06-11 20:38:16 +08:00
|
|
|
io.Copy(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn})
|
2022-04-25 19:50:20 +08:00
|
|
|
rightConn.SetReadDeadline(time.Now())
|
|
|
|
<-ch
|
|
|
|
}
|