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/edge"
|
2023-04-21 19:35:58 +02:00
|
|
|
"github.com/openziti/zrok/endpoints"
|
2023-04-14 20:55:49 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
2023-04-14 20:55:49 +02:00
|
|
|
)
|
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
|
2023-04-25 21:16:05 +02:00
|
|
|
RequestsChan chan *endpoints.Request
|
2023-04-14 19:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2023-12-13 17:22:20 +01:00
|
|
|
ConnectTimeout: 5 * time.Minute,
|
|
|
|
WaitForNEstablishedListeners: 1,
|
2023-04-14 20:55:49 +02:00
|
|
|
}
|
2023-05-25 17:50:38 +02:00
|
|
|
zcfg, err := ziti.NewConfigFromFile(cfg.IdentityPath)
|
2023-04-14 20:55:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading config")
|
|
|
|
}
|
2023-05-25 17:50:38 +02:00
|
|
|
zctx, err := ziti.NewContext(zcfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading ziti context")
|
|
|
|
}
|
|
|
|
listener, err := zctx.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) {
|
2023-04-25 21:16:05 +02:00
|
|
|
logrus.Debugf("handling '%v'", conn.RemoteAddr())
|
2023-04-21 21:09:01 +02:00
|
|
|
if rAddr, err := net.ResolveTCPAddr("tcp", b.cfg.EndpointAddress); err == nil {
|
|
|
|
if rConn, err := net.DialTCP("tcp", nil, rAddr); err == nil {
|
|
|
|
go endpoints.TXer(conn, rConn)
|
|
|
|
go endpoints.TXer(rConn, conn)
|
2023-05-01 18:19:06 +02:00
|
|
|
if b.cfg.RequestsChan != nil {
|
|
|
|
b.cfg.RequestsChan <- &endpoints.Request{
|
|
|
|
Stamp: time.Now(),
|
|
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
|
|
Method: "ACCEPT",
|
|
|
|
Path: rAddr.String(),
|
|
|
|
}
|
2023-04-25 21:16:05 +02:00
|
|
|
}
|
2023-04-21 21:09:01 +02:00
|
|
|
} else {
|
|
|
|
logrus.Errorf("error dialing '%v': %v", b.cfg.EndpointAddress, err)
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("error resolving '%v': %v", b.cfg.EndpointAddress, err)
|
2023-04-18 18:14:27 +02:00
|
|
|
}
|
2023-04-14 20:55:49 +02:00
|
|
|
}
|