-
Notifications
You must be signed in to change notification settings - Fork 3
feat: whitelist connection gater #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7e0d57a
add boilerplate for whitelist connection gater
skosito d29d262
intercept secured log and comment out whitelist
skosito c6db553
fixes
skosito ec5aee1
init logger
skosito 69d39f7
comment out dht code
skosito 81c3bb9
add whitelist peers to constructor and gater
skosito a893c99
logs
skosito 97ba467
ci fixes
skosito 25a5190
tests fixes
skosito db434de
test fixes
skosito 2738661
debug
skosito 69798ef
shut down dht instead of removing
skosito 26ad436
whitelist bootstrap peers
skosito f40fec6
cleanup disableWhitelist flag
skosito b70c068
move pubkey to peerid to conversion
skosito 1a7b79b
fix peerAllowed bug...
skosito 9ce4a78
use whitelisted peers in tests
skosito e2c8878
cleanup
skosito 8938bc1
PR comment
skosito 983cfc9
PR comments
skosito fe47d25
Merge branch 'master' into whitelist-connection-gater
skosito 5558cea
communication tests
skosito 48efd48
PR comment
skosito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
skosito marked this conversation as resolved.
Show resolved
Hide resolved
skosito marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package p2p | ||
|
|
||
| import ( | ||
| "github.com/libp2p/go-libp2p/core/control" | ||
| "github.com/libp2p/go-libp2p/core/network" | ||
| "github.com/rs/zerolog" | ||
|
|
||
| "github.com/libp2p/go-libp2p/core/peer" | ||
| maddr "github.com/multiformats/go-multiaddr" | ||
| ) | ||
|
|
||
| type WhitelistConnectionGater struct { | ||
| whitelistedPeers map[peer.ID]bool | ||
| logger zerolog.Logger | ||
| } | ||
|
|
||
| func NewWhitelistConnectionGater(whitelistedPeers []peer.ID, logger zerolog.Logger) *WhitelistConnectionGater { | ||
| gater := &WhitelistConnectionGater{ | ||
| logger: logger, | ||
| whitelistedPeers: make(map[peer.ID]bool), | ||
| } | ||
|
|
||
| for _, p := range whitelistedPeers { | ||
| logger.Info().Msgf("Adding peer %s to whitelist", p) | ||
skosito marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
skosito marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| gater.whitelistedPeers[p] = true | ||
| } | ||
|
|
||
| return gater | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) { | ||
| return wg.peerAllowed("InterceptPeerDial", p, nil) | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) InterceptAddrDial(p peer.ID, m maddr.Multiaddr) (allow bool) { | ||
| return wg.peerAllowed("InterceptAddrDial", p, &m) | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) InterceptAccept(m network.ConnMultiaddrs) (allow bool) { | ||
| return true | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) InterceptSecured(direction network.Direction, p peer.ID, m network.ConnMultiaddrs) (allow bool) { | ||
| remoteMultiAddr := m.RemoteMultiaddr() | ||
| return wg.peerAllowed("InterceptSecured", p, &remoteMultiAddr) | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) InterceptUpgraded(network.Conn) (bool, control.DisconnectReason) { | ||
| // Allow connection upgrades | ||
| return true, 0 | ||
| } | ||
|
|
||
| func (wg *WhitelistConnectionGater) peerAllowed(interceptor string, p peer.ID, remoteAddr *maddr.Multiaddr) bool { | ||
| allowed := wg.whitelistedPeers[p] | ||
|
|
||
| var event *zerolog.Event | ||
| if allowed { | ||
| event = wg.logger.Debug() // log allowed peers at Debug level | ||
| } else { | ||
| event = wg.logger.Info() // log denied peers at Info level | ||
| } | ||
|
|
||
| event = event. | ||
| Str("interceptor", interceptor). | ||
| Stringer("peer", p). | ||
| Bool("allowed", allowed) | ||
|
|
||
| if remoteAddr != nil { | ||
| event.Str("remote_address", (*remoteAddr).String()) | ||
| } | ||
|
|
||
| if allowed { | ||
| event.Msg("Peer allowed") | ||
| } else { | ||
| event.Msg("Peer denied") | ||
| } | ||
|
|
||
| return allowed | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.