From 16daf6e873466e271a26f76460508f5a9d96215c Mon Sep 17 00:00:00 2001 From: Dylan <64976002+galt-tr@users.noreply.github.com> Date: Wed, 14 Feb 2024 14:55:51 -0500 Subject: [PATCH] Add support for public broadcast IP (#52) --- app/config/config.go | 1 + app/config/envs/mainnet.json | 1 + app/config/envs/stn.json | 1 + app/config/envs/testnet.json | 1 + app/p2p/server.go | 19 +++++++++++++++++++ 5 files changed, 23 insertions(+) diff --git a/app/config/config.go b/app/config/config.go index 8b25ac7..a1c8ef0 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -97,6 +97,7 @@ type ( P2PConfig struct { AlertSystemProtocolID string `json:"alert_system_protocol_id" mapstructure:"alert_system_protocol_id"` // AlertSystemProtocolID is the protocol ID to use on the libp2p network for alert system communication BootstrapPeer string `json:"bootstrap_peer" mapstructure:"bootstrap_peer"` // BootstrapPeer is the bootstrap peer for the libp2p network + BroadcastIP string `json:"broadcast_ip" mapstructure:"broadcast_ip"` // BroadcastIP is the public facing IP address to broadcast to other peers IP string `json:"ip" mapstructure:"ip"` // IP is the IP address for the P2P server Port string `json:"port" mapstructure:"port"` // Port is the port for the P2P server PrivateKeyPath string `json:"private_key_path" mapstructure:"private_key_path"` // PrivateKeyPath is the path to the private key diff --git a/app/config/envs/mainnet.json b/app/config/envs/mainnet.json index 332fade..c7b1f7f 100644 --- a/app/config/envs/mainnet.json +++ b/app/config/envs/mainnet.json @@ -65,6 +65,7 @@ "p2p": { "ip": "0.0.0.0", "port": "9906", + "broadcast_ip": "", "alert_system_protocol_id": "/bitcoin/alert-system/1.0.0", "bootstrap_peer": "", "private_key_path": "", diff --git a/app/config/envs/stn.json b/app/config/envs/stn.json index 3c59cbf..b98c3ed 100644 --- a/app/config/envs/stn.json +++ b/app/config/envs/stn.json @@ -67,6 +67,7 @@ "port": "9906", "alert_system_protocol_id": "/bitcoin-stn/alert-system/0.0.1", "bootstrap_peer": "", + "broadcast_ip": "", "private_key_path": "", "topic_name": "bitcoin_alert_system_stn" }, diff --git a/app/config/envs/testnet.json b/app/config/envs/testnet.json index d030a8a..a07c517 100644 --- a/app/config/envs/testnet.json +++ b/app/config/envs/testnet.json @@ -67,6 +67,7 @@ "port": "9906", "alert_system_protocol_id": "/bitcoin-testnet/alert-system/0.0.1", "bootstrap_peer": "", + "broadcast_ip": "", "private_key_path": "", "topic_name": "bitcoin_alert_system_testnet" }, diff --git a/app/p2p/server.go b/app/p2p/server.go index 41cc88d..1e45d4d 100644 --- a/app/p2p/server.go +++ b/app/p2p/server.go @@ -8,6 +8,8 @@ import ( "os" "time" + maddr "github.com/multiformats/go-multiaddr" + "github.com/libp2p/go-libp2p/core/discovery" dht "github.com/libp2p/go-libp2p-kad-dht" @@ -73,12 +75,29 @@ func NewServer(o ServerOptions) (*Server, error) { } } + var extMultiAddr maddr.Multiaddr + if o.Config.P2P.BroadcastIP != "" { + extMultiAddr, err = maddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s", o.Config.P2P.BroadcastIP, o.Config.P2P.Port)) + if err != nil { + return nil, err + } + } + + addressFactory := func(addrs []maddr.Multiaddr) []maddr.Multiaddr { + if extMultiAddr != nil { + // here we're appending the external facing multiaddr we created above to the addressFactory so it will be broadcast out when I connect to a bootstrap node. + addrs = append(addrs, extMultiAddr) + } + return addrs + } + // Create a new host var h host.Host if h, err = libp2p.New( libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/%s/tcp/%s", o.Config.P2P.IP, o.Config.P2P.Port)), libp2p.Identity(*pk), libp2p.EnableHolePunching(), + libp2p.AddrsFactory(addressFactory), ); err != nil { return nil, err }