From e31dc8cfaa99e1f26cdbe8372491fe53e161d447 Mon Sep 17 00:00:00 2001 From: Michael Irwin Date: Wed, 26 Apr 2017 00:10:35 -0400 Subject: [PATCH] Replaced all uses of underscore in proxy URLs with dashes Issue #128 --- api.go | 21 ++++++++++++++------- handlers/reverseproxy.go | 4 ++-- www/assets/app.js | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/api.go b/api.go index 1bcb75b..c6da33f 100644 --- a/api.go +++ b/api.go @@ -65,8 +65,8 @@ func main() { corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"})) // Specific routes - r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).Handler(tcpHandler) - r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).Handler(tcpHandler) + r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).Handler(tcpHandler) + r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}}.{tld:.*}`).Handler(tcpHandler) r.HandleFunc("/ping", handlers.Ping).Methods("GET") corsRouter.HandleFunc("/instances/images", handlers.GetInstanceImages).Methods("GET") corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET") @@ -116,7 +116,7 @@ func main() { ssl := mux.NewRouter() sslProxyHandler := handlers.NewSSLDaemonHandler() - ssl.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-2375.{tld:.*}`).Handler(sslProxyHandler) + ssl.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}}-2375.{tld:.*}`).Handler(sslProxyHandler) log.Println("Listening TLS on port " + config.SSLPortNumber) s := &http.Server{Addr: "0.0.0.0:" + config.SSLPortNumber, Handler: ssl} @@ -125,7 +125,8 @@ func main() { chunks := strings.Split(clientHello.ServerName, ".") chunks = strings.Split(chunks[0], "-") - ip := strings.Replace(strings.TrimPrefix(chunks[0], "pwd"), "_", ".", -1) + ipAndPort := strings.TrimPrefix(chunks[0], "pwd") + ip := strings.Replace(ipAndPort[0:strings.LastIndex(ipAndPort, "-")], "-", ".", -1) i := services.FindInstanceByIP(ip) if i == nil { return nil, fmt.Errorf("Instance %s doesn't exist", clientHello.ServerName) @@ -138,15 +139,21 @@ func main() { log.Fatal(s.ListenAndServeTLS("", "")) } -var dnsFilter = regexp.MustCompile(`pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}`) +var dnsFilter = regexp.MustCompile(`pwd[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}`) func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) { if len(r.Question) > 0 && dnsFilter.MatchString(r.Question[0].Name) { // this is something we know about and we should try to handle question := r.Question[0].Name domainChunks := strings.Split(question, ".") - tldChunks := strings.Split(strings.TrimPrefix(domainChunks[0], "pwd"), "-") - ip := strings.Replace(tldChunks[0], "_", ".", -1) + ipAndPort := strings.TrimPrefix(domainChunks[0], "pwd") + ip := "" + + if len(strings.Split(ipAndPort, "-")) == 5 { + ip = strings.Replace(ipAndPort[0:strings.LastIndex(ipAndPort, "-")], "-", ".", -1) + } else { + ip = strings.Replace(ipAndPort, "-", ".", -1) + } m := new(dns.Msg) m.SetReply(r) diff --git a/handlers/reverseproxy.go b/handlers/reverseproxy.go index cbc5ae8..9686e18 100644 --- a/handlers/reverseproxy.go +++ b/handlers/reverseproxy.go @@ -27,7 +27,7 @@ func getTargetInfo(vars map[string]string, req *http.Request) (string, string) { if strings.HasPrefix(node, "pwd") { // Node is actually an ip, need to convert underscores by dots. - ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "_", ".", -1) + ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "-", ".", -1) if net.ParseIP(ip) == nil { // Not a valid IP, so treat this is a hostname. @@ -132,7 +132,7 @@ func NewSSLDaemonHandler() http.Handler { node := v["node"] if strings.HasPrefix(node, "pwd") { // Node is actually an ip, need to convert underscores by dots. - ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "_", ".", -1) + ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "-", ".", -1) if net.ParseIP(ip) == nil { // Not a valid IP, so treat this is a hostname. diff --git a/www/assets/app.js b/www/assets/app.js index 04b8a90..5985ff7 100644 --- a/www/assets/app.js +++ b/www/assets/app.js @@ -185,7 +185,7 @@ } $scope.getProxyUrl = function(instance, port) { - var url = window.location.protocol + '//pwd' + instance.ip.replace(/\./g, '_') + '-' + port + '.' + window.location.host; + var url = window.location.protocol + '//pwd' + instance.ip.replace(/\./g, '-') + '-' + port + '.' + window.location.host; return url; }