2019-12-20 09:40:50 +00:00
|
|
|
// Copyright 2019 The Waku Library Authors.
|
|
|
|
//
|
|
|
|
// The Waku library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Waku library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty off
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Waku library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
// This software uses the go-ethereum library, which is licensed
|
|
|
|
// under the GNU Lesser General Public Library, version 3 or any later.
|
|
|
|
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
package common
|
2019-12-20 09:40:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
"net"
|
2019-12-20 09:40:50 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/tsenart/tb"
|
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
|
|
|
|
2019-12-20 09:40:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
var errRateLimitExceeded = errors.New("rate limit has been exceeded")
|
|
|
|
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
type runLoop func(rw p2p.MsgReadWriter) error
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// RateLimiterPeer interface represents a Peer that is capable of being rate limited
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
type RateLimiterPeer interface {
|
|
|
|
ID() []byte
|
|
|
|
IP() net.IP
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// RateLimiterHandler interface represents handler functionality for a Rate Limiter in the cases of
|
|
|
|
// exceeding a peer limit and exceeding an IP limit
|
2019-12-20 09:40:50 +00:00
|
|
|
type RateLimiterHandler interface {
|
|
|
|
ExceedPeerLimit() error
|
|
|
|
ExceedIPLimit() error
|
|
|
|
}
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// MetricsRateLimiterHandler implements RateLimiterHandler, represents a handler for reporting rate limit Exceed data
|
|
|
|
// to the metrics collection service (currently prometheus)
|
2019-12-20 09:40:50 +00:00
|
|
|
type MetricsRateLimiterHandler struct{}
|
|
|
|
|
|
|
|
func (MetricsRateLimiterHandler) ExceedPeerLimit() error {
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
RateLimitsExceeded.WithLabelValues("peer_id").Inc()
|
2019-12-20 09:40:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (MetricsRateLimiterHandler) ExceedIPLimit() error {
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
RateLimitsExceeded.WithLabelValues("ip").Inc()
|
2019-12-20 09:40:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:54:24 +00:00
|
|
|
// RateLimits contains information about rate limit settings.
|
2020-06-09 07:21:13 +00:00
|
|
|
// It's agnostic on what it's being rate limited on (bytes or number of packets currently)
|
|
|
|
// It's exchanged with the status-update packet code
|
2020-04-23 19:54:24 +00:00
|
|
|
type RateLimits struct {
|
2020-06-09 07:21:13 +00:00
|
|
|
IPLimits uint64 // amount per second from a single IP (default 0, no limits)
|
|
|
|
PeerIDLimits uint64 // amount per second from a single peer ID (default 0, no limits)
|
|
|
|
TopicLimits uint64 // amount per second from a single topic (default 0, no limits)
|
2020-04-23 19:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r RateLimits) IsZero() bool {
|
|
|
|
return r == (RateLimits{})
|
|
|
|
}
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// DropPeerRateLimiterHandler implements RateLimiterHandler, represents a handler that introduces Tolerance to the
|
|
|
|
// number of Peer connections before Limit Exceeded errors are returned.
|
2019-12-20 09:40:50 +00:00
|
|
|
type DropPeerRateLimiterHandler struct {
|
2020-05-14 07:49:06 +00:00
|
|
|
// Tolerance is a number by which a limit must be exceeded before a peer is dropped.
|
2019-12-20 09:40:50 +00:00
|
|
|
Tolerance int64
|
|
|
|
|
|
|
|
peerLimitExceeds int64
|
|
|
|
ipLimitExceeds int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DropPeerRateLimiterHandler) ExceedPeerLimit() error {
|
|
|
|
h.peerLimitExceeds++
|
|
|
|
if h.Tolerance > 0 && h.peerLimitExceeds >= h.Tolerance {
|
2020-05-14 07:49:06 +00:00
|
|
|
return errRateLimitExceeded
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DropPeerRateLimiterHandler) ExceedIPLimit() error {
|
|
|
|
h.ipLimitExceeds++
|
|
|
|
if h.Tolerance > 0 && h.ipLimitExceeds >= h.Tolerance {
|
2020-05-14 07:49:06 +00:00
|
|
|
return errRateLimitExceeded
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// PeerRateLimiterConfig represents configurations for initialising a PeerRateLimiter
|
2019-12-20 09:40:50 +00:00
|
|
|
type PeerRateLimiterConfig struct {
|
2020-06-08 09:43:56 +00:00
|
|
|
PacketLimitPerSecIP int64
|
|
|
|
PacketLimitPerSecPeerID int64
|
2020-06-08 11:15:19 +00:00
|
|
|
BytesLimitPerSecIP int64
|
|
|
|
BytesLimitPerSecPeerID int64
|
2020-06-08 09:43:56 +00:00
|
|
|
WhitelistedIPs []string
|
|
|
|
WhitelistedPeerIDs []enode.ID
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultPeerRateLimiterConfig = PeerRateLimiterConfig{
|
2020-06-08 09:43:56 +00:00
|
|
|
PacketLimitPerSecIP: 10,
|
|
|
|
PacketLimitPerSecPeerID: 5,
|
2020-06-09 10:34:22 +00:00
|
|
|
BytesLimitPerSecIP: 1048576, // 1MB
|
|
|
|
BytesLimitPerSecPeerID: 1048576, // 1MB
|
2020-06-08 09:43:56 +00:00
|
|
|
WhitelistedIPs: nil,
|
|
|
|
WhitelistedPeerIDs: nil,
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 07:49:06 +00:00
|
|
|
// PeerRateLimiter represents a rate limiter that limits communication between Peers
|
2019-12-20 09:40:50 +00:00
|
|
|
type PeerRateLimiter struct {
|
2020-06-09 10:34:22 +00:00
|
|
|
packetThrottler *tb.Throttler
|
|
|
|
bytesThrottler *tb.Throttler
|
2020-06-08 11:15:19 +00:00
|
|
|
|
2020-06-08 09:43:56 +00:00
|
|
|
PacketLimitPerSecIP int64
|
|
|
|
PacketLimitPerSecPeerID int64
|
2019-12-20 09:40:50 +00:00
|
|
|
|
2020-06-08 11:15:19 +00:00
|
|
|
BytesLimitPerSecIP int64
|
|
|
|
BytesLimitPerSecPeerID int64
|
|
|
|
|
2019-12-20 09:40:50 +00:00
|
|
|
whitelistedPeerIDs []enode.ID
|
|
|
|
whitelistedIPs []string
|
|
|
|
|
|
|
|
handlers []RateLimiterHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPeerRateLimiter(cfg *PeerRateLimiterConfig, handlers ...RateLimiterHandler) *PeerRateLimiter {
|
|
|
|
if cfg == nil {
|
2020-04-30 12:52:48 +00:00
|
|
|
cfgCopy := defaultPeerRateLimiterConfig
|
|
|
|
cfg = &cfgCopy
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &PeerRateLimiter{
|
2020-06-09 10:34:22 +00:00
|
|
|
packetThrottler: tb.NewThrottler(time.Millisecond * 100),
|
|
|
|
bytesThrottler: tb.NewThrottler(time.Millisecond * 100),
|
2020-06-08 09:43:56 +00:00
|
|
|
PacketLimitPerSecIP: cfg.PacketLimitPerSecIP,
|
|
|
|
PacketLimitPerSecPeerID: cfg.PacketLimitPerSecPeerID,
|
2020-06-08 11:15:19 +00:00
|
|
|
BytesLimitPerSecIP: cfg.BytesLimitPerSecIP,
|
|
|
|
BytesLimitPerSecPeerID: cfg.BytesLimitPerSecPeerID,
|
2020-06-08 09:43:56 +00:00
|
|
|
whitelistedPeerIDs: cfg.WhitelistedPeerIDs,
|
|
|
|
whitelistedIPs: cfg.WhitelistedIPs,
|
|
|
|
handlers: handlers,
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
func (r *PeerRateLimiter) Decorate(p RateLimiterPeer, rw p2p.MsgReadWriter, runLoop runLoop) error {
|
2019-12-20 09:40:50 +00:00
|
|
|
errC := make(chan error, 1)
|
|
|
|
|
2020-04-30 12:52:48 +00:00
|
|
|
in, out := p2p.MsgPipe()
|
|
|
|
defer func() {
|
|
|
|
if err := in.Close(); err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- err:
|
|
|
|
default:
|
|
|
|
}
|
2020-04-30 12:52:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
if err := out.Close(); err != nil {
|
|
|
|
errC <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-12-20 09:40:50 +00:00
|
|
|
// Read from the original reader and write to the message pipe.
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2019-12-20 09:40:50 +00:00
|
|
|
for {
|
|
|
|
packet, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- fmt.Errorf("failed to read packet: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
RateLimitsProcessed.Inc()
|
2019-12-20 09:40:50 +00:00
|
|
|
|
|
|
|
var ip string
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
if p != nil {
|
|
|
|
// this relies on <nil> being the string representation of nil
|
|
|
|
// as IP() might return a nil value
|
|
|
|
ip = p.IP().String()
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
2020-06-08 11:15:19 +00:00
|
|
|
if halted := r.throttleIP(ip, packet.Size); halted {
|
2019-12-20 09:40:50 +00:00
|
|
|
for _, h := range r.handlers {
|
|
|
|
if err := h.ExceedIPLimit(); err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
|
|
|
|
case errC <- fmt.Errorf("exceed rate limit by IP: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var peerID []byte
|
|
|
|
if p != nil {
|
|
|
|
peerID = p.ID()
|
|
|
|
}
|
2020-06-08 11:15:19 +00:00
|
|
|
if halted := r.throttlePeer(peerID, packet.Size); halted {
|
2019-12-20 09:40:50 +00:00
|
|
|
for _, h := range r.handlers {
|
|
|
|
if err := h.ExceedPeerLimit(); err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- fmt.Errorf("exceeded rate limit by peer: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := in.WriteMsg(packet); err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- fmt.Errorf("failed to write packet to pipe: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Read from the message pipe and write to the original writer.
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2019-12-20 09:40:50 +00:00
|
|
|
for {
|
|
|
|
packet, err := in.ReadMsg()
|
|
|
|
if err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- fmt.Errorf("failed to read packet from pipe: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
if err := rw.WriteMsg(packet); err != nil {
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- fmt.Errorf("failed to write packet: %v", err):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2021-01-26 15:15:26 +00:00
|
|
|
// Don't block as otherwise we might leak go routines
|
|
|
|
select {
|
|
|
|
case errC <- runLoop(out):
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2019-12-20 09:40:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return <-errC
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:15:19 +00:00
|
|
|
// throttleIP throttles packets incoming from a given IP.
|
|
|
|
func (r *PeerRateLimiter) throttleIP(ip string, size uint32) bool {
|
2019-12-20 09:40:50 +00:00
|
|
|
if stringSliceContains(r.whitelistedIPs, ip) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:15:19 +00:00
|
|
|
var packetLimiterResponse bool
|
|
|
|
var bytesLimiterResponse bool
|
|
|
|
|
|
|
|
if r.PacketLimitPerSecIP != 0 {
|
2020-06-09 10:34:22 +00:00
|
|
|
packetLimiterResponse = r.packetThrottler.Halt(ip, 1, r.PacketLimitPerSecIP)
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
2020-06-08 11:15:19 +00:00
|
|
|
if r.BytesLimitPerSecIP != 0 {
|
2020-06-09 10:34:22 +00:00
|
|
|
bytesLimiterResponse = r.bytesThrottler.Halt(ip, int64(size), r.BytesLimitPerSecIP)
|
2020-06-08 11:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packetLimiterResponse || bytesLimiterResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// throttlePeer throttles packets incoming from a peer.
|
|
|
|
func (r *PeerRateLimiter) throttlePeer(peerID []byte, size uint32) bool {
|
2019-12-20 09:40:50 +00:00
|
|
|
var id enode.ID
|
|
|
|
copy(id[:], peerID)
|
|
|
|
if enodeIDSliceContains(r.whitelistedPeerIDs, id) {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-08 11:15:19 +00:00
|
|
|
|
|
|
|
var packetLimiterResponse bool
|
|
|
|
var bytesLimiterResponse bool
|
|
|
|
|
|
|
|
if r.PacketLimitPerSecPeerID != 0 {
|
2020-06-09 10:34:22 +00:00
|
|
|
packetLimiterResponse = r.packetThrottler.Halt(id.String(), 1, r.PacketLimitPerSecPeerID)
|
2020-06-08 11:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.BytesLimitPerSecPeerID != 0 {
|
2020-06-09 10:34:22 +00:00
|
|
|
bytesLimiterResponse = r.bytesThrottler.Halt(id.String(), int64(size), r.BytesLimitPerSecPeerID)
|
2020-06-08 11:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return packetLimiterResponse || bytesLimiterResponse
|
2019-12-20 09:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringSliceContains(s []string, searched string) bool {
|
|
|
|
for _, item := range s {
|
|
|
|
if item == searched {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func enodeIDSliceContains(s []enode.ID, searched enode.ID) bool {
|
|
|
|
for _, item := range s {
|
|
|
|
if bytes.Equal(item.Bytes(), searched.Bytes()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|