zrok/endpoints/txer.go

41 lines
950 B
Go
Raw Normal View History

2023-04-21 19:35:58 +02:00
package endpoints
2023-04-18 21:31:48 +02:00
import (
"github.com/sirupsen/logrus"
2023-04-19 15:41:19 +02:00
"io"
2023-04-18 21:31:48 +02:00
"net"
)
const bufSz = 10240
2023-04-21 19:35:58 +02:00
func TXer(from, to net.Conn) {
2023-04-19 15:41:19 +02:00
logrus.Debugf("started '%v' -> '%v'", from.RemoteAddr(), to.RemoteAddr())
defer logrus.Debugf("exited '%v' -> '%v'", from.RemoteAddr(), to.RemoteAddr())
2023-04-18 21:31:48 +02:00
buf := make([]byte, bufSz)
for {
if rxsz, err := from.Read(buf); err == nil {
if txsz, err := to.Write(buf[:rxsz]); err == nil {
if txsz != rxsz {
logrus.Errorf("short write '%v' -> '%v' (%d != %d)", from.RemoteAddr(), to.RemoteAddr(), txsz, rxsz)
_ = to.Close()
_ = from.Close()
return
}
} else {
logrus.Errorf("write error '%v' -> '%v': %v", from.RemoteAddr(), to.RemoteAddr(), err)
_ = to.Close()
_ = from.Close()
return
}
} else {
2023-04-19 15:41:19 +02:00
if err != io.EOF {
logrus.Errorf("read error '%v' -> '%v': %v", from.RemoteAddr(), to.RemoteAddr(), err)
}
2023-04-18 21:31:48 +02:00
_ = to.Close()
_ = from.Close()
return
}
}
}