mirror of
https://github.com/openziti/zrok.git
synced 2024-11-23 00:23:48 +01:00
tunnelFrontend elaboration (#170)
This commit is contained in:
parent
017c351c0f
commit
6c9a651a08
@ -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",
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user