Skip to content

Commit

Permalink
Add support for connecting peers through a relay
Browse files Browse the repository at this point in the history
  • Loading branch information
haadcode committed Aug 2, 2024
1 parent 05138d2 commit d19f249
Show file tree
Hide file tree
Showing 18 changed files with 482 additions and 150 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
run: npm ci
- name: Run linter
run: npm run lint
- name: Run libp2p relay in the background
run: npm run start:relay:background
- name: Run an Orbiter 1 instance in the background
run: npm run start:orbiter1:background
- name: Run an Orbiter 2 instance in the background
Expand Down
3 changes: 3 additions & 0 deletions conf/webpack.tests.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export default (env, argv) => {
})
],
resolve: {
alias: {
'./test-config': path.resolve(__dirname, '../test/utils/test-config-browser')
},
modules: [
'node_modules',
path.resolve(__dirname, '../node_modules')
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
"lint:fix": "standard --fix",
"copy:fixtures1": "rm -rf ./orbiter1 && mkdir -p ./orbiter1/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore1/** ./orbiter1/voyager/orbiter/keystore/",
"copy:fixtures2": "rm -rf ./orbiter2 && mkdir -p ./orbiter2/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore2/** ./orbiter2/voyager/orbiter/keystore/",
"start:orbiter1": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 -vv",
"start:orbiter2": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 -vv",
"start:orbiter1:background": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 &",
"start:orbiter2:background": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 &",
"start:relay": "node ./test/utils/relay.js",
"start:relay:background": "node ./test/utils/relay.js &",
"start:orbiter1": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 -w 55441 --allow --directory ./orbiter1 -vv",
"start:orbiter2": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 -w 55442 --allow --directory ./orbiter2 -vv",
"start:orbiter1:background": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 -w 55441 --allow --directory ./orbiter1 &",
"start:orbiter2:background": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 -w 55442 --allow --directory ./orbiter2 &",
"test:browser": "npm run build:tests && ./node_modules/.bin/playwright-test test/browser/bundle.js --runner mocha"
},
"standard": {
Expand Down
5 changes: 5 additions & 0 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ yargs(hideBin(process.argv))
description: 'The port to listen on. Defaults to 0.',
type: 'number'
})
.option('wsport', {
alias: 'w',
description: 'The port to listen on for WebSockets. Defaults to 0.',
type: 'number'
})
},
async (argv) => {
await daemon({ options: argv })
Expand Down
5 changes: 2 additions & 3 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export default async ({ options }) => {
const defaultAccess = options.allow ? Access.ALLOW : Access.DENY

options.verbose = options.verbose || 0

options.silent = options.silent || false

options.port = options.port || 0
options.wsport = options.wsport || 0

const id = orbiterId

Expand All @@ -54,7 +53,7 @@ export default async ({ options }) => {
const peerId = await createFromPrivKey(await keystore.getKey(id))
await keystore.close()

const libp2p = await createLibp2p(await libp2pConfig({ peerId, port: options.port }))
const libp2p = await createLibp2p(await libp2pConfig({ peerId, port: options.port, websocketPort: options.wsport }))

log('peerid:', libp2p.peerId.toString())
for (const addr of libp2p.getMultiaddrs().map(e => e.toString())) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/lander.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async ({ orbitdb, orbiterAddressOrId }) => {
})()
}

const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol)
const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol, { runOnTransientConnection: true })

await pipe(pinDBs, stream, async (source) => {
for await (const chunk of source) {
Expand All @@ -39,7 +39,7 @@ export default async ({ orbitdb, orbiterAddressOrId }) => {
})()
}

const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol)
const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol, { runOnTransientConnection: true })

await pipe(unpinDBs, stream, async source => {
for await (const chunk of source) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orbiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default async ({ orbitdb, defaultAccess, verbose } = {}) => {
await pipe(stream, handleRequest({ log, orbitdb, pins, dbs, auth }), stream)
}

await orbitdb.ipfs.libp2p.handle(voyagerProtocol, handleMessages)
await orbitdb.ipfs.libp2p.handle(voyagerProtocol, handleMessages, { runOnTransientConnection: true })

log('open pinned databases')

Expand Down
9 changes: 6 additions & 3 deletions src/utils/libp2p-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { webSockets } from '@libp2p/websockets'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { mdns } from '@libp2p/mdns'

export const config = ({ peerId, port } = {}) => {
export const config = ({ peerId, port, websocketPort } = {}) => {
const conf = {
addresses: {
listen: [
'/ip4/0.0.0.0/tcp/0',
`/ip4/0.0.0.0/tcp/${port || 0}/ws`
`/ip4/0.0.0.0/tcp/${port || 0}`,
`/ip4/0.0.0.0/tcp/${websocketPort || 0}/ws`
]
},
transports: [
Expand All @@ -24,6 +24,9 @@ export const config = ({ peerId, port } = {}) => {
streamMuxers: [
yamux()
],
connectionGater: {
denyDialMultiaddr: () => false // allow dialling of private addresses.
},
peerDiscovery: [
mdns()
],
Expand Down
Loading

0 comments on commit d19f249

Please sign in to comment.