tunnelFrontend elaboration (#170)

This commit is contained in:
Michael Quigley 2023-04-18 11:58:07 -04:00
parent 017c351c0f
commit 6c9a651a08
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 77 additions and 8 deletions

View File

@ -1,7 +1,13 @@
package main package main
import ( import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints/tunnelFrontend" "github.com/openziti/zrok/endpoints/tunnelFrontend"
"github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"time" "time"
) )
@ -28,6 +34,35 @@ func newAccessPrivateTunnelCommand() *accessPrivateTunnelCommand {
} }
func (cmd *accessPrivateTunnelCommand) run(_ *cobra.Command, args []string) { func (cmd *accessPrivateTunnelCommand) run(_ *cobra.Command, args []string) {
zrd, err := zrokdir.Load()
if err != nil {
tui.Error("unable to load zrokdir", err)
}
if zrd.Env == nil {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
}
zrok, err := zrd.Client()
if err != nil {
tui.Error("unable to create zrok client", err)
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", zrd.Env.Token)
req := share.NewAccessParams()
req.Body = &rest_model_zrok.AccessRequest{
ShrToken: args[0],
EnvZID: zrd.Env.ZId,
}
accessResp, err := zrok.Share.Access(req, auth)
if err != nil {
if !panicInstead {
tui.Error("unable to access", err)
}
panic(err)
}
logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken)
fe, err := tunnelFrontend.NewFrontend(&tunnelFrontend.Config{ fe, err := tunnelFrontend.NewFrontend(&tunnelFrontend.Config{
BindAddress: cmd.bindAddress, BindAddress: cmd.bindAddress,
IdentityName: "backend", IdentityName: "backend",

View File

@ -3,6 +3,7 @@ package tunnelFrontend
import ( import (
"github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/openziti/transport/v2" "github.com/openziti/transport/v2"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
"github.com/openziti/zrok/zrokdir" "github.com/openziti/zrok/zrokdir"
@ -62,16 +63,49 @@ func (f *Frontend) Stop() {
} }
func (f *Frontend) accept(conn transport.Conn) { 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)
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) {
buf := make([]byte, 10240) buf := make([]byte, 10240)
for { for {
n, err := conn.Read(buf) if rxsz, err := conn.Read(buf); err == nil {
if err != nil { if txsz, err := zConn.Write(buf[:rxsz]); err == nil {
logrus.Errorf("error reading: %v", err) if txsz != rxsz {
logrus.Errorf("short write '%v' (%d != %d)", zConn.RemoteAddr(), txsz, rxsz)
}
} else {
logrus.Errorf("error writing '%v': %v", zConn.RemoteAddr(), err)
return return
} }
n, err = conn.Write(buf[:n]) } else {
if err != nil { logrus.Errorf("read error '%v': %v", zConn.RemoteAddr(), err)
logrus.Errorf("error writing: %v", err) return
}
}
}
func (f *Frontend) txer(conn transport.Conn, zConn edge.Conn) {
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)
return
}
} else {
logrus.Errorf("read error '%v': %v", conn.RemoteAddr(), err)
return return
} }
} }