improved url parsing; prefer http instead of https for local urls (#211)

This commit is contained in:
Michael Quigley 2023-02-08 11:21:24 -05:00
parent 3963259540
commit 5c58cc3240
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 30 additions and 18 deletions

View File

@ -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]

View File

@ -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]

View File

@ -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]

View File

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