rip out old tui; full backend data feed for requests (#56)

This commit is contained in:
Michael Quigley 2023-01-09 17:24:35 -05:00
parent b8f566bf6a
commit ee7c0fcca6
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 21 additions and 118 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend"
@ -103,7 +102,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
req.Body = &rest_model_zrok.ShareRequest{
EnvZID: zrd.Env.ZId,
ShareMode: "private",
BackendMode: "proxy",
BackendMode: cmd.backendMode,
BackendProxyEndpoint: target,
AuthScheme: string(model.None),
}
@ -121,7 +120,6 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
}
resp, err := zrok.Share.Share(req, auth)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create share", err)
}
@ -145,7 +143,6 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
}
_, err = cmd.proxyBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create proxy backend handler", err)
}
@ -160,7 +157,6 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
}
_, err = cmd.webBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create web backend handler", err)
}
@ -168,7 +164,6 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
}
default:
ui.Close()
showError("invalid backend mode", nil)
}

View File

@ -2,11 +2,8 @@ package main
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
tb "github.com/nsf/termbox-go"
"github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend"
"github.com/openziti-test-kitchen/zrok/endpoints/webBackend"
"github.com/openziti-test-kitchen/zrok/model"
@ -30,7 +27,6 @@ func init() {
}
type sharePublicCommand struct {
quiet bool
basicAuth []string
frontendSelection []string
backendMode string
@ -44,7 +40,6 @@ func newSharePublicCommand() *sharePublicCommand {
Args: cobra.ExactArgs(1),
}
command := &sharePublicCommand{cmd: cmd}
cmd.Flags().BoolVarP(&command.quiet, "quiet", "q", false, "Disable TUI 'chrome' for quiet operation")
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontends", []string{"public"}, "Selected frontends to use for the share")
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
@ -76,20 +71,8 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
}
if !cmd.quiet {
if err := ui.Init(); err != nil {
if !panicInstead {
showError("unable to initialize user interface", err)
}
panic(err)
}
defer ui.Close()
tb.SetInputMode(tb.InputEsc)
}
zrd, err := zrokdir.Load()
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to load zrokdir", nil)
}
@ -97,13 +80,11 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
if zrd.Env == nil {
ui.Close()
showError("unable to load environment; did you 'zrok enable'?", nil)
}
zif, err := zrokdir.ZitiIdentityFile("backend")
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to load ziti identity configuration", err)
}
@ -112,7 +93,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
zrok, err := zrd.Client()
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create zrok client", err)
}
@ -124,7 +104,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
EnvZID: zrd.Env.ZId,
ShareMode: "public",
FrontendSelection: cmd.frontendSelection,
BackendMode: "proxy",
BackendMode: cmd.backendMode,
BackendProxyEndpoint: target,
AuthScheme: string(model.None),
}
@ -142,7 +122,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
resp, err := zrok.Share.Share(req, auth)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create tunnel", err)
}
@ -167,7 +146,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
bh, err = cmd.proxyBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create proxy backend handler", err)
}
@ -182,7 +160,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
bh, err = cmd.webBackendMode(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create web backend handler", err)
}
@ -190,75 +167,13 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}
default:
ui.Close()
showError("invalid backend mode", nil)
}
if !cmd.quiet {
ui.Clear()
w, h := ui.TerminalDimensions()
p := widgets.NewParagraph()
p.Border = true
p.Title = " access your zrok share "
p.Text = fmt.Sprintf("%v%v", strings.Repeat(" ", (((w-12)-len(resp.Payload.FrontendProxyEndpoints[0]))/2)-1), resp.Payload.FrontendProxyEndpoints[0])
p.TextStyle = ui.Style{Fg: ui.ColorWhite}
p.PaddingTop = 1
p.SetRect(5, 5, w-10, 10)
lastRequests := float64(0)
var requestData []float64
spk := widgets.NewSparkline()
spk.Title = " requests "
spk.Data = requestData
spk.LineColor = ui.ColorCyan
slg := widgets.NewSparklineGroup(spk)
slg.SetRect(5, 11, w-10, h-5)
ui.Render(p, slg)
ticker := time.NewTicker(time.Second).C
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
switch e.Type {
case ui.ResizeEvent:
ui.Clear()
w, h = ui.TerminalDimensions()
p.SetRect(5, 5, w-10, 10)
slg.SetRect(5, 11, w-10, h-5)
ui.Render(p, slg)
case ui.KeyboardEvent:
switch e.ID {
case "q", "<C-c>":
ui.Close()
cmd.destroy(zrd.Env.ZId, resp.Payload.ShrToken, zrok, auth)
os.Exit(0)
}
}
case <-ticker:
currentRequests := float64(bh.Requests()())
deltaRequests := currentRequests - lastRequests
requestData = append(requestData, deltaRequests)
lastRequests = currentRequests
requestData = append(requestData, deltaRequests)
for len(requestData) > w-17 {
requestData = requestData[1:]
}
spk.Title = fmt.Sprintf(" requests (%d) ", int(currentRequests))
spk.Data = requestData
ui.Render(p, slg)
}
}
} else {
logrus.Infof("access your zrok share: %v", resp.Payload.FrontendProxyEndpoints[0])
for {
time.Sleep(30 * time.Second)
}
time.Sleep(5 * time.Second)
logrus.Infof("requests: %d", bh.Requests()())
}
}

View File

@ -1,7 +1,6 @@
package main
import (
ui "github.com/gizak/termui/v3"
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/endpoints/proxyBackend"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/metadata"
@ -53,7 +52,6 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
zrd, err := zrokdir.Load()
if err != nil {
ui.Close()
if !panicInstead {
showError("error loading zrokdir", err)
}
@ -61,13 +59,11 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
}
if zrd.Env == nil {
ui.Close()
showError("unable to load environment; did you 'zrok enable'?", nil)
}
zrok, err := zrd.Client()
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create zrok client", err)
}
@ -89,7 +85,6 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
zif, err := zrokdir.ZitiIdentityFile("backend")
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to load ziti identity configuration", err)
}
@ -121,7 +116,6 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
httpProxy, err := proxyBackend.NewBackend(cfg)
if err != nil {
ui.Close()
if !panicInstead {
showError("unable to create http backend", err)
}

View File

@ -2,6 +2,7 @@ package proxyBackend
import (
"context"
"fmt"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
@ -80,6 +81,7 @@ func newReverseProxy(target string) (*httputil.ReverseProxy, error) {
proxy.Transport = tpt
director := proxy.Director
proxy.Director = func(req *http.Request) {
fmt.Printf("proxy <= %v %v <= %v\n", req.Method, req.URL.String(), req.Header["X-Real-Ip"])
director(req)
logrus.Debugf("-> %v", req.URL.String())
req.Header.Set("X-Proxy", "zrok")

View File

@ -1,6 +1,7 @@
package webBackend
import (
"fmt"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
@ -38,7 +39,7 @@ func NewBackend(cfg *Config) (*backend, error) {
return &backend{
cfg: cfg,
listener: listener,
handler: http.FileServer(http.Dir(cfg.WebRoot)),
handler: &requestLogger{handler: http.FileServer(http.Dir(cfg.WebRoot))},
}, nil
}
@ -52,3 +53,12 @@ func (self *backend) Run() error {
func (self *backend) Requests() func() int32 {
return func() int32 { return 0 }
}
type requestLogger struct {
handler http.Handler
}
func (rl *requestLogger) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
fmt.Printf("web <= %v %v <= %v\n", req.Method, req.URL.String(), req.Header["X-Real-Ip"])
rl.handler.ServeHTTP(resp, req)
}

8
go.mod
View File

@ -4,7 +4,6 @@ go 1.18
require (
github.com/charmbracelet/lipgloss v0.6.0
github.com/gizak/termui/v3 v3.1.0
github.com/go-openapi/errors v0.20.2
github.com/go-openapi/loads v0.21.1
github.com/go-openapi/runtime v0.24.1
@ -15,13 +14,13 @@ require (
github.com/iancoleman/strcase v0.2.0
github.com/influxdata/influxdb-client-go/v2 v2.11.0
github.com/jaevor/go-nanoid v1.3.0
github.com/jedib0t/go-pretty/v6 v6.4.3
github.com/jessevdk/go-flags v1.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.0
github.com/mattn/go-sqlite3 v1.14.14
github.com/michaelquigley/cf v0.0.13
github.com/michaelquigley/pfxlog v0.6.9
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
github.com/opentracing/opentracing-go v1.2.0
github.com/openziti/edge v0.22.39
github.com/openziti/foundation/v2 v2.0.4
@ -31,9 +30,7 @@ require (
github.com/shirou/gopsutil/v3 v3.22.8
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569
github.com/wneessen/go-mail v0.2.7
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
@ -58,7 +55,6 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
@ -69,7 +65,6 @@ require (
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 // indirect
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0 // indirect
@ -86,6 +81,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/speps/go-hashids v2.0.0+incompatible // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect

9
go.sum
View File

@ -100,8 +100,6 @@ github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa h1:RDBNVkRviHZtvD
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gizak/termui/v3 v3.1.0 h1:ZZmVDgwHl7gR7elfKf1xc4IudXZ5qqfDh4wExk4Iajc=
github.com/gizak/termui/v3 v3.1.0/go.mod h1:bXQEBkJpzxUAKf0+xq9MSWAvWZlE7c+aidmyFlkYTrY=
github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@ -362,7 +360,6 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
@ -388,8 +385,6 @@ github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
@ -411,8 +406,6 @@ github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ
github.com/netfoundry/secretstream v0.1.2 h1:NgqrYytDnjKbOfWI29TT0SJM+RwB3yf9MIkJVJaU+J0=
github.com/netfoundry/secretstream v0.1.2/go.mod h1:uasYkYSp0MmNSlKOWJ2sVzxPms8e58TS4ENq4yro86k=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
@ -520,8 +513,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=