mirror of
https://github.com/openziti/zrok.git
synced 2024-11-26 10:04:16 +01:00
Merge pull request #222 from openziti/improved_url_parsing
Improved URL Parsing (#211
This commit is contained in:
commit
b97f8b1d4f
@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,17 +47,14 @@ func (cmd *reserveCommand) run(_ *cobra.Command, args []string) {
|
|||||||
var target string
|
var target string
|
||||||
switch cmd.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[1])
|
v, err := parseUrl(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
tui.Error("invalid target endpoint URL", err)
|
tui.Error("invalid target endpoint URL", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if targetEndpoint.Scheme == "" {
|
target = v
|
||||||
targetEndpoint.Scheme = "https"
|
|
||||||
}
|
|
||||||
target = targetEndpoint.String()
|
|
||||||
|
|
||||||
case "web":
|
case "web":
|
||||||
target = args[1]
|
target = args[1]
|
||||||
|
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
@ -56,17 +55,14 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
|
|
||||||
switch cmd.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[0])
|
v, err := parseUrl(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
tui.Error("invalid target endpoint URL", err)
|
tui.Error("invalid target endpoint URL", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if targetEndpoint.Scheme == "" {
|
target = v
|
||||||
targetEndpoint.Scheme = "https"
|
|
||||||
}
|
|
||||||
target = targetEndpoint.String()
|
|
||||||
|
|
||||||
case "web":
|
case "web":
|
||||||
target = args[0]
|
target = args[0]
|
||||||
|
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
@ -58,17 +57,14 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
|
|
||||||
switch cmd.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
targetEndpoint, err := url.Parse(args[0])
|
v, err := parseUrl(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
tui.Error("invalid target endpoint URL", err)
|
tui.Error("invalid target endpoint URL", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if targetEndpoint.Scheme == "" {
|
target = v
|
||||||
targetEndpoint.Scheme = "https"
|
|
||||||
}
|
|
||||||
target = targetEndpoint.String()
|
|
||||||
|
|
||||||
case "web":
|
case "web":
|
||||||
target = args[0]
|
target = args[0]
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/go-openapi/runtime"
|
"github.com/go-openapi/runtime"
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"math"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
|
func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
|
||||||
@ -13,3 +19,29 @@ func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
|
|||||||
}
|
}
|
||||||
return httptransport.APIKeyAuth("X-TOKEN", "header", adminToken)
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user