mirror of https://github.com/waku-org/nwaku.git
ip colocation is parameterizable. If set to 0, it is disabled (#2323)
The "ip colocation" concept refers to the maximum allowed peers from the same IP address. For example, we allow disabling this limit when the node works behind a reverse proxy.
This commit is contained in:
parent
07beea0209
commit
ebad0385ef
|
@ -303,6 +303,7 @@ proc initNode(conf: WakuNodeConf,
|
||||||
sendSignedPeerRecord = conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled
|
sendSignedPeerRecord = conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled
|
||||||
agentString = some(conf.agentString)
|
agentString = some(conf.agentString)
|
||||||
)
|
)
|
||||||
|
builder.withColocationLimit(conf.colocationLimit)
|
||||||
builder.withPeerManagerConfig(maxRelayPeers = conf.maxRelayPeers)
|
builder.withPeerManagerConfig(maxRelayPeers = conf.maxRelayPeers)
|
||||||
|
|
||||||
node = ? builder.build().mapErr(proc (err: string): string = "failed to create waku node instance: " & err)
|
node = ? builder.build().mapErr(proc (err: string): string = "failed to create waku node instance: " & err)
|
||||||
|
|
|
@ -17,7 +17,8 @@ import
|
||||||
../../waku/common/confutils/envvar/defs as confEnvvarDefs,
|
../../waku/common/confutils/envvar/defs as confEnvvarDefs,
|
||||||
../../waku/common/confutils/envvar/std/net as confEnvvarNet,
|
../../waku/common/confutils/envvar/std/net as confEnvvarNet,
|
||||||
../../waku/common/logging,
|
../../waku/common/logging,
|
||||||
../../waku/waku_enr
|
../../waku/waku_enr,
|
||||||
|
../../waku/node/peer_manager
|
||||||
|
|
||||||
export
|
export
|
||||||
confTomlDefs,
|
confTomlDefs,
|
||||||
|
@ -143,6 +144,11 @@ type
|
||||||
defaultValue: 50
|
defaultValue: 50
|
||||||
name: "max-connections" }: uint16
|
name: "max-connections" }: uint16
|
||||||
|
|
||||||
|
colocationLimit* {.
|
||||||
|
desc: "Max num allowed peers from the same IP. Set it to 0 to remove the limitation."
|
||||||
|
defaultValue: defaultColocationLimit()
|
||||||
|
name: "ip-colocation-limit" }: int
|
||||||
|
|
||||||
maxRelayPeers* {.
|
maxRelayPeers* {.
|
||||||
desc: "Maximum allowed number of relay peers."
|
desc: "Maximum allowed number of relay peers."
|
||||||
name: "max-relay-peers" }: Option[int]
|
name: "max-relay-peers" }: Option[int]
|
||||||
|
@ -524,6 +530,9 @@ proc defaultListenAddress*(): IpAddress =
|
||||||
# Maybe there should be a config option for this.
|
# Maybe there should be a config option for this.
|
||||||
(static parseIpAddress("0.0.0.0"))
|
(static parseIpAddress("0.0.0.0"))
|
||||||
|
|
||||||
|
proc defaultColocationLimit*(): int =
|
||||||
|
return DefaultColocationLimit
|
||||||
|
|
||||||
proc parseCmdArg*(T: type Port, p: string): T =
|
proc parseCmdArg*(T: type Port, p: string): T =
|
||||||
try:
|
try:
|
||||||
Port(parseInt(p))
|
Port(parseInt(p))
|
||||||
|
|
|
@ -35,6 +35,7 @@ type
|
||||||
|
|
||||||
# Peer manager config
|
# Peer manager config
|
||||||
maxRelayPeers: Option[int]
|
maxRelayPeers: Option[int]
|
||||||
|
colocationLimit: int
|
||||||
|
|
||||||
# Libp2p switch
|
# Libp2p switch
|
||||||
switchMaxConnections: Option[int]
|
switchMaxConnections: Option[int]
|
||||||
|
@ -107,7 +108,9 @@ proc withPeerManagerConfig*(builder: var WakuNodeBuilder,
|
||||||
maxRelayPeers = none(int)) =
|
maxRelayPeers = none(int)) =
|
||||||
builder.maxRelayPeers = maxRelayPeers
|
builder.maxRelayPeers = maxRelayPeers
|
||||||
|
|
||||||
|
proc withColocationLimit*(builder: var WakuNodeBuilder,
|
||||||
|
colocationLimit: int) =
|
||||||
|
builder.colocationLimit = colocationLimit
|
||||||
|
|
||||||
## Waku switch
|
## Waku switch
|
||||||
|
|
||||||
|
@ -170,6 +173,7 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] =
|
||||||
switch = switch,
|
switch = switch,
|
||||||
storage = builder.peerStorage.get(nil),
|
storage = builder.peerStorage.get(nil),
|
||||||
maxRelayPeers = builder.maxRelayPeers,
|
maxRelayPeers = builder.maxRelayPeers,
|
||||||
|
colocationLimit = builder.colocationLimit,
|
||||||
)
|
)
|
||||||
|
|
||||||
var node: WakuNode
|
var node: WakuNode
|
||||||
|
|
|
@ -62,7 +62,7 @@ const
|
||||||
LogAndMetricsInterval = chronos.minutes(3)
|
LogAndMetricsInterval = chronos.minutes(3)
|
||||||
|
|
||||||
# Max peers that we allow from the same IP
|
# Max peers that we allow from the same IP
|
||||||
ColocationLimit = 5
|
DefaultColocationLimit* = 5
|
||||||
|
|
||||||
type
|
type
|
||||||
PeerManager* = ref object of RootObj
|
PeerManager* = ref object of RootObj
|
||||||
|
@ -375,7 +375,9 @@ proc onPeerEvent(pm: PeerManager, peerId: PeerId, event: PeerEvent) {.async.} =
|
||||||
pm.ipTable.mgetOrPut(ip.get, newSeq[PeerId]()).add(peerId)
|
pm.ipTable.mgetOrPut(ip.get, newSeq[PeerId]()).add(peerId)
|
||||||
|
|
||||||
let peersBehindIp = pm.ipTable[ip.get]
|
let peersBehindIp = pm.ipTable[ip.get]
|
||||||
if peersBehindIp.len > pm.colocationLimit:
|
# pm.colocationLimit == 0 disables the ip colocation limit
|
||||||
|
if pm.colocationLimit != 0 and
|
||||||
|
peersBehindIp.len > pm.colocationLimit:
|
||||||
# in theory this should always be one, but just in case
|
# in theory this should always be one, but just in case
|
||||||
for peerId in peersBehindIp[0..<(peersBehindIp.len - pm.colocationLimit)]:
|
for peerId in peersBehindIp[0..<(peersBehindIp.len - pm.colocationLimit)]:
|
||||||
debug "Pruning connection due to ip colocation", peerId = peerId, ip = ip
|
debug "Pruning connection due to ip colocation", peerId = peerId, ip = ip
|
||||||
|
@ -411,7 +413,7 @@ proc new*(T: type PeerManager,
|
||||||
initialBackoffInSec = InitialBackoffInSec,
|
initialBackoffInSec = InitialBackoffInSec,
|
||||||
backoffFactor = BackoffFactor,
|
backoffFactor = BackoffFactor,
|
||||||
maxFailedAttempts = MaxFailedAttempts,
|
maxFailedAttempts = MaxFailedAttempts,
|
||||||
colocationLimit = ColocationLimit,): PeerManager =
|
colocationLimit = DefaultColocationLimit,): PeerManager =
|
||||||
|
|
||||||
let capacity = switch.peerStore.capacity
|
let capacity = switch.peerStore.capacity
|
||||||
let maxConnections = switch.connManager.inSema.size
|
let maxConnections = switch.connManager.inSema.size
|
||||||
|
|
Loading…
Reference in New Issue