mirror of
https://github.com/openziti/zrok.git
synced 2024-11-08 01:04:08 +01:00
improved url parsing; prefer http instead of https for local urls (#211)
This commit is contained in:
parent
3963259540
commit
5c58cc3240
@ -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]
|
||||
|
@ -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]
|
||||
|
@ -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]
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user