-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: whitelist connection gater (#31)
- Loading branch information
Showing
13 changed files
with
214 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,81 @@ | ||
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(). | ||
Stringer("peer", p). | ||
Msg("Adding peer to whitelist") | ||
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.