From 5c58cc32401e36e600a6e711c9abbc55ec5052f2 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 8 Feb 2023 11:21:24 -0500 Subject: [PATCH 1/3] improved url parsing; prefer http instead of https for local urls (#211) --- cmd/zrok/reserve.go | 8 ++------ cmd/zrok/sharePrivate.go | 8 ++------ cmd/zrok/sharePublic.go | 8 ++------ cmd/zrok/util.go | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cmd/zrok/reserve.go b/cmd/zrok/reserve.go index 077ce6b0..2dc28b9e 100644 --- a/cmd/zrok/reserve.go +++ b/cmd/zrok/reserve.go @@ -10,7 +10,6 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "net/url" "strings" ) @@ -48,17 +47,14 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) { var target string switch cmd.backendMode { case "proxy": - targetEndpoint, err := url.Parse(args[1]) + v, err := parseUrl(args[1]) if err != nil { if !panicInstead { tui.Error("invalid target endpoint URL", err) } panic(err) } - if targetEndpoint.Scheme == "" { - targetEndpoint.Scheme = "https" - } - target = targetEndpoint.String() + target = v case "web": target = args[1] diff --git a/cmd/zrok/sharePrivate.go b/cmd/zrok/sharePrivate.go index fca0b17a..2d4c1952 100644 --- a/cmd/zrok/sharePrivate.go +++ b/cmd/zrok/sharePrivate.go @@ -17,7 +17,6 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "net/url" "os" "os/signal" "strings" @@ -56,17 +55,14 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { switch cmd.backendMode { case "proxy": - targetEndpoint, err := url.Parse(args[0]) + v, err := parseUrl(args[0]) if err != nil { if !panicInstead { tui.Error("invalid target endpoint URL", err) } panic(err) } - if targetEndpoint.Scheme == "" { - targetEndpoint.Scheme = "https" - } - target = targetEndpoint.String() + target = v case "web": target = args[0] diff --git a/cmd/zrok/sharePublic.go b/cmd/zrok/sharePublic.go index 2ca340c5..a3b2b4d7 100644 --- a/cmd/zrok/sharePublic.go +++ b/cmd/zrok/sharePublic.go @@ -17,7 +17,6 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "net/url" "os" "os/signal" "strings" @@ -58,17 +57,14 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { switch cmd.backendMode { case "proxy": - targetEndpoint, err := url.Parse(args[0]) + v, err := parseUrl(args[0]) if err != nil { if !panicInstead { tui.Error("invalid target endpoint URL", err) } panic(err) } - if targetEndpoint.Scheme == "" { - targetEndpoint.Scheme = "https" - } - target = targetEndpoint.String() + target = v case "web": target = args[0] diff --git a/cmd/zrok/util.go b/cmd/zrok/util.go index 248b43f8..72b6367f 100644 --- a/cmd/zrok/util.go +++ b/cmd/zrok/util.go @@ -1,9 +1,13 @@ package main import ( + "fmt" "github.com/go-openapi/runtime" httptransport "github.com/go-openapi/runtime/client" + "net/url" "os" + "strconv" + "strings" ) func mustGetAdminAuth() runtime.ClientAuthInfoWriter { @@ -13,3 +17,23 @@ func mustGetAdminAuth() runtime.ClientAuthInfoWriter { } return httptransport.APIKeyAuth("X-TOKEN", "header", adminToken) } + +func parseUrl(in string) (string, error) { + // parse port-only urls + if iv, err := strconv.ParseInt(in, 10, 0); err == nil { + return fmt.Sprintf("http://127.0.0.1:%d", iv), nil + } + + // make sure either https:// or http:// was specified + if !strings.HasPrefix(in, "https://") && !strings.HasPrefix(in, "http://") { + in = "http://" + in + } + + // parse the url + targetEndpoint, err := url.Parse(in) + if err != nil { + return "", err + } + + return targetEndpoint.String(), nil +} From a30645171a25ff5a8077c49eb4d875008ccb8bb3 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 8 Feb 2023 11:36:33 -0500 Subject: [PATCH 2/3] tweaks to url parsing (#211) --- cmd/zrok/util.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/zrok/util.go b/cmd/zrok/util.go index 72b6367f..bff5c9fb 100644 --- a/cmd/zrok/util.go +++ b/cmd/zrok/util.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/go-openapi/runtime" httptransport "github.com/go-openapi/runtime/client" + "github.com/pkg/errors" "net/url" "os" "strconv" @@ -21,7 +22,13 @@ func mustGetAdminAuth() runtime.ClientAuthInfoWriter { func parseUrl(in string) (string, error) { // parse port-only urls if iv, err := strconv.ParseInt(in, 10, 0); err == nil { - return fmt.Sprintf("http://127.0.0.1:%d", iv), nil + if iv > 0 && iv < 65536 { + if iv == 443 { + return fmt.Sprintf("https://127.0.0.1:%d", iv), nil + } + return fmt.Sprintf("http://127.0.0.1:%d", iv), nil + } + return "", errors.Errorf("ports must be between 1 and 65536; %d is not", iv) } // make sure either https:// or http:// was specified From 8244910842efd818962b217447989f7e72b63401 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 8 Feb 2023 11:41:05 -0500 Subject: [PATCH 3/3] parsing tweaks (#211) --- cmd/zrok/util.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/zrok/util.go b/cmd/zrok/util.go index bff5c9fb..6a04d288 100644 --- a/cmd/zrok/util.go +++ b/cmd/zrok/util.go @@ -5,6 +5,7 @@ import ( "github.com/go-openapi/runtime" httptransport "github.com/go-openapi/runtime/client" "github.com/pkg/errors" + "math" "net/url" "os" "strconv" @@ -22,13 +23,13 @@ func mustGetAdminAuth() runtime.ClientAuthInfoWriter { func parseUrl(in string) (string, error) { // parse port-only urls if iv, err := strconv.ParseInt(in, 10, 0); err == nil { - if iv > 0 && iv < 65536 { + if iv > 0 && iv <= math.MaxUint16 { if iv == 443 { return fmt.Sprintf("https://127.0.0.1:%d", iv), nil } return fmt.Sprintf("http://127.0.0.1:%d", iv), nil } - return "", errors.Errorf("ports must be between 1 and 65536; %d is not", iv) + return "", errors.Errorf("ports must be between 1 and %d; %d is not", math.MaxUint16, iv) } // make sure either https:// or http:// was specified