headless mode for public sharing (#56)

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

View File

@ -10,22 +10,22 @@ import (
)
type shareModel struct {
shareToken string
frontendEndpoints []string
shareMode string
backendMode string
requests []*endpoints.BackendRequest
logMessages []string
width int
height int
shareToken string
frontendDescriptions []string
shareMode string
backendMode string
requests []*endpoints.BackendRequest
logMessages []string
width int
height int
}
func newShareModel(shareToken string, frontendEndpoints []string, shareMode, backendMode string) *shareModel {
return &shareModel{
shareToken: shareToken,
frontendEndpoints: frontendEndpoints,
shareMode: shareMode,
backendMode: backendMode,
shareToken: shareToken,
frontendDescriptions: frontendEndpoints,
shareMode: shareMode,
backendMode: backendMode,
}
}
@ -45,7 +45,7 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
configHeaderStyle.Width(26)
m.height = msg.Height
requestsStyle.Width(m.width - 2)
requestsStyle.Height(m.height - (len(m.frontendEndpoints) + 6))
requestsStyle.Height(m.height - (len(m.frontendDescriptions) + 6))
case tea.KeyMsg:
switch msg.String() {
@ -61,7 +61,7 @@ func (m *shareModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *shareModel) View() string {
topRow := lipgloss.JoinHorizontal(lipgloss.Top,
shareHeaderStyle.Render(strings.Join(m.frontendEndpoints, "\n")),
shareHeaderStyle.Render(strings.Join(m.frontendDescriptions, "\n")),
configHeaderStyle.Render(m.renderConfig()),
)
requests := requestsStyle.Render(m.renderBackendRequests())

View File

@ -32,6 +32,7 @@ type sharePublicCommand struct {
basicAuth []string
frontendSelection []string
backendMode string
headless bool
cmd *cobra.Command
}
@ -45,6 +46,7 @@ func newSharePublicCommand() *sharePublicCommand {
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}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Run = command.run
return command
}
@ -177,30 +179,35 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
_ = bh.Requests()()
//logrus.Infof("access your zrok share: %v", resp.Payload.FrontendProxyEndpoints[0])
//for {
// time.Sleep(5 * time.Second)
// logrus.Infof("requests: %d", bh.Requests()())
//}
mdl := newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints, "public", cmd.backendMode)
prg := tea.NewProgram(mdl, tea.WithAltScreen())
go func() {
if cmd.headless {
logrus.Infof("access your zrok share at the following endpoints:\n %v", strings.Join(resp.Payload.FrontendProxyEndpoints, "\n"))
for {
select {
case req := <-requestsChan:
prg.Send(req)
logrus.Infof("%v -> %v %v", req.RemoteAddr, req.Method, req.Path)
}
}
}()
if _, err := prg.Run(); err != nil {
tui.Error("An error occurred", err)
} else {
mdl := newShareModel(resp.Payload.ShrToken, resp.Payload.FrontendProxyEndpoints, "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)
}
close(requestsChan)
cmd.destroy(zrd.Env.ZId, resp.Payload.ShrToken, zrok, auth)
}
func (cmd *sharePublicCommand) proxyBackendMode(cfg *proxyBackend.Config) (endpoints.BackendHandler, error) {