Commit 9a3d287c authored by Jeromy's avatar Jeromy Committed by Juan Batiz-Benet
Browse files

broke filters out into a struct



License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 89c1c828
......@@ -16,6 +16,7 @@ import (
ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
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"
......@@ -52,7 +53,7 @@ type Swarm struct {
notifs map[inet.Notifiee]ps.Notifiee
// filters for addresses that shouldnt be dialed
filters []*net.IPNet
Filters *Filters
cg ctxgroup.ContextGroup
bwc metrics.Reporter
......@@ -68,13 +69,14 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
}
s := &Swarm{
swarm: ps.NewSwarm(PSTransport),
local: local,
peers: peers,
cg: ctxgroup.WithContext(ctx),
dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee),
bwc: bwc,
swarm: ps.NewSwarm(PSTransport),
local: local,
peers: peers,
cg: ctxgroup.WithContext(ctx),
dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee),
bwc: bwc,
Filters: new(Filters),
}
// configure Swarm
......@@ -88,8 +90,28 @@ func (s *Swarm) teardown() error {
return s.swarm.Close()
}
func (s *Swarm) AddDialFilter(f *net.IPNet) {
s.filters = append(s.filters, f)
type Filters struct {
filters []*net.IPNet
}
func (fs *Filters) AddDialFilter(f *net.IPNet) {
fs.filters = append(fs.filters, f)
}
func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
_, addr, err := manet.DialArgs(a)
if err != nil {
// if we cant parse it, its probably not blocked
return false
}
ip := net.ParseIP(addr)
for _, ft := range f.filters {
if ft.Contains(ip) {
return true
}
}
return false
}
// CtxGroup returns the Context Group of the swarm
......
......@@ -459,29 +459,13 @@ func (s *Swarm) dialAddr(ctx context.Context, d *conn.Dialer, p peer.ID, addr ma
func (s *Swarm) filterAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
var out []ma.Multiaddr
for _, a := range addrs {
if !s.addrBlocked(a) {
if !s.Filters.AddrBlocked(a) {
out = append(out, a)
}
}
return out
}
func (s *Swarm) addrBlocked(a ma.Multiaddr) bool {
_, addr, err := manet.DialArgs(a)
if err != nil {
// if we cant parse it, its probably not blocked
return false
}
ip := net.ParseIP(addr)
for _, f := range s.filters {
if f.Contains(ip) {
return true
}
}
return false
}
// dialConnSetup is the setup logic for a connection from the dial side. it
// needs to add the Conn to the StreamSwarm, then run newConnSetup
func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
......
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