Skip to content
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

fix: enable hole punching and libp2p listener config #79

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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

"github.com/ipfs/boxo/ipns"
Expand Down Expand Up @@ -54,6 +56,20 @@
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)",
},

Check warning on line 72 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L59-L72

Added lines #L59 - L72 were not covered by tests
&cli.IntFlag{
Name: "libp2p-connmgr-low",
Value: 100,
Expand Down Expand Up @@ -94,13 +110,21 @@
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"),

Check warning on line 118 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L113-L118

Added lines #L113 - L118 were not covered by tests
}

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

Check warning on line 121 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L121

Added line #L121 was not covered by tests

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)

Check warning on line 126 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L123-L126

Added lines #L123 - L126 were not covered by tests

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

func printIfListConfigured(message string, list []string) {
if len(list) > 0 {
fmt.Printf(message+"%v\n", strings.Join(list, ", "))

Check warning on line 229 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L227-L229

Added lines #L227 - L229 were not covered by tests
}
}
35 changes: 24 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
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 @@
return err
}

fmt.Printf("Someguy libp2p host listening on %v\n", h.Addrs())

Check warning on line 65 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L65

Added line #L65 was not covered by tests
var dhtRouting routing.Routing
if cfg.acceleratedDHTClient {
wrappedDHT, err := newBundledDHT(ctx, h)
Expand Down Expand Up @@ -96,8 +98,6 @@
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 @@
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 @@
return nil, err
}

h, err := libp2p.New(
libp2p.UserAgent("someguy/"+buildVersion()),
opts := []libp2p.Option{
libp2p.UserAgent("someguy/" + buildVersion()),

Check warning on line 185 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L184-L185

Added lines #L184 - L185 were not covered by tests
libp2p.ConnectionManager(cmgr),
libp2p.ResourceManager(rcmgr),
)
libp2p.NATPortMap(),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.EnableHolePunching(),

Check warning on line 191 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L188-L191

Added lines #L188 - L191 were not covered by tests
}

if len(cfg.libp2pListenAddress) == 0 {

Check warning on line 194 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L194

Added line #L194 was not covered by tests
// 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...))

Check warning on line 199 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L197-L199

Added lines #L197 - L199 were not covered by tests
}

h, err := libp2p.New(opts...)

Check warning on line 202 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L202

Added line #L202 was not covered by tests
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"
}
Loading