Skip to content

Commit

Permalink
update 'zrok share reserved' to latest subordinate protocol (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Nov 12, 2024
1 parent ef5e654 commit bf3a470
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cmd/zrok/sharePublic.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}

if !root.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
cmd.error("unable to create share", errors.New("unable to load environment; did you 'zrok enable'?"))
}

if cmd.subordinate || cmd.forceLocal {
Expand Down
120 changes: 47 additions & 73 deletions cmd/zrok/shareReserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/tui"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -66,14 +67,11 @@ func newShareReservedCommand() *shareReservedCommand {
func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
root, err := environment.LoadRoot()
if err != nil {
if !panicInstead {
tui.Error("error loading environment", err)
}
panic(err)
cmd.error("error loading environment", err)
}

if !root.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
cmd.error("unable to create share", errors.New("unable to load environment; did you 'zrok enable'?"))
}

if cmd.subordinate || cmd.forceLocal {
Expand All @@ -100,20 +98,14 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

zrok, err := root.Client()
if err != nil {
if !panicInstead {
tui.Error("unable to create zrok client", err)
}
panic(err)
cmd.error("unable to create zrok client", err)
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
req := metadata.NewGetShareDetailParams()
req.ShrToken = shrToken
resp, err := zrok.Metadata.GetShareDetail(req, auth)
if err != nil {
if !panicInstead {
tui.Error("unable to retrieve reserved share", err)
}
panic(err)
cmd.error("unable to retrieve reserved share", err)
}
target = cmd.overrideEndpoint
if target == "" {
Expand All @@ -125,10 +117,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

zif, err := root.ZitiIdentityNamed(root.EnvironmentIdentityName())
if err != nil {
if !panicInstead {
tui.Error("unable to load ziti identity configuration", err)
}
panic(err)
cmd.error("unable to load ziti identity configuration", err)
}

if resp.Payload.BackendMode != "socks" {
Expand All @@ -143,10 +132,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {
BackendProxyEndpoint: target,
}
if _, err := zrok.Share.UpdateShare(upReq, auth); err != nil {
if !panicInstead {
tui.Error("unable to update backend target", err)
}
panic(err)
cmd.error("unable to update backend target", err)
}
if !cmd.subordinate {
logrus.Infof("updated backend target to: %v", target)
Expand All @@ -166,24 +152,6 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {
shareDescription = fmt.Sprintf("access your share with: %v", tui.Code.Render(fmt.Sprintf("zrok access private %v", shrToken)))
}

if cmd.subordinate {
data := make(map[string]interface{})
data["token"] = resp.Payload.Token
data["backend_mode"] = resp.Payload.BackendMode
data["share_mode"] = resp.Payload.ShareMode
if resp.Payload.FrontendEndpoint != "" {
data["frontend_endpoints"] = resp.Payload.FrontendEndpoint
}
if resp.Payload.BackendProxyEndpoint != "" {
data["target"] = resp.Payload.BackendProxyEndpoint
}
jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
}

mdl := newShareModel(shrToken, []string{shareDescription}, sdk.ShareMode(resp.Payload.ShareMode), sdk.BackendMode(resp.Payload.BackendMode))
if !cmd.headless && !cmd.subordinate {
proxy.SetCaddyLoggingWriter(mdl)
Expand All @@ -202,10 +170,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := proxy.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create proxy backend handler", err)
}
panic(err)
cmd.error("unable to create 'proxy' backend", err)
}

go func() {
Expand All @@ -224,10 +189,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := proxy.NewCaddyWebBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating web backend", err)
}
panic(err)
cmd.error("unable to create 'web' backend", err)
}

go func() {
Expand All @@ -246,10 +208,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := tcpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating tcpTunnel backend", err)
}
panic(err)
cmd.error("unable to create 'tcpTunnel' backend", err)
}

go func() {
Expand All @@ -268,10 +227,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := udpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating udpTunnel backend", err)
}
panic(err)
cmd.error("unable to create 'udpTunnel' backend", err)
}

go func() {
Expand All @@ -289,10 +245,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := proxy.NewCaddyfileBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating caddy backend", err)
}
panic(err)
cmd.error("unable to create 'caddy' backend", err)
}

go func() {
Expand All @@ -311,10 +264,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := drive.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating drive backend", err)
}
panic(err)
cmd.error("unable to create 'drive' backend", err)
}

go func() {
Expand All @@ -332,10 +282,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := socks.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating socks backend", err)
}
panic(err)
cmd.error("unable to create 'socks' backend", err)
}

go func() {
Expand All @@ -354,10 +301,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {

be, err := vpn.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("error creating VPN backend", err)
}
panic(err)
cmd.error("unable to create 'vpn' backend", err)
}

go func() {
Expand All @@ -367,10 +311,29 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {
}()

default:
tui.Error("invalid backend mode", nil)
cmd.error("unable to share", errors.New("invalid backend mode"))
}

if cmd.subordinate {
data := make(map[string]interface{})
data["message"] = "boot"
data["token"] = resp.Payload.Token
data["backend_mode"] = resp.Payload.BackendMode
data["share_mode"] = resp.Payload.ShareMode
if resp.Payload.FrontendEndpoint != "" {
data["frontend_endpoints"] = resp.Payload.FrontendEndpoint
}
if resp.Payload.BackendProxyEndpoint != "" {
data["target"] = resp.Payload.BackendProxyEndpoint
}
jsonData, err := json.Marshal(data)
if err != nil {
cmd.error("unable to marshal", err)
}
fmt.Println(string(jsonData))
}

if cmd.headless {
if cmd.headless && !cmd.subordinate {
switch resp.Payload.ShareMode {
case string(sdk.PublicShareMode):
logrus.Infof("access your zrok share: %v", resp.Payload.FrontendEndpoint)
Expand All @@ -390,6 +353,7 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {
select {
case req := <-requests:
data := make(map[string]interface{})
data["message"] = "access"
data["remote-address"] = req.RemoteAddr
data["method"] = req.Method
data["path"] = req.Path
Expand Down Expand Up @@ -423,6 +387,16 @@ func (cmd *shareReservedCommand) shareLocal(args []string, root env_core.Root) {
}
}

func (cmd *shareReservedCommand) error(msg string, err error) {
if cmd.subordinate {
subordinateError(errors.Wrap(err, msg))
}
if !panicInstead {
tui.Error(msg, err)
}
panic(errors.Wrap(err, msg))
}

func (cmd *shareReservedCommand) shareAgent(args []string, root env_core.Root) {
logrus.Info("starting")

Expand Down

0 comments on commit bf3a470

Please sign in to comment.