Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved URL Parsing #222

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions cmd/zrok/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
"strings"
)

Expand Down Expand Up @@ -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]
Expand Down
8 changes: 2 additions & 6 deletions cmd/zrok/sharePrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 2 additions & 6 deletions cmd/zrok/sharePublic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -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]
Expand Down
32 changes: 32 additions & 0 deletions cmd/zrok/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package main

import (
"fmt"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/pkg/errors"
"math"
"net/url"
"os"
"strconv"
"strings"
)

func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
Expand All @@ -13,3 +19,29 @@ 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 {
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 %d; %d is not", math.MaxUint16, iv)
}

// 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
}