Skip to content

Commit 6110582

Browse files
committed
add banlist and tests
1 parent 60bfa4f commit 6110582

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

http/banlist/banlist.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package banlist
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"os"
7+
"os/signal"
8+
"strings"
9+
"sync"
10+
"sync/atomic"
11+
"syscall"
12+
13+
"github.com/pkg/errors"
14+
"github.com/sirupsen/logrus"
15+
)
16+
17+
type Config struct {
18+
Domains []string
19+
URLs []string
20+
}
21+
22+
type Banlist struct {
23+
domainHolder atomic.Value
24+
urlHolder atomic.Value
25+
mtx sync.Mutex
26+
ch chan os.Signal
27+
log logrus.FieldLogger
28+
path string
29+
}
30+
31+
func New(log logrus.FieldLogger, path string) *Banlist {
32+
bl := newBanlist(log, path)
33+
bl.listen()
34+
bl.runUpdate()
35+
return bl
36+
}
37+
38+
func newBanlist(log logrus.FieldLogger, path string) *Banlist {
39+
bl := &Banlist{log: log, path: path}
40+
bl.domainHolder.Store(make(map[string]struct{}))
41+
bl.urlHolder.Store(make(map[string]struct{}))
42+
return bl
43+
}
44+
45+
func (b *Banlist) listen() {
46+
b.ch = make(chan os.Signal, 1)
47+
signal.Notify(b.ch, syscall.SIGHUP)
48+
go func() {
49+
for range b.ch {
50+
b.runUpdate()
51+
}
52+
b.log.Info("No longer listening for SIGHUP")
53+
}()
54+
}
55+
56+
func (b *Banlist) runUpdate() {
57+
if err := b.update(); err != nil {
58+
b.log.WithError(err).Warn("error updating banlist")
59+
} else {
60+
b.log.Info("banlist updated")
61+
}
62+
}
63+
64+
func (b *Banlist) update() error {
65+
b.mtx.Lock()
66+
defer b.mtx.Unlock()
67+
68+
f, err := os.Open(b.path)
69+
if err != nil {
70+
return errors.Wrap(err, "error opening banlist config")
71+
}
72+
defer f.Close()
73+
74+
c := new(Config)
75+
if err := json.NewDecoder(f).Decode(c); err != nil {
76+
return errors.Wrap(err, "error decoding banlist config")
77+
}
78+
79+
domains := make(map[string]struct{})
80+
urls := make(map[string]struct{})
81+
for _, el := range c.Domains {
82+
domains[strings.ToLower(el)] = struct{}{}
83+
}
84+
for _, el := range c.URLs {
85+
urls[strings.ToLower(el)] = struct{}{}
86+
}
87+
88+
b.domainHolder.Store(domains)
89+
b.urlHolder.Store(urls)
90+
return nil
91+
}
92+
93+
// CheckRequest will check if the domain is blocked or the path is blocked
94+
func (b *Banlist) CheckRequest(r *http.Request) bool {
95+
domain := strings.SplitN(r.Host, ":", 2)[0]
96+
if _, ok := b.domains()[strings.ToLower(domain)]; ok {
97+
return true
98+
}
99+
100+
url := domain + r.URL.Path
101+
if _, ok := b.urls()[strings.ToLower(url)]; ok {
102+
return true
103+
}
104+
105+
return false
106+
}
107+
108+
func (b *Banlist) Close() {
109+
signal.Stop(b.ch)
110+
close(b.ch)
111+
}
112+
113+
func (b *Banlist) domains() map[string]struct{} {
114+
return b.domainHolder.Load().(map[string]struct{})
115+
}
116+
117+
func (b *Banlist) urls() map[string]struct{} {
118+
return b.urlHolder.Load().(map[string]struct{})
119+
}

http/banlist/banlist_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package banlist
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/httptest"
8+
"os"
9+
"testing"
10+
11+
"github.com/sirupsen/logrus"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func init() {
17+
logrus.SetLevel(logrus.DebugLevel)
18+
}
19+
20+
func TestBanlistMissingFile(t *testing.T) {
21+
bl := newBanlist(tl(t), "not a path")
22+
require.Error(t, bl.update())
23+
}
24+
25+
func TestBanlistInvalidFileContents(t *testing.T) {
26+
path, err := ioutil.TempFile("", "")
27+
require.NoError(t, err)
28+
defer os.Remove(path.Name())
29+
30+
_, err = path.WriteString("this isn't valid json")
31+
require.NoError(t, err)
32+
33+
bl := newBanlist(tl(t), path.Name())
34+
require.Error(t, bl.update())
35+
}
36+
37+
func TestBanlistNoPaths(t *testing.T) {
38+
bl := testList(t, &Config{
39+
Domains: []string{"something.com"},
40+
})
41+
42+
assert.Len(t, bl.urls(), 0)
43+
domains := bl.domains()
44+
assert.Len(t, domains, 1)
45+
_, ok := domains["something.com"]
46+
assert.True(t, ok)
47+
}
48+
49+
func TestBanlistNoDomains(t *testing.T) {
50+
bl := testList(t, &Config{
51+
URLs: []string{"something.com/path/to/thing"},
52+
})
53+
54+
urls := bl.urls()
55+
assert.Len(t, urls, 1)
56+
_, ok := urls["something.com/path/to/thing"]
57+
assert.True(t, ok)
58+
59+
assert.Len(t, bl.domains(), 0)
60+
}
61+
func TestBanlistBanning(t *testing.T) {
62+
bl := testList(t, &Config{
63+
URLs: []string{"villians.com/the/joker"},
64+
Domains: []string{"sick.com"},
65+
})
66+
67+
tests := []struct {
68+
url string
69+
isBanned bool
70+
name string
71+
}{
72+
{"http://heros.com", false, "completely unbanned"},
73+
{"http://sick.com:12345", true, "banned domain with port"},
74+
{"http://sick.com", true, "banned domain without port"},
75+
{"http://siCK.com", true, "banned domain mixed case"},
76+
{"http://villians.com:12354/the/joker", true, "banned path with port"},
77+
{"http://villians.com/the/joker", true, "banned path without port"},
78+
{"http://villians.com/the/Joker", true, "banned path mixed case"},
79+
{"http://villians.com/the/joker?query=param", true, "banned path with query params"},
80+
}
81+
for _, test := range tests {
82+
t.Run(test.name, func(t *testing.T) {
83+
req := httptest.NewRequest(http.MethodGet, test.url, nil)
84+
assert.Equal(t, test.isBanned, bl.CheckRequest(req))
85+
})
86+
}
87+
}
88+
89+
func tl(t *testing.T) logrus.FieldLogger {
90+
return logrus.WithField("test", t.Name())
91+
}
92+
93+
func testList(t *testing.T, config *Config) *Banlist {
94+
path, err := ioutil.TempFile("", "")
95+
require.NoError(t, err)
96+
defer os.Remove(path.Name())
97+
98+
require.NoError(t, json.NewEncoder(path).Encode(config))
99+
100+
bl := newBanlist(tl(t), path.Name())
101+
require.NoError(t, bl.update())
102+
return bl
103+
}

0 commit comments

Comments
 (0)