Skip to content

Commit

Permalink
Made the target host parsing more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanHabic committed Mar 2, 2019
1 parent 7bd0bcd commit a3bbe5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion commands/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var proxyPort string
var forceProxy bool

func init() {
proxyCmd.PersistentFlags().StringVar(&targetSchema, "target-schema", "http", "Blockchain rpc schema.")
proxyCmd.PersistentFlags().StringVar(&targetSchema, "target-schema", "", "Blockchain rpc schema.")
proxyCmd.PersistentFlags().StringVar(&targetHost, "target-host", "127.0.0.1", "Blockchain rpc host.")
proxyCmd.PersistentFlags().StringVar(&targetPort, "target-port", "8545", "Blockchain rpc port.")
proxyCmd.PersistentFlags().StringVar(&proxyHost, "proxy-host", "127.0.0.1", "Call host.")
Expand Down
39 changes: 31 additions & 8 deletions commands/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/logrusorgru/aurora"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -273,10 +275,13 @@ func isBatchRequest(data []byte) bool {

func Start(targetSchema, targetHost, targetPort, proxyHost, proxyPort, path string) error {
logrus.Infof("Proxy starting on %s:%s", proxyHost, proxyPort)
logrus.Infof("Redirecting calls to %s:%s", targetHost, targetPort)

projectPath = path
proxy, err := NewProxy(targetSchema + "://" + getTargetHost(targetHost) + ":" + targetPort)

host := getTargetHost(targetHost, targetSchema, targetPort)
logrus.Infof("Redirecting calls to %s", host)

proxy, err := NewProxy(host)
if err != nil {
userError.LogErrorf("failed starting proxy: %s", err)
os.Exit(1)
Expand All @@ -285,13 +290,31 @@ func Start(targetSchema, targetHost, targetPort, proxyHost, proxyPort, path stri
return http.ListenAndServe(proxyHost+":"+proxyPort, proxy)
}

func getTargetHost(targetHost string) string {
if strings.HasPrefix(targetHost, "http://") {
return targetHost[7:]
func getTargetHost(targetHost, targetSchema, targetPort string) string {
initialSchema := "http"
if strings.HasPrefix(targetHost, "https") {
initialSchema = "https"
}

re := regexp.MustCompile(`^(https?://|www\.)+`)

host := fmt.Sprintf("%s://%s", initialSchema, re.ReplaceAllString(targetHost, ""))

parsedUrl, err := url.Parse(host)
if err != nil {
userError.LogErrorf("couldn't parse target host: %s", userError.NewUserError(
err,
aurora.Sprintf("Couldn't parse target host: %s", aurora.Bold(aurora.Red(host))),
))
os.Exit(1)
}

if len(targetSchema) > 0 {
parsedUrl.Scheme = targetSchema
}
if strings.HasPrefix(targetHost, "https://") {
return targetHost[8:]
if len(targetPort) > 0 {
parsedUrl.Host = fmt.Sprintf("%s:%s", parsedUrl.Hostname(), targetPort)
}

return targetHost
return parsedUrl.String()
}

0 comments on commit a3bbe5b

Please sign in to comment.