Remove duplicated regex and unused function

This commit is contained in:
Marcos Lilljedahl
2017-05-15 14:30:42 -03:00
parent aa02432c56
commit a6c19e451f
5 changed files with 36 additions and 51 deletions

9
api.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@@ -60,10 +61,10 @@ func main() {
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"})) corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))
// Specific routes // 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(fmt.Sprintf("{subdomain:.*}pwd{node:%s}-{port:%s}.{tld:.*}", config.PWDHostnameRegex, config.PortRegex)).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(fmt.Sprintf("{subdomain:.*}pwd{node:%s}.{tld:.*}", config.PWDHostnameRegex)).Handler(tcpHandler)
r.Host(`pwd{alias:.*}-{session:.*}-{port:[0-9]*}.{tld:.*}`).Handler(tcpHandler) r.Host(fmt.Sprintf("pwd{alias:%s}-{session:%s}-{port:%s}.{tld:.*}", config.AliasnameRegex, config.AliasSessionRegex, config.PortRegex)).Handler(tcpHandler)
r.Host(`pwd{alias:.*}-{session:.*}.{tld:.*}`).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") r.HandleFunc("/ping", handlers.Ping).Methods("GET")
corsRouter.HandleFunc("/instances/images", handlers.GetInstanceImages).Methods("GET") corsRouter.HandleFunc("/instances/images", handlers.GetInstanceImages).Methods("GET")
corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET") corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")

View File

@@ -1,6 +1,22 @@
package config 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 SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey string
var MaxLoadAvg float64 var MaxLoadAvg float64

View File

@@ -4,22 +4,19 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"regexp"
"strings" "strings"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/services" "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) { 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 // this is something we know about and we should try to handle
question := r.Question[0].Name question := r.Question[0].Name
match := dnsFilter.FindStringSubmatch(question) match := config.NameFilter.FindStringSubmatch(question)
ip := strings.Replace(match[1], "-", ".", -1) ip := strings.Replace(match[1], "-", ".", -1)
@@ -34,11 +31,11 @@ func DnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m.Answer = append(m.Answer, a) m.Answer = append(m.Answer, a)
w.WriteMsg(m) w.WriteMsg(m)
return 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 // this is something we know about and we should try to handle
question := r.Question[0].Name question := r.Question[0].Name
match := aliasFilter.FindStringSubmatch(question) match := config.AliasFilter.FindStringSubmatch(question)
i := services.FindInstanceByAlias(match[2], match[1]) i := services.FindInstanceByAlias(match[2], match[1])

View File

@@ -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.
// Node is actually an ip, need to convert underscores by dots. ip := strings.Replace(node, "-", ".", -1)
ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "-", ".", -1)
if net.ParseIP(ip) == nil { if net.ParseIP(ip) == nil {
// Not a valid IP, so treat this is a hostname. // Not a valid IP, so treat this is a hostname.
} else { } else {
node = ip node = ip
}
} }
return node, port return node, port
@@ -136,28 +134,3 @@ func NewTCPProxy() http.Handler {
} }
return &tcpProxy{Director: director} 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}
}

View File

@@ -5,16 +5,14 @@ import (
"io" "io"
"log" "log"
"net" "net"
"regexp"
"strings" "strings"
vhost "github.com/inconshreveable/go-vhost" vhost "github.com/inconshreveable/go-vhost"
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/services" "github.com/play-with-docker/play-with-docker/services"
) )
func StartTLSProxy(port string) { 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)) tlsListener, tlsErr := net.Listen("tcp", fmt.Sprintf(":%s", port))
log.Println("Listening on port " + port) log.Println("Listening on port " + port)
@@ -43,10 +41,10 @@ func StartTLSProxy(port string) {
targetPort := "443" targetPort := "443"
host := vhostConn.ClientHelloMsg.ServerName host := vhostConn.ClientHelloMsg.ServerName
match := validProxyHost.FindStringSubmatch(host) match := config.NameFilter.FindStringSubmatch(host)
if len(match) < 2 { if len(match) < 2 {
// Not a valid proxy host, try alias hosts // Not a valid proxy host, try alias hosts
match := validAliasProxyHost.FindStringSubmatch(host) match := config.AliasFilter.FindStringSubmatch(host)
if len(match) < 4 { if len(match) < 4 {
// Not valid, just close the connection // Not valid, just close the connection
return return