Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Implement support for whitelists, default-deny/allow #1

Closed
wants to merge 14 commits into from
Next Next commit
Implement support for whitelists, default-deny/allow
arrdem committed Mar 30, 2017
commit 0bf6dc037c7468ae965aea9b2e9703f6ffd64540
33 changes: 25 additions & 8 deletions filter.go
Original file line number Diff line number Diff line change
@@ -8,21 +8,34 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)

type FilterEntry struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be unexported as it seems to have no use externally.

f *net.IPNet
reject bool
}

type Filters struct {
mu sync.RWMutex
filters map[string]*net.IPNet
mu sync.RWMutex
filterDefault bool
filters map[string]*FilterEntry
}

func NewFilters() *Filters {
return &Filters{
filters: make(map[string]*net.IPNet),
filterDefault: false,
filters: make(map[string]*FilterEntry),
}
}

func (fs *Filters) AddDialFilter(f *net.IPNet) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.filters[f.String()] = f
fs.filters[f.String()] = &FilterEntry{f: f, reject: true}
}

func (fs *Filters) AddAllowFilter(f *net.IPNet) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.filters[f.String()] = &FilterEntry{f: f, reject: false}
}

func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
@@ -42,20 +55,24 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {

f.mu.RLock()
defer f.mu.RUnlock()

var flag bool = f.filterDefault

for _, ft := range f.filters {
if ft.Contains(netip) {
return true
if ft.f.Contains(netip) {
flag = ft.reject
}
}
return false

return flag
}

func (f *Filters) Filters() []*net.IPNet {
var out []*net.IPNet
f.mu.RLock()
defer f.mu.RUnlock()
for _, ff := range f.filters {
out = append(out, ff)
out = append(out, ff.f)
}
return out
}