Skip to content

Commit

Permalink
fix: enable hole punching and libp2p listener config (#79)
Browse files Browse the repository at this point in the history
* fix: enable hole punching
* feat: config to customize libp2p listen addrs
* feat: log config when starting

---------

Co-authored-by: Daniel N <2color@users.noreply.github.com>
  • Loading branch information
lidel and 2color authored Aug 19, 2024
1 parent 293c322 commit 2f2fb2e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ The following emojis are used to highlight certain changes:

### Security

## [v0.4.1]

### Added

- `SOMEGUY_LIBP2P_LISTEN_ADDRS` config [environment variable](./docs/environment-variables.md#someguy_libp2p_listen_addrs) for customizing the interfaces, ports, and transports of the libp2p host created by someguy.

### Fixed

- enabled NAT port map and Hole Punching to increase connectivity in non-public network topologies

## [v0.4.0]

### Changed
Expand Down
7 changes: 7 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [`SOMEGUY_PROVIDER_ENDPOINTS`](#someguy_provider_endpoints)
- [`SOMEGUY_PEER_ENDPOINTS`](#someguy_peer_endpoints)
- [`SOMEGUY_IPNS_ENDPOINTS`](#someguy_ipns_endpoints)
- [`SOMEGUY_LIBP2P_LISTEN_ADDRS`](#someguy_libp2p_listen_addrs)
- [`SOMEGUY_LIBP2P_CONNMGR_LOW`](#someguy_libp2p_connmgr_low)
- [`SOMEGUY_LIBP2P_CONNMGR_HIGH`](#someguy_libp2p_connmgr_high)
- [`SOMEGUY_LIBP2P_CONNMGR_GRACE_PERIOD`](#someguy_libp2p_connmgr_grace_period)
Expand Down Expand Up @@ -51,6 +52,12 @@ Comma-separated list of other Delegated Routing V1 endpoints to proxy IPNS reque

Default: none

### `SOMEGUY_LIBP2P_LISTEN_ADDRS`

Multiaddresses for libp2p host to listen on (comma-separated).

Default: `someguy start --help`

### `SOMEGUY_LIBP2P_CONNMGR_LOW`

Minimum number of libp2p connections to keep.
Expand Down
40 changes: 35 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/ipfs/boxo/ipns"
Expand Down Expand Up @@ -54,6 +56,20 @@ func main() {
EnvVars: []string{"SOMEGUY_IPNS_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy IPNS requests to",
},
&cli.StringSliceFlag{
Name: "libp2p-listen-addrs",
Value: cli.NewStringSlice(
"/ip4/0.0.0.0/tcp/4004",
"/ip4/0.0.0.0/udp/4004/quic-v1",
"/ip4/0.0.0.0/udp/4004/webrtc-direct",
"/ip4/0.0.0.0/udp/4004/quic-v1/webtransport",
"/ip6/::/tcp/4004",
"/ip6/::/udp/4004/quic-v1",
"/ip6/::/udp/4004/webrtc-direct",
"/ip6/::/udp/4004/quic-v1/webtransport"),
EnvVars: []string{"SOMEGUY_LIBP2P_LISTEN_ADDRS"},
Usage: "Multiaddresses for libp2p host to listen on (comma-separated)",
},
&cli.IntFlag{
Name: "libp2p-connmgr-low",
Value: 100,
Expand Down Expand Up @@ -94,13 +110,21 @@ func main() {
peerEndpoints: ctx.StringSlice("peer-endpoints"),
ipnsEndpoints: ctx.StringSlice("ipns-endpoints"),

connMgrLow: ctx.Int("libp2p-connmgr-low"),
connMgrHi: ctx.Int("libp2p-connmgr-high"),
connMgrGrace: ctx.Duration("libp2p-connmgr-grace"),
maxMemory: ctx.Uint64("libp2p-max-memory"),
maxFD: ctx.Int("libp2p-max-fd"),
libp2pListenAddress: ctx.StringSlice("libp2p-listen-addrs"),
connMgrLow: ctx.Int("libp2p-connmgr-low"),
connMgrHi: ctx.Int("libp2p-connmgr-high"),
connMgrGrace: ctx.Duration("libp2p-connmgr-grace"),
maxMemory: ctx.Uint64("libp2p-max-memory"),
maxFD: ctx.Int("libp2p-max-fd"),
}

fmt.Printf("Starting %s %s\n", name, version)

fmt.Printf("SOMEGUY_ACCELERATED_DHT = %t\n", cfg.acceleratedDHTClient)
printIfListConfigured("SOMEGUY_PROVIDER_ENDPOINTS = ", cfg.contentEndpoints)
printIfListConfigured("SOMEGUY_PEER_ENDPOINTS = ", cfg.peerEndpoints)
printIfListConfigured("SOMEGUY_IPNS_ENDPOINTS = ", cfg.ipnsEndpoints)

return start(ctx.Context, cfg)
},
},
Expand Down Expand Up @@ -199,3 +223,9 @@ func main() {
log.Fatal(err)
}
}

func printIfListConfigured(message string, list []string) {
if len(list) > 0 {
fmt.Printf(message+"%v\n", strings.Join(list, ", "))
}
}
35 changes: 24 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ type config struct {
peerEndpoints []string
ipnsEndpoints []string

connMgrLow int
connMgrHi int
connMgrGrace time.Duration
maxMemory uint64
maxFD int
libp2pListenAddress []string
connMgrLow int
connMgrHi int
connMgrGrace time.Duration
maxMemory uint64
maxFD int
}

func start(ctx context.Context, cfg *config) error {
Expand All @@ -61,6 +62,7 @@ func start(ctx context.Context, cfg *config) error {
return err
}

fmt.Printf("Someguy libp2p host listening on %v\n", h.Addrs())
var dhtRouting routing.Routing
if cfg.acceleratedDHTClient {
wrappedDHT, err := newBundledDHT(ctx, h)
Expand Down Expand Up @@ -96,8 +98,6 @@ func start(ctx context.Context, cfg *config) error {
return err
}

fmt.Printf("Starting %s %s\n", name, version)

mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{Prefix: "someguy"}),
})
Expand Down Expand Up @@ -140,7 +140,6 @@ func start(ctx context.Context, cfg *config) error {
var wg sync.WaitGroup
wg.Add(1)

fmt.Printf("Listening on %s\n", cfg.listenAddress)
fmt.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1\n", port)

go func() {
Expand Down Expand Up @@ -182,11 +181,25 @@ func newHost(cfg *config) (host.Host, error) {
return nil, err
}

h, err := libp2p.New(
libp2p.UserAgent("someguy/"+buildVersion()),
opts := []libp2p.Option{
libp2p.UserAgent("someguy/" + buildVersion()),
libp2p.ConnectionManager(cmgr),
libp2p.ResourceManager(rcmgr),
)
libp2p.NATPortMap(),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.EnableHolePunching(),
}

if len(cfg.libp2pListenAddress) == 0 {
// Note: because the transports are set above we must also set the listen addresses
// We need to set listen addresses in order for hole punching to work
opts = append(opts, libp2p.DefaultListenAddrs)
} else {
opts = append(opts, libp2p.ListenAddrStrings(cfg.libp2pListenAddress...))
}

h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.4.0"
"version": "v0.4.1"
}

0 comments on commit 2f2fb2e

Please sign in to comment.