From e47f864966a7551ceee8df6d2e6aefa1b3b7e929 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Wed, 15 Feb 2023 18:09:21 +0530 Subject: [PATCH] Add an option to use a domain name for a announce address (#320) * Add an option to use a domain name for a announce address * Update chat CLI readme * Set port range for relay node * Add descriptive comment * Upgrade webrtc-direct version --- packages/cli/README.md | 7 ++++--- packages/peer/package.json | 2 +- packages/peer/src/constants.ts | 7 +++++++ packages/peer/src/relay.ts | 22 ++++++++++++++++++---- yarn.lock | 18 +++++++++--------- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/packages/cli/README.md b/packages/cli/README.md index 46f864e3..8784ba36 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -30,11 +30,12 @@ A basic CLI to pass messages between peers using `stdin`/`stdout` ```bash # In packages/peer - yarn relay-node --host [LISTEN_HOST] --port [LISTEN_PORT] --peer-id-file [PEER_ID_FILE_PATH] --relay-peers [RELAY_PEERS_FILE_PATH] + yarn relay-node --host [LISTEN_HOST] --port [LISTEN_PORT] --announce [ANNOUNCE_DOMAIN] --peer-id-file [PEER_ID_FILE_PATH] --relay-peers [RELAY_PEERS_FILE_PATH] ``` - * `host (h)`: Host to bind to (default: `127.0.0.1`) - * `port (p)`: Port to start listening on (default: `9090`) + * `host (h)`: host to bind to (default: `127.0.0.1`) + * `port (p)`: port to start listening on (default: `9090`) + * `announce (a)`: domain name to be used in the announce address * `peer-id-file (f)`: file path for peer id to be used (json) * `relay-peers (r)`: file path for relay peer multiaddr(s) to dial on startup (json) diff --git a/packages/peer/package.json b/packages/peer/package.json index 73286833..79051f0a 100644 --- a/packages/peer/package.json +++ b/packages/peer/package.json @@ -27,7 +27,7 @@ }, "dependencies": { "@cerc-io/libp2p": "0.42.2-laconic-0.1.1", - "@cerc-io/webrtc-direct": "^5.0.0-laconic-0.1.2", + "@cerc-io/webrtc-direct": "^5.0.0-laconic-0.1.3", "@chainsafe/libp2p-noise": "^11.0.0", "@libp2p/floodsub": "^6.0.0", "@libp2p/mplex": "^7.1.1", diff --git a/packages/peer/src/constants.ts b/packages/peer/src/constants.ts index 94c760b2..184cf431 100644 --- a/packages/peer/src/constants.ts +++ b/packages/peer/src/constants.ts @@ -35,6 +35,13 @@ export const RELAY_REDIAL_DELAY = 5000; // 5 sconds // Max number of relay node connections for a peer after which it starts igoring them export const DEFAULT_MAX_RELAY_CONNECTIONS = 2; +// Range of ports to be used for WebRTC connections +// (option only availabe when running in nodejs) +export const WEBRTC_PORT_RANGE = { + min: 10000, + max: 11000 +}; + // Peer connection manager config constants // Number of max concurrent dials per peer diff --git a/packages/peer/src/relay.ts b/packages/peer/src/relay.ts index 657289f5..4349416c 100644 --- a/packages/peer/src/relay.ts +++ b/packages/peer/src/relay.ts @@ -20,7 +20,7 @@ import type { Connection } from '@libp2p/interface-connection'; import { multiaddr } from '@multiformats/multiaddr'; import type { PeerId } from '@libp2p/interface-peer-id'; -import { HOP_TIMEOUT, PUBSUB_DISCOVERY_INTERVAL, PUBSUB_SIGNATURE_POLICY } from './constants.js'; +import { HOP_TIMEOUT, PUBSUB_DISCOVERY_INTERVAL, PUBSUB_SIGNATURE_POLICY, WEBRTC_PORT_RANGE } from './constants.js'; import { PeerHearbeatChecker } from './peer-heartbeat-checker.js'; const log = debug('laconic:relay'); @@ -28,6 +28,7 @@ const log = debug('laconic:relay'); interface Arguments { host: string; port: number; + announce: string; peerIdFile: string; relayPeers: string; } @@ -47,18 +48,26 @@ async function main (): Promise { console.log('Creating a new peer id'); } - const listenMultiaddr = `/ip4/${argv.host}/tcp/${argv.port}/http/p2p-webrtc-direct`; + const listenMultiaddrs = [`/ip4/${argv.host}/tcp/${argv.port}/http/p2p-webrtc-direct`]; + const announceMultiaddrs = []; + + if (argv.announce) { + announceMultiaddrs.push(`/dns4/${argv.announce}/tcp/443/https/p2p-webrtc-direct`); + } const node = await createLibp2p({ peerId, addresses: { - listen: [listenMultiaddr] + listen: listenMultiaddrs, + announce: announceMultiaddrs }, transports: [ webRTCDirect({ wrtc, enableSignalling: true, - nodeType: WebRTCDirectNodeType.Relay + nodeType: WebRTCDirectNodeType.Relay, + initiatorOptions: { webRTCPortRange: WEBRTC_PORT_RANGE }, + receiverOptions: { webRTCPortRange: WEBRTC_PORT_RANGE } }) ], connectionEncryption: [noise()], @@ -144,6 +153,11 @@ function _getArgv (): any { default: '9090', describe: 'Port to start listening on' }, + announce: { + type: 'string', + alias: 'a', + describe: 'Domain name to be used in the announce address' + }, peerIdFile: { type: 'string', alias: 'f', diff --git a/yarn.lock b/yarn.lock index 9ad9f737..5e8b4eb4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -373,12 +373,12 @@ wherearewe "^2.0.0" xsalsa20 "^1.1.0" -"@cerc-io/webrtc-direct@^5.0.0-laconic-0.1.2": - version "5.0.0-laconic-0.1.2" - resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fwebrtc-direct/-/5.0.0-laconic-0.1.2/webrtc-direct-5.0.0-laconic-0.1.2.tgz#b0697f3efe297ce7ccccadd537a36b012425df61" - integrity sha512-SSHpUHDjyTwFX1bYL+cMjKqbAiEJQRd0N5bVMQGl86hWmmVXu+09vtpHxO6VTUZNNpnYkOisQPvn9CUyAsfWng== +"@cerc-io/webrtc-direct@^5.0.0-laconic-0.1.3": + version "5.0.0-laconic-0.1.3" + resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fwebrtc-direct/-/5.0.0-laconic-0.1.3/webrtc-direct-5.0.0-laconic-0.1.3.tgz#14802ba88899c904bddc327082d96cb541523ffb" + integrity sha512-HiRn2eoXbOFM2Dklecr+H76BB0H1H/k4I59Hnjj7tppdlr6wwts9MuA/SX0draXYEiRA5Ft4vXrfo469fGS68A== dependencies: - "@cerc-io/webrtc-peer" "^2.0.2-laconic-0.1.3" + "@cerc-io/webrtc-peer" "^2.0.2-laconic-0.1.4" "@libp2p/interface-transport" "^2.0.0" "@libp2p/interfaces" "^3.0.3" "@libp2p/logger" "^2.0.1" @@ -399,10 +399,10 @@ undici "^5.2.0" wherearewe "^2.0.1" -"@cerc-io/webrtc-peer@^2.0.2-laconic-0.1.3": - version "2.0.2-laconic-0.1.3" - resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fwebrtc-peer/-/2.0.2-laconic-0.1.3/webrtc-peer-2.0.2-laconic-0.1.3.tgz#decfc53539f206d70f7250b8f913a5d74da5c505" - integrity sha512-B0fcADAeU8W0EfdLp/ju6wBDrd/WOXnsqZuGAk+EVTXqvm2cSJiA/TzttmiKQIAvYuWTsLvmRUQ14KHCbtMdSA== +"@cerc-io/webrtc-peer@^2.0.2-laconic-0.1.4": + version "2.0.2-laconic-0.1.4" + resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fwebrtc-peer/-/2.0.2-laconic-0.1.4/webrtc-peer-2.0.2-laconic-0.1.4.tgz#9ce10477ad4f6391de61a3f6f9a6063b09c0e196" + integrity sha512-tCXLCmDW1NKjn9m2ymgh6GbN3YWvEHMIsupW27L2Xt3q02DmWM7acZB04mjvuCDtQt5FC7ok0B6yTQ8a1liXnQ== dependencies: "@libp2p/interfaces" "^3.0.2" "@libp2p/logger" "^2.0.0"