zrok/cmd/zrok/util.go
2023-02-08 11:41:05 -05:00

48 lines
1.1 KiB
Go

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 {
adminToken := os.Getenv("ZROK_ADMIN_TOKEN")
if adminToken == "" {
panic("please set ZROK_ADMIN_TOKEN to a valid admin token for your zrok instance")
}
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
}