Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
9a3d287c
Commit
9a3d287c
authored
Jun 17, 2015
by
Jeromy
Committed by
Juan Batiz-Benet
Jun 23, 2015
Browse files
broke filters out into a struct
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
89c1c828
Changes
2
Hide whitespace changes
Inline
Side-by-side
net/swarm/swarm.go
View file @
9a3d287c
...
...
@@ -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
f
ilters
[]
*
net
.
IPNet
F
ilters
*
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
...
...
net/swarm/swarm_dial.go
View file @
9a3d287c
...
...
@@ -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
.
a
ddrBlocked
(
a
)
{
if
!
s
.
Filters
.
A
ddrBlocked
(
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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment