2023-04-18 19:28:15 +02:00
|
|
|
package tcpTunnel
|
2023-04-17 19:55:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/openziti/sdk-golang/ziti"
|
2023-04-21 19:35:58 +02:00
|
|
|
"github.com/openziti/zrok/endpoints"
|
2023-07-13 20:26:35 +02:00
|
|
|
"github.com/openziti/zrok/environment"
|
2023-07-17 22:45:20 +02:00
|
|
|
"github.com/openziti/zrok/sdk"
|
2023-04-17 19:55:35 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
2023-04-17 19:55:35 +02:00
|
|
|
)
|
|
|
|
|
2023-04-18 19:28:15 +02:00
|
|
|
type FrontendConfig struct {
|
2023-04-17 19:55:35 +02:00
|
|
|
BindAddress string
|
|
|
|
IdentityName string
|
|
|
|
ShrToken string
|
2023-05-01 18:19:06 +02:00
|
|
|
RequestsChan chan *endpoints.Request
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Frontend struct {
|
2023-04-21 21:09:01 +02:00
|
|
|
cfg *FrontendConfig
|
|
|
|
zCtx ziti.Context
|
|
|
|
lAddr *net.TCPAddr
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:28:15 +02:00
|
|
|
func NewFrontend(cfg *FrontendConfig) (*Frontend, error) {
|
2023-04-21 21:09:01 +02:00
|
|
|
lAddr, err := net.ResolveTCPAddr("tcp", cfg.BindAddress)
|
2023-04-17 19:55:35 +02:00
|
|
|
if err != nil {
|
2023-04-21 21:09:01 +02:00
|
|
|
return nil, errors.Wrapf(err, "error resolving tcp address '%v'", cfg.BindAddress)
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
2023-07-13 20:26:35 +02:00
|
|
|
env, err := environment.LoadRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading environment root")
|
|
|
|
}
|
2023-07-14 16:14:32 +02:00
|
|
|
zCfgPath, err := env.ZitiIdentityNamed(cfg.IdentityName)
|
2023-04-17 19:55:35 +02:00
|
|
|
if err != nil {
|
2023-07-10 22:41:16 +02:00
|
|
|
return nil, errors.Wrapf(err, "error getting ziti identity '%v' from environment", cfg.IdentityName)
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
2023-05-25 17:50:38 +02:00
|
|
|
zCfg, err := ziti.NewConfigFromFile(zCfgPath)
|
2023-04-17 19:55:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error loading config")
|
|
|
|
}
|
2023-07-17 22:45:20 +02:00
|
|
|
zCfg.ConfigTypes = []string{sdk.ZrokProxyConfig}
|
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")
|
|
|
|
}
|
2023-04-17 19:55:35 +02:00
|
|
|
return &Frontend{
|
2023-04-21 21:09:01 +02:00
|
|
|
cfg: cfg,
|
|
|
|
zCtx: zCtx,
|
|
|
|
lAddr: lAddr,
|
2023-04-17 19:55:35 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Frontend) Run() error {
|
2023-04-21 21:09:01 +02:00
|
|
|
l, err := net.ListenTCP("tcp", f.lAddr)
|
2023-04-17 19:55:35 +02:00
|
|
|
if err != nil {
|
2023-04-21 21:09:01 +02:00
|
|
|
return errors.Wrapf(err, "error listening at '%v'", f.lAddr)
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
2023-04-21 21:09:01 +02:00
|
|
|
for {
|
|
|
|
if conn, err := l.Accept(); err == nil {
|
|
|
|
go f.accept(conn)
|
2023-05-01 18:19:06 +02:00
|
|
|
logrus.Debugf("accepted tcp connection from '%v'", conn.RemoteAddr())
|
2023-04-21 21:09:01 +02:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-17 19:55:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:09:01 +02:00
|
|
|
func (f *Frontend) accept(conn net.Conn) {
|
2023-04-18 17:58:07 +02:00
|
|
|
if zConn, err := f.zCtx.Dial(f.cfg.ShrToken); err == nil {
|
2023-04-21 19:35:58 +02:00
|
|
|
go endpoints.TXer(conn, zConn)
|
|
|
|
go endpoints.TXer(zConn, conn)
|
2023-05-01 18:19:06 +02:00
|
|
|
if f.cfg.RequestsChan != nil {
|
|
|
|
f.cfg.RequestsChan <- &endpoints.Request{
|
|
|
|
Stamp: time.Now(),
|
|
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
|
|
Method: "ACCEPT",
|
|
|
|
Path: f.cfg.ShrToken,
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 17:58:07 +02:00
|
|
|
} else {
|
|
|
|
logrus.Errorf("error dialing '%v': %v", f.cfg.ShrToken, err)
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
}
|