Skip to content

Commit

Permalink
Adds wildcard support to allowed_hosts setting, as per PR #73 (#111)
Browse files Browse the repository at this point in the history
Wildcards will also allow to use subdomains, eliminating the need for PR #81
  • Loading branch information
flashmob authored May 31, 2018
1 parent 9cec012 commit 29dd65f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
16 changes: 15 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/flashmob/go-guerrilla/mail"
"github.com/flashmob/go-guerrilla/response"
"io/ioutil"
"path/filepath"
)

const (
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
47 changes: 47 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 29dd65f

Please sign in to comment.