2
0
Эх сурвалжийг харах

Adds wildcard support to `allowed_hosts` setting, as per PR #73 (#111)

Wildcards will also allow to use subdomains, eliminating the need for PR #81
Flashmob 7 жил өмнө
parent
commit
29dd65f67d
2 өөрчлөгдсөн 62 нэмэгдсэн , 1 устгасан
  1. 15 1
      server.go
  2. 47 0
      server_test.go

+ 15 - 1
server.go

@@ -17,6 +17,7 @@ import (
 	"github.com/flashmob/go-guerrilla/mail"
 	"github.com/flashmob/go-guerrilla/response"
 	"io/ioutil"
+	"path/filepath"
 )
 
 const (
@@ -66,6 +67,7 @@ type server struct {
 
 type allowedHosts struct {
 	table      map[string]bool // host lookup table
+	wildcards  []string        // host wildcard list (* is used as a wildcard)
 	sync.Mutex                 // guard access to the map
 }
 
@@ -195,8 +197,13 @@ func (server *server) setAllowedHosts(allowedHosts []string) {
 	server.hosts.Lock()
 	defer server.hosts.Unlock()
 	server.hosts.table = make(map[string]bool, len(allowedHosts))
+	server.hosts.wildcards = nil
 	for _, h := range allowedHosts {
-		server.hosts.table[strings.ToLower(h)] = true
+		if strings.Index(h, "*") != -1 {
+			server.hosts.wildcards = append(server.hosts.wildcards, strings.ToLower(h))
+		} else {
+			server.hosts.table[strings.ToLower(h)] = true
+		}
 	}
 }
 
@@ -278,6 +285,7 @@ func (server *server) GetActiveClientsCount() int {
 func (server *server) allowsHost(host string) bool {
 	server.hosts.Lock()
 	defer server.hosts.Unlock()
+	// if hosts contains a single dot, further processing is skipped
 	if len(server.hosts.table) == 1 {
 		if _, ok := server.hosts.table["."]; ok {
 			return true
@@ -286,6 +294,12 @@ func (server *server) allowsHost(host string) bool {
 	if _, ok := server.hosts.table[strings.ToLower(host)]; ok {
 		return true
 	}
+	// check the willdcards
+	for _, w := range server.hosts.wildcards {
+		if matched, err := filepath.Match(w, strings.ToLower(host)); matched && err == nil {
+			return true
+		}
+	}
 	return false
 }
 

+ 47 - 0
server_test.go

@@ -418,5 +418,52 @@ func TestGatewayPanic(t *testing.T) {
 
 }
 
+func TestAllowsHosts(t *testing.T) {
+	s := server{}
+	allowedHosts := []string{
+		"spam4.me",
+		"grr.la",
+		"newhost.com",
+		"example.*",
+		"*.test",
+		"wild*.card",
+		"multiple*wild*cards.*",
+	}
+	s.setAllowedHosts(allowedHosts)
+
+	testTable := map[string]bool{
+		"spam4.me":                true,
+		"dont.match":              false,
+		"example.com":             true,
+		"another.example.com":     false,
+		"anything.test":           true,
+		"wild.card":               true,
+		"wild.card.com":           false,
+		"multipleXwildXcards.com": true,
+	}
+
+	for host, allows := range testTable {
+		if res := s.allowsHost(host); res != allows {
+			t.Error(host, ": expected", allows, "but got", res)
+		}
+	}
+
+	// only wildcard - should match anything
+	s.setAllowedHosts([]string{"*"})
+	if !s.allowsHost("match.me") {
+		t.Error("match.me: expected true but got false")
+	}
+
+	// turns off
+	s.setAllowedHosts([]string{"."})
+	if !s.allowsHost("match.me") {
+		t.Error("match.me: expected true but got false")
+	}
+
+	// no wilcards
+	s.setAllowedHosts([]string{"grr.la", "example.com"})
+
+}
+
 // TODO
 // - test github issue #44 and #42