2023-04-18 19:28:15 +02:00
|
|
|
package tcpTunnel
|
2023-04-14 19:09:54 +02:00
|
|
|
|
2023-04-14 20:55:49 +02:00
|
|
|
import (
|
|
|
|
"github.com/openziti/sdk-golang/ziti"
|
|
|
|
"github.com/openziti/sdk-golang/ziti/config"
|
|
|
|
"github.com/openziti/sdk-golang/ziti/edge"
|
2023-04-18 18:14:27 +02:00
|
|
|
"github.com/openziti/transport/v2/tcp"
|
2023-04-14 20:55:49 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
2023-04-14 19:09:54 +02:00
|
|
|
|
2023-04-18 19:28:15 +02:00
|
|
|
type BackendConfig struct {
|
2023-04-14 19:09:54 +02:00
|
|
|
IdentityPath string
|
|
|
|
EndpointAddress string
|
|
|
|
ShrToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Backend struct {
|
2023-04-18 19:28:15 +02:00
|
|
|
cfg *BackendConfig
|
2023-04-14 19:09:54 +02:00
|
|
|
listener edge.Listener
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:28:15 +02:00
|
|
|
func NewBackend(cfg *BackendConfig) (*Backend, error) {
|
2023-04-14 20:55:49 +02:00
|
|
|
options := ziti.ListenOptions{
|
|
|
|
ConnectTimeout: 5 * time.Minute,
|
|
|
|
MaxConnections: 64,
|
|
|
|
}
|
|
|
|
zcfg, err := config.NewFromFile(cfg.IdentityPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading config")
|
|
|
|
}
|
|
|
|
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions(cfg.ShrToken, &options)
|
2023-04-17 18:47:52 +02:00
|
|
|
if err != nil {
|
2023-04-14 20:55:49 +02:00
|
|
|
return nil, errors.Wrap(err, "error listening")
|
|
|
|
}
|
|
|
|
b := &Backend{
|
|
|
|
cfg: cfg,
|
|
|
|
listener: listener,
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) Run() error {
|
|
|
|
logrus.Info("started")
|
|
|
|
defer logrus.Info("exited")
|
|
|
|
|
|
|
|
for {
|
|
|
|
if conn, err := b.listener.Accept(); err == nil {
|
|
|
|
go b.handle(conn)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) handle(conn net.Conn) {
|
|
|
|
logrus.Infof("handling '%v'", conn.RemoteAddr())
|
2023-04-18 18:14:27 +02:00
|
|
|
rConn, err := tcp.Dial(b.cfg.EndpointAddress, "tcp", 30*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error dialing '%v': %v", b.cfg.EndpointAddress, err)
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go b.rxer(conn, rConn)
|
|
|
|
go b.txer(conn, rConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) rxer(conn, rConn net.Conn) {
|
2023-04-18 19:11:21 +02:00
|
|
|
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
|
|
|
|
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
|
|
|
|
|
2023-04-18 18:14:27 +02:00
|
|
|
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)
|
2023-04-18 19:11:21 +02:00
|
|
|
_ = rConn.Close()
|
|
|
|
_ = conn.Close()
|
2023-04-18 18:14:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("read error '%v': %v", rConn.RemoteAddr(), err)
|
2023-04-18 19:11:21 +02:00
|
|
|
_ = rConn.Close()
|
|
|
|
_ = conn.Close()
|
2023-04-18 18:14:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) txer(conn, rConn net.Conn) {
|
2023-04-18 19:11:21 +02:00
|
|
|
logrus.Infof("started '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
|
|
|
|
defer logrus.Warnf("exited '%v' <=> '%v'", conn.RemoteAddr(), rConn.RemoteAddr())
|
|
|
|
|
2023-04-18 18:14:27 +02:00
|
|
|
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)
|
2023-04-18 19:11:21 +02:00
|
|
|
_ = rConn.Close()
|
|
|
|
_ = conn.Close()
|
2023-04-18 18:14:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("read error '%v': %v", conn.RemoteAddr(), err)
|
2023-04-18 19:11:21 +02:00
|
|
|
_ = rConn.Close()
|
|
|
|
_ = conn.Close()
|
2023-04-18 18:14:27 +02:00
|
|
|
return
|
|
|
|
}
|
2023-04-14 20:55:49 +02:00
|
|
|
}
|
|
|
|
}
|