Merge pull request #222 from openziti/improved_url_parsing

Improved URL Parsing (#211
This commit is contained in:
Michael Quigley 2023-02-08 11:48:48 -05:00 committed by GitHub
commit b97f8b1d4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 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,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 {
@ -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
}