consolidated txer (#305)

This commit is contained in:
Michael Quigley 2023-04-18 15:31:48 -04:00
parent e432dfdb28
commit fdc038c12d
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 41 additions and 109 deletions

View File

@ -63,58 +63,6 @@ func (b *Backend) handle(conn net.Conn) {
_ = conn.Close()
return
}
go b.rxer(conn, rConn)
go b.txer(conn, rConn)
}
func (b *Backend) rxer(conn, rConn net.Conn) {
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
buf := make([]byte, 10240)
for {
if rxsz, err := conn.Read(buf); err == nil {
if txsz, err := rConn.Write(buf[:rxsz]); err == nil {
if txsz != rxsz {
logrus.Errorf("short write '%v' (%d != %d)", rConn.RemoteAddr(), txsz, rxsz)
}
} else {
logrus.Errorf("error writing '%v': %v", rConn.RemoteAddr(), err)
_ = rConn.Close()
_ = conn.Close()
return
}
} else {
logrus.Errorf("read error '%v': %v", rConn.RemoteAddr(), err)
_ = rConn.Close()
_ = conn.Close()
return
}
}
}
func (b *Backend) txer(conn, rConn net.Conn) {
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
buf := make([]byte, 10240)
for {
if rxsz, err := rConn.Read(buf); err == nil {
if txsz, err := conn.Write(buf[:rxsz]); err == nil {
if txsz != rxsz {
logrus.Errorf("short write '%v' (%d != %d)", conn.RemoteAddr(), txsz, rxsz)
}
} else {
logrus.Errorf("error writing '%v': %v", conn.RemoteAddr(), err)
_ = rConn.Close()
_ = conn.Close()
return
}
} else {
logrus.Errorf("read error '%v': %v", conn.RemoteAddr(), err)
_ = rConn.Close()
_ = conn.Close()
return
}
}
go txer(conn, rConn)
go txer(rConn, conn)
}

View File

@ -3,7 +3,6 @@ package tcpTunnel
import (
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/openziti/transport/v2"
"github.com/openziti/zrok/model"
"github.com/openziti/zrok/zrokdir"
@ -64,63 +63,11 @@ func (f *Frontend) Stop() {
func (f *Frontend) accept(conn transport.Conn) {
if zConn, err := f.zCtx.Dial(f.cfg.ShrToken); err == nil {
go f.rxer(conn, zConn)
go f.txer(conn, zConn)
go txer(conn, zConn)
go txer(zConn, conn)
logrus.Infof("accepted '%v' <=> '%v'", conn.RemoteAddr(), zConn.RemoteAddr())
} else {
logrus.Errorf("error dialing '%v': %v", f.cfg.ShrToken, err)
_ = conn.Close()
}
}
func (f *Frontend) rxer(conn transport.Conn, zConn edge.Conn) {
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), zConn.RemoteAddr())
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), zConn.RemoteAddr())
buf := make([]byte, 10240)
for {
if rxsz, err := conn.Read(buf); err == nil {
if txsz, err := zConn.Write(buf[:rxsz]); err == nil {
if txsz != rxsz {
logrus.Errorf("short write '%v' (%d != %d)", zConn.RemoteAddr(), txsz, rxsz)
}
} else {
logrus.Errorf("error writing '%v': %v", zConn.RemoteAddr(), err)
_ = zConn.Close()
_ = conn.Close()
return
}
} else {
logrus.Errorf("read error '%v': %v", zConn.RemoteAddr(), err)
_ = zConn.Close()
_ = conn.Close()
return
}
}
}
func (f *Frontend) txer(conn transport.Conn, zConn edge.Conn) {
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), zConn.RemoteAddr())
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), zConn.RemoteAddr())
buf := make([]byte, 10240)
for {
if rxsz, err := zConn.Read(buf); err == nil {
if txsz, err := conn.Write(buf[:rxsz]); err == nil {
if txsz != rxsz {
logrus.Errorf("short write '%v' (%d != %d)'", conn.RemoteAddr(), txsz, rxsz)
}
} else {
logrus.Errorf("error writing '%v': %v", conn.RemoteAddr(), err)
_ = zConn.Close()
_ = conn.Close()
return
}
} else {
logrus.Errorf("read error '%v': %v", conn.RemoteAddr(), err)
_ = zConn.Close()
_ = conn.Close()
return
}
}
}

View File

@ -0,0 +1,37 @@
package tcpTunnel
import (
"github.com/sirupsen/logrus"
"net"
)
const bufSz = 10240
func txer(from, to net.Conn) {
logrus.Infof("started '%v' -> '%v'", from.RemoteAddr(), to.RemoteAddr())
defer logrus.Warnf("exited '%v' -> '%v'", from.RemoteAddr(), to.RemoteAddr())
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 {
logrus.Errorf("read error '%v' -> '%v': %v", from.RemoteAddr(), to.RemoteAddr(), err)
_ = to.Close()
_ = from.Close()
return
}
}
}