Commit 8ad672a7 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

Merge pull request #1433 from ipfs/fix/addr-filter

add filters from config to addr filter in swarm
parents bafe1b04 1c1e389d
......@@ -9,11 +9,17 @@ import (
)
type Filters struct {
filters []*net.IPNet
filters map[string]*net.IPNet
}
func NewFilters() *Filters {
return &Filters{
filters: make(map[string]*net.IPNet),
}
}
func (fs *Filters) AddDialFilter(f *net.IPNet) {
fs.filters = append(fs.filters, f)
fs.filters[f.String()] = f
}
func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
......@@ -32,3 +38,15 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
}
return false
}
func (f *Filters) Filters() []*net.IPNet {
var out []*net.IPNet
for _, ff := range f.filters {
out = append(out, ff)
}
return out
}
func (f *Filters) Remove(ff *net.IPNet) {
delete(f.filters, ff.String())
}
......@@ -20,6 +20,7 @@ import (
pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
prom "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)
......@@ -83,7 +84,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee),
bwc: bwc,
Filters: new(filter.Filters),
Filters: filter.NewFilters(),
}
// configure Swarm
......@@ -101,6 +102,16 @@ func (s *Swarm) teardown() error {
return s.swarm.Close()
}
func (s *Swarm) AddAddrFilter(f string) error {
m, err := mafilter.NewMask(f)
if err != nil {
return err
}
s.Filters.AddDialFilter(m)
return nil
}
// CtxGroup returns the Context Group of the swarm
func filterAddrs(listenAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
if len(listenAddrs) > 0 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment