diff --git a/api.go b/api.go index 4d0317b..7ec2981 100644 --- a/api.go +++ b/api.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" "os" @@ -60,10 +61,10 @@ 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(`pwd{alias:.*}-{session:.*}-{port:[0-9]*}.{tld:.*}`).Handler(tcpHandler) - r.Host(`pwd{alias:.*}-{session:.*}.{tld:.*}`).Handler(tcpHandler) + r.Host(fmt.Sprintf("{subdomain:.*}pwd{node:%s}-{port:%s}.{tld:.*}", config.PWDHostnameRegex, config.PortRegex)).Handler(tcpHandler) + r.Host(fmt.Sprintf("{subdomain:.*}pwd{node:%s}.{tld:.*}", config.PWDHostnameRegex)).Handler(tcpHandler) + r.Host(fmt.Sprintf("pwd{alias:%s}-{session:%s}-{port:%s}.{tld:.*}", config.AliasnameRegex, config.AliasSessionRegex, config.PortRegex)).Handler(tcpHandler) + r.Host(fmt.Sprintf("pwd{alias:%s}-{session:%s}.{tld:.*}", config.AliasnameRegex, config.AliasSessionRegex)).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") diff --git a/config/config.go b/config/config.go index 461baa1..0981d94 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,22 @@ package config -import "flag" +import ( + "flag" + "regexp" +) + +const ( + PWDHostnameRegex = "[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}" + PortRegex = "[0-9]{1,5}" + AliasnameRegex = "[0-9|a-z|A-Z|-]*" + AliasSessionRegex = "[0-9|a-z|A-Z]{8}" + AliasGroupRegex = "(" + AliasnameRegex + ")-(" + AliasSessionRegex + ")" + PWDHostPortGroupRegex = "^.*pwd(" + PWDHostnameRegex + ")(?:-?(" + PortRegex + "))?\\..*$" + AliasPortGroupRegex = "^.*pwd" + AliasGroupRegex + "(?:-?(" + PortRegex + "))?\\..*$" +) + +var NameFilter = regexp.MustCompile(PWDHostPortGroupRegex) +var AliasFilter = regexp.MustCompile(AliasPortGroupRegex) var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey string var MaxLoadAvg float64 diff --git a/handlers/dns.go b/handlers/dns.go index fdec640..7cdd27e 100644 --- a/handlers/dns.go +++ b/handlers/dns.go @@ -4,22 +4,19 @@ import ( "fmt" "log" "net" - "regexp" "strings" "github.com/miekg/dns" + "github.com/play-with-docker/play-with-docker/config" "github.com/play-with-docker/play-with-docker/services" ) -var dnsFilter = regexp.MustCompile(`^.*pwd([0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3})(?:-[0-9]{1,5})?\..*$`) -var aliasFilter = regexp.MustCompile(`^.*pwd(.*?)-(.*?)[\.-].*`) - func DnsRequest(w dns.ResponseWriter, r *dns.Msg) { - if len(r.Question) > 0 && dnsFilter.MatchString(r.Question[0].Name) { + if len(r.Question) > 0 && config.NameFilter.MatchString(r.Question[0].Name) { // this is something we know about and we should try to handle question := r.Question[0].Name - match := dnsFilter.FindStringSubmatch(question) + match := config.NameFilter.FindStringSubmatch(question) ip := strings.Replace(match[1], "-", ".", -1) @@ -34,11 +31,11 @@ func DnsRequest(w dns.ResponseWriter, r *dns.Msg) { m.Answer = append(m.Answer, a) w.WriteMsg(m) return - } else if len(r.Question) > 0 && aliasFilter.MatchString(r.Question[0].Name) { + } else if len(r.Question) > 0 && config.AliasFilter.MatchString(r.Question[0].Name) { // this is something we know about and we should try to handle question := r.Question[0].Name - match := aliasFilter.FindStringSubmatch(question) + match := config.AliasFilter.FindStringSubmatch(question) i := services.FindInstanceByAlias(match[2], match[1]) diff --git a/handlers/reverseproxy.go b/handlers/reverseproxy.go index b1449a5..4a2fbc8 100644 --- a/handlers/reverseproxy.go +++ b/handlers/reverseproxy.go @@ -36,15 +36,13 @@ 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) + // Node is actually an ip, need to convert underscores by dots. + ip := strings.Replace(node, "-", ".", -1) - if net.ParseIP(ip) == nil { - // Not a valid IP, so treat this is a hostname. - } else { - node = ip - } + if net.ParseIP(ip) == nil { + // Not a valid IP, so treat this is a hostname. + } else { + node = ip } return node, port @@ -136,28 +134,3 @@ func NewTCPProxy() http.Handler { } return &tcpProxy{Director: director} } - -func NewSSLDaemonHandler() http.Handler { - director := func(req *http.Request) { - v := mux.Vars(req) - 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) - - if net.ParseIP(ip) == nil { - // Not a valid IP, so treat this is a hostname. - } else { - node = ip - } - } - - // Only proxy http for now - req.URL.Scheme = "http" - - req.URL.Host = fmt.Sprintf("%s:%s", node, "2375") - log.Printf("HTTPS Reverse proxying to %s\n", req.URL.Host) - } - - return &tcpProxy{Director: director} -} diff --git a/handlers/tlsproxy.go b/handlers/tlsproxy.go index e6194c6..34a7919 100644 --- a/handlers/tlsproxy.go +++ b/handlers/tlsproxy.go @@ -5,16 +5,14 @@ import ( "io" "log" "net" - "regexp" "strings" vhost "github.com/inconshreveable/go-vhost" + "github.com/play-with-docker/play-with-docker/config" "github.com/play-with-docker/play-with-docker/services" ) func StartTLSProxy(port string) { - var validProxyHost = regexp.MustCompile(`^.*pwd([0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3})(?:-?([0-9]{1,5}))?\..*$`) - var validAliasProxyHost = regexp.MustCompile(`^.*pwd([0-9|a-z|A-Z]*)-([0-9|a-z|A-Z]{8})(?:-?([0-9]{1,5}))?\..*$`) tlsListener, tlsErr := net.Listen("tcp", fmt.Sprintf(":%s", port)) log.Println("Listening on port " + port) @@ -43,10 +41,10 @@ func StartTLSProxy(port string) { targetPort := "443" host := vhostConn.ClientHelloMsg.ServerName - match := validProxyHost.FindStringSubmatch(host) + match := config.NameFilter.FindStringSubmatch(host) if len(match) < 2 { // Not a valid proxy host, try alias hosts - match := validAliasProxyHost.FindStringSubmatch(host) + match := config.AliasFilter.FindStringSubmatch(host) if len(match) < 4 { // Not valid, just close the connection return