Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Move Config and ACL to the top-level module (#20)
Browse files Browse the repository at this point in the history
I would like to programatically generate relays configurations, however this
is slightly more difficult because, despite being exported types, they are in
the "main" package.

This PR moves them to the top-level directory so that they can be imported from
other modules.

I also took the liberty of documenting things.
  • Loading branch information
hsanjuan authored Jul 24, 2022
1 parent 84a8f56 commit 58fc32b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
17 changes: 13 additions & 4 deletions cmd/libp2p-relay-daemon/acl.go → acl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package relaydaemon

import (
"fmt"
Expand All @@ -16,6 +16,7 @@ import (
manet "github.com/multiformats/go-multiaddr/net"
)

// ACLFilter implements the libp2p relay ACL interface.
type ACLFilter struct {
allowPeers map[peer.ID]struct{}
allowSubnets []*net.IPNet
Expand All @@ -28,6 +29,8 @@ type ACLFilter struct {
var _ relayv1.ACLFilter = (*ACLFilter)(nil)
var _ relayv2.ACLFilter = (*ACLFilter)(nil)

// NewACL returns an implementation of the relay ACL interface using the given
// host and relay daemon ACL config.
func NewACL(h host.Host, cfg ACLConfig) (*ACLFilter, error) {
acl := &ACLFilter{}

Expand Down Expand Up @@ -63,7 +66,7 @@ func NewACL(h host.Host, cfg ACLConfig) (*ACLFilter, error) {
return acl, nil
}

// relayv2 ACL
// AllowReserve is relevant for the relayv2 ACL implementation.
func (a *ACLFilter) AllowReserve(p peer.ID, addr ma.Multiaddr) bool {
if len(a.allowPeers) > 0 {
_, ok := a.allowPeers[p]
Expand All @@ -90,11 +93,13 @@ func (a *ACLFilter) AllowReserve(p peer.ID, addr ma.Multiaddr) bool {
return true
}

// AllowConnect is always true, as we are accepting any public node to be able
// to contact the nodes allowed to make reservations through this relay.
func (a *ACLFilter) AllowConnect(src peer.ID, srcAddr ma.Multiaddr, dest peer.ID) bool {
return true
}

// relayv1 ACL
// AllowHop is relevant for relayv1 ACL implementation.
func (a *ACLFilter) AllowHop(src, dest peer.ID) bool {
if len(a.allowPeers) > 0 {
_, ok := a.allowPeers[dest]
Expand Down Expand Up @@ -127,7 +132,9 @@ func (a *ACLFilter) AllowHop(src, dest peer.ID) bool {
return true
}

// notifications
// Connected handles the Connect notification and stores the address of the
// connected node so that the ACL can decide whether other nodes can connect
// to it (relayV1).
func (a *ACLFilter) Connected(n network.Network, c network.Conn) {
p := c.RemotePeer()
addr := c.RemoteMultiaddr()
Expand All @@ -144,6 +151,8 @@ func (a *ACLFilter) Connected(n network.Network, c network.Conn) {
addrs[addr] = struct{}{}
}

// Disconnected handles the Disconnect notification and deletes the address of
// the disconnected node.
func (a *ACLFilter) Disconnected(n network.Network, c network.Conn) {
p := c.RemotePeer()
addr := c.RemoteMultiaddr()
Expand Down
7 changes: 4 additions & 3 deletions cmd/libp2p-relay-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/libp2p/go-libp2p"
relaydaemon "github.com/libp2p/go-libp2p-relay-daemon"
relayv1 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv1/relay"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"

Expand All @@ -22,11 +23,11 @@ func main() {
cfgPath := flag.String("config", "", "json configuration file; empty uses the default configuration")
flag.Parse()

cfg, err := loadConfig(*cfgPath)
cfg, err := relaydaemon.LoadConfig(*cfgPath)
if err != nil {
panic(err)
}
privk, err := loadIdentity(*idPath)
privk, err := relaydaemon.LoadIdentity(*idPath)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -92,7 +93,7 @@ func main() {
go listenPprof(cfg.Daemon.PprofPort)
time.Sleep(10 * time.Millisecond)

acl, err := NewACL(host, cfg.ACL)
acl, err := relaydaemon.NewACL(host, cfg.ACL)
if err != nil {
panic(err)
}
Expand Down
26 changes: 22 additions & 4 deletions cmd/libp2p-relay-daemon/config.go → config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package relaydaemon

import (
"encoding/json"
Expand All @@ -9,6 +9,8 @@ import (
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
)

// Config stores the full configuration of the relays, ACLs and other settings
// that influence behaviour of a relay daemon.
type Config struct {
Network NetworkConfig
ConnMgr ConnMgrConfig
Expand All @@ -18,37 +20,50 @@ type Config struct {
Daemon DaemonConfig
}

// DaemonConfig controls settings for the relay-daemon itself.
type DaemonConfig struct {
PprofPort int
}

// NetworkConfig controls listen and annouce settings for the libp2p host.
type NetworkConfig struct {
ListenAddrs []string
AnnounceAddrs []string
}

// ConnMgrConfig controls the libp2p connection manager settings.
type ConnMgrConfig struct {
ConnMgrLo int
ConnMgrHi int
ConnMgrGrace time.Duration
}

// RelayV1Config controls activation of V1 circuits and resouce configuration
// for them.
type RelayV1Config struct {
Enabled bool
Resources relayv1.Resources
}

// RelayV2Config controls activation of V2 circuits and resouce configuration
// for them.
type RelayV2Config struct {
Enabled bool
Resources relayv2.Resources
}

// ACLConfig provides filtering configuration to allow specific peers or
// subnets to be fronted by relays. In V2, this specifies the peers/subnets
// that are able to make reservations on the relay. In V1, this specifies the
// peers/subnets that can be contacted through the relays.
type ACLConfig struct {
AllowPeers []string
AllowSubnets []string
}

func defaultConfig() Config {
// DefaultConfig returns a default relay configuration using default resource
// settings and no ACLs.
func DefaultConfig() Config {
return Config{
Network: NetworkConfig{
ListenAddrs: []string{
Expand Down Expand Up @@ -77,8 +92,11 @@ func defaultConfig() Config {
}
}

func loadConfig(cfgPath string) (Config, error) {
cfg := defaultConfig()
// LoadConfig reads a relay daemon JSON configuration from the given path.
// The configuration is first initialized with DefaultConfig, so all unset
// fields will take defaults from there.
func LoadConfig(cfgPath string) (Config, error) {
cfg := DefaultConfig()

if cfgPath != "" {
cfgFile, err := os.Open(cfgPath)
Expand Down
16 changes: 10 additions & 6 deletions cmd/libp2p-relay-daemon/identity.go → identity.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package relaydaemon

import (
"fmt"
Expand All @@ -8,18 +8,21 @@ import (
"github.com/libp2p/go-libp2p-core/crypto"
)

func loadIdentity(idPath string) (crypto.PrivKey, error) {
// LoadIdentity reads a private key from the given path and, if it does not
// exist, generates a new one.
func LoadIdentity(idPath string) (crypto.PrivKey, error) {
if _, err := os.Stat(idPath); err == nil {
return readIdentity(idPath)
return ReadIdentity(idPath)
} else if os.IsNotExist(err) {
fmt.Printf("Generating peer identity in %s\n", idPath)
return generateIdentity(idPath)
return GenerateIdentity(idPath)
} else {
return nil, err
}
}

func readIdentity(path string) (crypto.PrivKey, error) {
// ReadIdentity reads a private key from the given path.
func ReadIdentity(path string) (crypto.PrivKey, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
Expand All @@ -28,7 +31,8 @@ func readIdentity(path string) (crypto.PrivKey, error) {
return crypto.UnmarshalPrivateKey(bytes)
}

func generateIdentity(path string) (crypto.PrivKey, error) {
// GenerateIdentity writes a new random private key to the given path.
func GenerateIdentity(path string) (crypto.PrivKey, error) {
privk, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 0)
if err != nil {
return nil, err
Expand Down

0 comments on commit 58fc32b

Please sign in to comment.