Remove duplicated regex and unused function
This commit is contained in:
@@ -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])
|
||||
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user