From fb3086dcc36f00c32ca125bfd78355793b1b1fb3 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 14 Aug 2024 14:48:15 +0200 Subject: [PATCH 1/3] fix: enable hole punching this idoes not hurt, and should help if someone is runnign someguy without public addr + hole punching is necessary --- CHANGELOG.md | 6 ++++++ server.go | 4 ++++ version.json | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 930411c..09031dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,12 @@ The following emojis are used to highlight certain changes: ### Security +## [v0.4.1] + +### Fixed + +- enabled NAT port map and Hole Punching to increase connectivity in non-public network topologies + ## [v0.4.0] ### Changed diff --git a/server.go b/server.go index 3b16511..be979f4 100644 --- a/server.go +++ b/server.go @@ -186,6 +186,10 @@ func newHost(cfg *config) (host.Host, error) { libp2p.UserAgent("someguy/"+buildVersion()), libp2p.ConnectionManager(cmgr), libp2p.ResourceManager(rcmgr), + libp2p.NATPortMap(), + libp2p.DefaultTransports, + libp2p.DefaultMuxers, + libp2p.EnableHolePunching(), ) if err != nil { return nil, err diff --git a/version.json b/version.json index 372b6ea..26a7d47 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "version": "v0.4.0" + "version": "v0.4.1" } From 9664fad92b28c9d166cae4ae86c36b1e1e87ace4 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:03:58 +0200 Subject: [PATCH 2/3] feat: config to customize libp2p listen addrs --- CHANGELOG.md | 4 ++++ docs/environment-variables.md | 7 +++++++ main.go | 25 ++++++++++++++++++++----- server.go | 27 +++++++++++++++++++-------- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09031dc..c674e75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ The following emojis are used to highlight certain changes: ## [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 diff --git a/docs/environment-variables.md b/docs/environment-variables.md index a96948c..3c59c66 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -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) @@ -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. diff --git a/main.go b/main.go index 0bc83bf..a629608 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,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, @@ -94,11 +108,12 @@ 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"), } return start(ctx.Context, cfg) diff --git a/server.go b/server.go index be979f4..722409c 100644 --- a/server.go +++ b/server.go @@ -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 { @@ -182,15 +183,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 } From e2b9f09cb8dca4dc46e91a6230000c475dc1242a Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:17:19 +0200 Subject: [PATCH 3/3] feat: log config when starting --- main.go | 15 +++++++++++++++ server.go | 4 +--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index a629608..e8e00c0 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "errors" + "fmt" "log" "os" + "strings" "time" "github.com/ipfs/boxo/ipns" @@ -116,6 +118,13 @@ func main() { 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) }, }, @@ -214,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, ", ")) + } +} diff --git a/server.go b/server.go index 722409c..985d11d 100644 --- a/server.go +++ b/server.go @@ -62,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) @@ -97,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"}), }) @@ -141,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() {