Skip to content

Commit

Permalink
support a list of trusted peer IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
emberian committed Sep 23, 2020
1 parent 7573a48 commit 10b31e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/app/libp2p_helper/src/codanet.go
Expand Up @@ -49,16 +49,21 @@ type customValidator struct {
Base record.Validator
}

// this type implements the ConnectionGater interface
// https://godoc.org/github.com/libp2p/go-libp2p-core/connmgr#ConnectionGater
// the comments of the functions below are taken from those docs.
type codaGaterState struct {
AddrFilters *ma.Filters
DeniedPeers *peer.Set
AllowedPeers *peer.Set
}

// InterceptPeerDial tests whether we're permitted to Dial the specified peer.
//
// This is called by the network.Network implementation when dialling a peer.
func (gs *codaGaterState) InterceptPeerDial(p peer.ID) (allow bool) {
allow = !gs.DeniedPeers.Contains(p)
allow = !gs.DeniedPeers.Contains(p) || gs.AllowedPeers.Contains(p)

return
}

Expand All @@ -68,7 +73,7 @@ func (gs *codaGaterState) InterceptPeerDial(p peer.ID) (allow bool) {
// This is called by the network.Network implementation after it has
// resolved the peer's addrs, and prior to dialling each.
func (gs *codaGaterState) InterceptAddrDial(id peer.ID, addr ma.Multiaddr) (allow bool) {
allow = !gs.DeniedPeers.Contains(id) && !gs.AddrFilters.AddrBlocked(addr)
allow = (!gs.DeniedPeers.Contains(id) || gs.AllowedPeers.Contains(p)) && !gs.AddrFilters.AddrBlocked(addr)
return
}

Expand All @@ -93,7 +98,7 @@ func (gs *codaGaterState) InterceptSecured(_ network.Direction, id peer.ID, addr
// connections in coda are symmetric: if i am allowed to connect to
// you, you are allowed to connect to me.
remoteAddr := addrs.RemoteMultiaddr()
allow = !gs.DeniedPeers.Contains(id) && !gs.AddrFilters.AddrBlocked(remoteAddr)
allow = (!gs.DeniedPeers.Contains(id) || gs.AllowedPeers.Contains(id)) && !gs.AddrFilters.AddrBlocked(remoteAddr)
return
}

Expand Down
1 change: 1 addition & 0 deletions src/app/libp2p_helper/src/libp2p_helper/main.go
Expand Up @@ -961,6 +961,7 @@ type unbanIPMsg struct {
type setGaterConfigMsg struct {
BannedIPs []string `json:"banned_ips"`
BannedPeerIDs []string `json:"banned_peers"`
TrustedPeerIDs []string `json:"trusted_peers"`
TrustedIPs []string `json:"trusted_ips"`
Isolate bool `json:"isolate"`
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/coda_net2/coda_net2.ml
Expand Up @@ -362,7 +362,7 @@ module Helper = struct
end

module Set_gater_config = struct
type input = {banned_ips: string list ; banned_peers: string list; trusted_ips: string list; isolate: bool} [@@deriving yojson]
type input = {banned_ips: string list ; banned_peers: string list; trusted_peers: string list; trusted_ips: string list; isolate: bool} [@@deriving yojson]

type output = string [@@deriving yojson]

Expand Down Expand Up @@ -1260,7 +1260,13 @@ let lookup_peerid = Helper.lookup_peerid

let configure_connection_gating net config =
match%map
Helper.(do_rpc net (module Rpcs.Set_gater_config) config)
Helper.(do_rpc net (module Rpcs.Set_gater_config) {
banned_ips= List.map ~f:(fun p -> Unix.Inet_addr.to_string p.host) config.banned_peers ;
banned_peers= List.map ~f:(fun p -> p.peer_id) config.banned_peers ;
trusted_ips= List.map ~f:(fun p -> Unix.Inet_addr.to_string p.host) config.trusted_peers ;
trusted_peers= List.map ~f:(fun p -> p.peer_id) config.trusted_peers ;
isolate= config.isolate
})
with
| Ok "ok" ->
Ok ()
Expand Down

0 comments on commit 10b31e9

Please sign in to comment.