broke filters out into a struct
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jun 17, 2015 at 09:53 UTC
fbab2a727320af83c45a9f81bf10c16a1a02d062
2 files changed
+33
-27
p2p/net/swarm/swarm.go
+32
-10
@@ -16,6 +16,7 @@ import (
16
17
ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
18
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
19
+ manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
20
ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
21
pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
22
psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
@@ -52,7 +53,7 @@ type Swarm struct {
53
notifs map[inet.Notifiee]ps.Notifiee
54
55
// filters for addresses that shouldnt be dialed
55
- filters []*net.IPNet
56
+ Filters *Filters
57
58
cg ctxgroup.ContextGroup
59
bwc metrics.Reporter
@@ -68,13 +69,14 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
69
}
70
71
s := &Swarm{
71
- swarm: ps.NewSwarm(PSTransport),
72
- local: local,
73
- peers: peers,
74
- cg: ctxgroup.WithContext(ctx),
75
- dialT: DialTimeout,
76
- notifs: make(map[inet.Notifiee]ps.Notifiee),
77
- bwc: bwc,
72
+ swarm: ps.NewSwarm(PSTransport),
73
+ local: local,
74
+ peers: peers,
75
+ cg: ctxgroup.WithContext(ctx),
76
+ dialT: DialTimeout,
77
+ notifs: make(map[inet.Notifiee]ps.Notifiee),
78
+ bwc: bwc,
79
+ Filters: new(Filters),
80
}
81
82
// configure Swarm
@@ -88,8 +90,28 @@ func (s *Swarm) teardown() error {
90
return s.swarm.Close()
91
}
92
91
-func (s *Swarm) AddDialFilter(f *net.IPNet) {
92
- s.filters = append(s.filters, f)
93
+type Filters struct {
94
+ filters []*net.IPNet
95
+}
96
+
97
+func (fs *Filters) AddDialFilter(f *net.IPNet) {
98
+ fs.filters = append(fs.filters, f)
99
+}
100
+
101
+func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
102
+ _, addr, err := manet.DialArgs(a)
103
+ if err != nil {
104
+ // if we cant parse it, its probably not blocked
105
+ return false
106
+ }
107
+
108
+ ip := net.ParseIP(addr)
109
+ for _, ft := range f.filters {
110
+ if ft.Contains(ip) {
111
+ return true
112
+ }
113
+ }
114
+ return false
115
}
116
117
// CtxGroup returns the Context Group of the swarm
p2p/net/swarm/swarm_dial.go
+1
-17
@@ -459,29 +459,13 @@ func (s *Swarm) dialAddr(ctx context.Context, d *conn.Dialer, p peer.ID, addr ma
459
func (s *Swarm) filterAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
460
var out []ma.Multiaddr
461
for _, a := range addrs {
462
- if !s.addrBlocked(a) {
462
+ if !s.Filters.AddrBlocked(a) {
463
out = append(out, a)
464
}
465
}
466
return out
467
}
468
469
-func (s *Swarm) addrBlocked(a ma.Multiaddr) bool {
470
- _, addr, err := manet.DialArgs(a)
471
- if err != nil {
472
- // if we cant parse it, its probably not blocked
473
- return false
474
- }
475
-
476
- ip := net.ParseIP(addr)
477
- for _, f := range s.filters {
478
- if f.Contains(ip) {
479
- return true
480
- }
481
- }
482
- return false
483
-}
484
-
469
// dialConnSetup is the setup logic for a connection from the dial side. it
470
// needs to add the Conn to the StreamSwarm, then run newConnSetup
471
func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {