Skip to content

Commit

Permalink
'--agent' for 'zrok access private' (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Sep 17, 2024
1 parent 9d829c1 commit e6a74ad
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cmd/zrok/accessPrivate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"encoding/json"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
Expand Down Expand Up @@ -30,6 +32,7 @@ func init() {
type accessPrivateCommand struct {
bindAddress string
headless bool
agent bool
responseHeaders []string
cmd *cobra.Command
}
Expand All @@ -42,6 +45,8 @@ func newAccessPrivateCommand() *accessPrivateCommand {
}
command := &accessPrivateCommand{cmd: cmd}
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.agent, "agent", false, "Enable agent mode")
cmd.MarkFlagsMutuallyExclusive("headless", "agent")
cmd.Flags().StringVarP(&command.bindAddress, "bind", "b", "127.0.0.1:9191", "The address to bind the private frontend")
cmd.Flags().StringArrayVar(&command.responseHeaders, "response-header", []string{}, "Add a response header ('key:value')")
cmd.Run = command.run
Expand Down Expand Up @@ -81,7 +86,19 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
}
panic(err)
}
logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken)

if cmd.agent {
data := make(map[string]interface{})
data["frontend-token"] = accessResp.Payload.FrontendToken
data["bind-address"] = cmd.bindAddress
jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
} else {
logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken)
}

protocol := "http://"
switch accessResp.Payload.BackendMode {
Expand Down Expand Up @@ -231,6 +248,21 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
}
}

} else if cmd.agent {
for {
select {
case req := <-requests:
data := make(map[string]interface{})
data["remote-address"] = req.RemoteAddr
data["method"] = req.Method
data["path"] = req.Path
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(jsonData))
}
}
} else {
mdl := newAccessModel(shrToken, endpointUrl.String())
logrus.SetOutput(mdl)
Expand Down

0 comments on commit e6a74ad

Please sign in to comment.