share private tui (#56)

This commit is contained in:
Michael Quigley 2023-01-10 15:39:45 -05:00
parent ad3ecab2ac
commit 2c5ea40b73
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 41 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/endpoints"
@ -21,7 +22,6 @@ import (
"os/signal"
"strings"
"syscall"
"time"
)
func init() {
@ -31,18 +31,20 @@ func init() {
type sharePrivateCommand struct {
basicAuth []string
backendMode string
headless bool
cmd *cobra.Command
}
func newSharePrivateCommand() *sharePrivateCommand {
cmd := &cobra.Command{
Use: "private <targetEndpoint>",
Short: "Share a target endpoint privately",
Use: "private <target>",
Short: "Share a target resource privately",
Args: cobra.ExactArgs(1),
}
command := &sharePrivateCommand{cmd: cmd}
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...")
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Run = command.run
return command
}
@ -136,12 +138,14 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
os.Exit(0)
}()
requestsChan := make(chan *endpoints.BackendRequest, 1024)
switch cmd.backendMode {
case "proxy":
cfg := &proxyBackend.Config{
IdentityPath: zif,
EndpointAddress: target,
ShrToken: resp.Payload.ShrToken,
RequestsChan: requestsChan,
}
_, err = cmd.proxyBackendMode(cfg)
if err != nil {
@ -156,6 +160,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
IdentityPath: zif,
WebRoot: target,
ShrToken: resp.Payload.ShrToken,
RequestsChan: requestsChan,
}
_, err = cmd.webBackendMode(cfg)
if err != nil {
@ -169,10 +174,35 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
tui.Error("invalid backend mode", nil)
}
logrus.Infof("share with others; they will use this command for access: 'zrok access private %v'", resp.Payload.ShrToken)
if cmd.headless {
logrus.Infof("allow other to access your share with the following command:\nzrok access private %v", resp.Payload.ShrToken)
for {
select {
case req := <-requestsChan:
logrus.Infof("%v -> %v %v", req.RemoteAddr, req.Method, req.Path)
}
}
for {
time.Sleep(30 * time.Second)
} else {
shareDescription := fmt.Sprintf("access your share with: %v", tui.CodeStyle.Render(fmt.Sprintf("zrok access private %v", resp.Payload.ShrToken)))
mdl := newShareModel(resp.Payload.ShrToken, []string{shareDescription}, "public", cmd.backendMode)
prg := tea.NewProgram(mdl, tea.WithAltScreen())
go func() {
for {
select {
case req := <-requestsChan:
prg.Send(req)
}
}
}()
if _, err := prg.Run(); err != nil {
tui.Error("An error occurred", err)
}
close(requestsChan)
cmd.destroy(zrd.Env.ZId, resp.Payload.ShrToken, zrok, auth)
}
}

View File

@ -78,7 +78,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
zrd, err := zrokdir.Load()
if err != nil {
if !panicInstead {
tui.Error("unable to load zrokdir", nil)
tui.Error("unable to load zrokdir", err)
}
panic(err)
}
@ -102,6 +102,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
panic(err)
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", zrd.Env.Token)
req := share.NewShareParams()
req.Body = &rest_model_zrok.ShareRequest{
@ -127,7 +128,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
resp, err := zrok.Share.Share(req, auth)
if err != nil {
if !panicInstead {
tui.Error("unable to create tunnel", err)
tui.Error("unable to create share", err)
}
panic(err)
}
@ -140,7 +141,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
os.Exit(0)
}()
var bh endpoints.BackendHandler
requestsChan := make(chan *endpoints.BackendRequest, 1024)
switch cmd.backendMode {
case "proxy":
@ -150,7 +150,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
ShrToken: resp.Payload.ShrToken,
RequestsChan: requestsChan,
}
bh, err = cmd.proxyBackendMode(cfg)
_, err = cmd.proxyBackendMode(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create proxy backend handler", err)
@ -165,7 +165,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
ShrToken: resp.Payload.ShrToken,
RequestsChan: requestsChan,
}
bh, err = cmd.webBackendMode(cfg)
_, err = cmd.webBackendMode(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create web backend handler", err)
@ -177,8 +177,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
tui.Error("invalid backend mode", nil)
}
_ = bh.Requests()()
if cmd.headless {
logrus.Infof("access your zrok share at the following endpoints:\n %v", strings.Join(resp.Payload.FrontendProxyEndpoints, "\n"))
for {