mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
broke filters out into a struct
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
|
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"
|
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"
|
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"
|
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"
|
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
|
notifs map[inet.Notifiee]ps.Notifiee
|
||||||
|
|
||||||
// filters for addresses that shouldnt be dialed
|
// filters for addresses that shouldnt be dialed
|
||||||
filters []*net.IPNet
|
Filters *Filters
|
||||||
|
|
||||||
cg ctxgroup.ContextGroup
|
cg ctxgroup.ContextGroup
|
||||||
bwc metrics.Reporter
|
bwc metrics.Reporter
|
||||||
@ -68,13 +69,14 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
s := &Swarm{
|
s := &Swarm{
|
||||||
swarm: ps.NewSwarm(PSTransport),
|
swarm: ps.NewSwarm(PSTransport),
|
||||||
local: local,
|
local: local,
|
||||||
peers: peers,
|
peers: peers,
|
||||||
cg: ctxgroup.WithContext(ctx),
|
cg: ctxgroup.WithContext(ctx),
|
||||||
dialT: DialTimeout,
|
dialT: DialTimeout,
|
||||||
notifs: make(map[inet.Notifiee]ps.Notifiee),
|
notifs: make(map[inet.Notifiee]ps.Notifiee),
|
||||||
bwc: bwc,
|
bwc: bwc,
|
||||||
|
Filters: new(Filters),
|
||||||
}
|
}
|
||||||
|
|
||||||
// configure Swarm
|
// configure Swarm
|
||||||
@ -88,8 +90,28 @@ func (s *Swarm) teardown() error {
|
|||||||
return s.swarm.Close()
|
return s.swarm.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Swarm) AddDialFilter(f *net.IPNet) {
|
type Filters struct {
|
||||||
s.filters = append(s.filters, f)
|
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
|
// 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 {
|
func (s *Swarm) filterAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
|
||||||
var out []ma.Multiaddr
|
var out []ma.Multiaddr
|
||||||
for _, a := range addrs {
|
for _, a := range addrs {
|
||||||
if !s.addrBlocked(a) {
|
if !s.Filters.AddrBlocked(a) {
|
||||||
out = append(out, a)
|
out = append(out, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
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
|
// dialConnSetup is the setup logic for a connection from the dial side. it
|
||||||
// needs to add the Conn to the StreamSwarm, then run newConnSetup
|
// needs to add the Conn to the StreamSwarm, then run newConnSetup
|
||||||
func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
|
func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
|
||||||
|
Reference in New Issue
Block a user