Skip to content

Commit

Permalink
Add Secure WebSockets example
Browse files Browse the repository at this point in the history
  • Loading branch information
D4nte committed Jun 9, 2021
1 parent 2068c84 commit 57213a8
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"which": "^2.0.1"
},
"devDependencies": {
"https": "^1.0.0",
"playwright": "^1.7.1"
}
}
84 changes: 84 additions & 0 deletions examples/transports/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable no-console */
'use strict'

const Libp2p = require('../..')
const TCP = require('libp2p-tcp')
const WebSockets = require('libp2p-websockets')
const { NOISE } = require('libp2p-noise')
const MPLEX = require('libp2p-mplex')

const fs = require('fs');
const https = require('https');
const pipe = require('it-pipe')

const transportKey = WebSockets.prototype[Symbol.toStringTag];

const httpServer = https.createServer({
cert: fs.readFileSync('./test_certs/cert.pem'),
key: fs.readFileSync('./test_certs/key.pem'),
});

const createNode = async (addresses = []) => {
if (!Array.isArray(addresses)) {
addresses = [addresses]
}

const node = await Libp2p.create({
addresses: {
listen: addresses
},
modules: {
transport: [WebSockets],
connEncryption: [NOISE],
streamMuxer: [MPLEX]
},
config: {
transport: {
[transportKey]: {
listenerOptions: { server: httpServer },
},
},
}
})

await node.start()
return node
}

function printAddrs(node, number) {
console.log('node %s is listening on:', number)
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
}

function print ({ stream }) {
pipe(
stream,
async function (source) {
for await (const msg of source) {
console.log(msg.toString())
}
}
)
}

;(async () => {
const [node1, node2] = await Promise.all([
createNode('/ip4/127.0.0.1/tcp/10000/wss'),
createNode([])
])

printAddrs(node1, '1')
printAddrs(node2, '2')

node1.handle('/print', print)
node2.handle('/print', print)

node2.peerStore.addressBook.set(node1.peerId, node1.multiaddrs)

// node 2 (Secure WebSockets) dials to node 1 (Secure Websockets)
const { stream } = await node2.dialProtocol(node1.peerId, '/print')
await pipe(
['node 2 dialed to node 1 successfully'],
stream
)
})();
33 changes: 33 additions & 0 deletions examples/transports/test-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const path = require('path')
const execa = require('execa')
const pDefer = require('p-defer')
const uint8ArrayToString = require('uint8arrays/to-string')

async function test () {
const deferNode1 = pDefer()

process.stdout.write('4.js\n')

const proc = execa('node', [path.join(__dirname, '4.js')], {
cwd: path.resolve(__dirname),
all: true
})

proc.all.on('data', async (data) => {
process.stdout.write(data)
const line = uint8ArrayToString(data)

if (line.includes('node 2 dialed to node 1 successfully')) {
deferNode1.resolve()
}
})

await Promise.all([
deferNode1.promise,
])
proc.kill()
}

module.exports = test
2 changes: 2 additions & 0 deletions examples/transports/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
const test1 = require('./test-1')
const test2 = require('./test-2')
const test3 = require('./test-3')
const test4 = require('./test-4')

async function test() {
await test1()
await test2()
await test3()
await test4()
}

module.exports = test
32 changes: 32 additions & 0 deletions examples/transports/test_certs/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFlzCCA3+gAwIBAgIUMYedwb9L/BtvZ7Lhu71iSKrXsa4wDQYJKoZIhvcNAQEL
BQAwajELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMREwDwYDVQQHDAhTb21lQ2l0
eTESMBAGA1UECgwJTXlDb21wYW55MRMwEQYDVQQLDApNeURpdmlzaW9uMRIwEAYD
VQQDDAkxMjcuMC4wLjEwHhcNMjEwNDI4MDIzMjA5WhcNMjIwNDI4MDIzMjA5WjBq
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExETAPBgNVBAcMCFNvbWVDaXR5MRIw
EAYDVQQKDAlNeUNvbXBhbnkxEzARBgNVBAsMCk15RGl2aXNpb24xEjAQBgNVBAMM
CTEyNy4wLjAuMTCCAiIwDQYJKoZIhvcNAQEB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c6R1/ZRTIH6i73sD4rYs0bx1fCJvkbcgXtKMHEkiHuG/MzR7Pa4cJAVKCx9
lRTgrO7Gkllt2+jp4qg0YhdNq89e0DNA5cyB9H4udRgHQOcrlVRiX9OD/Kz+F5m/
fQwMdbnqdg3ar5DSa8Q5g3bdLbNSCcI9sjCLTkNxUC/XTWGdG03RCVIt1qvBvZHk
JaG6xGpbRZ5CN0T9eindd38JBrkPAPfgl6qhwvcqh6uVFYua+7KmF9K+mKarlmMw
6RWaw2j4sMgUyRIS6fR9vDc20SrtoNvKQM1U6+0VYs1nizfkmsqqqRODmERKbKwc
ahKJFubXfr8gz+PipAKFZbxr2EPAyoiNkx+0eM6Eedo55oP2BoGHEfXEoAonyMFM
F/xTbpFtdRYE2hwsZCk86fpbcPTmdCY8txeZ7+4Bme2d9XXsTAxF64usqQ==
-----END CERTIFICATE-----
52 changes: 52 additions & 0 deletions examples/transports/test_certs/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDTYVwbtBh9Ss5f
YmkMQsRJ8sgEoUuRWEnHSsaqWLICU+KaC1i0AWUBBSxKm3YpZSJU2ajLLI1qpBS9
sZo6nXJzihRGVGq57VRNocRWGEHsdhYvDGMBY/j77e+HdNsT8n8xTRYMl2tRGpKB
gcApWn9yO41u62j7nY3H+B58EO6/zcIjvBahxWYC3E58CPG5PnMQ6AwKrpoajAeZ
53q6ryhMkfRW9j6C/vD8DjyTjikw1cCzHjCbtli0fAFNhczo+UlSNqrLvT7OWs8r
A/CkADHMOMvbOnaN+kmjEEvOX271wVe1wajA9+BzwVGxYgqBaGgsyvWX26Ql8lST
FDZsOYcqvZ241LeCr6/+fG1NoI1CknT3+ioQcRZw2cJftqqj3VSG/6rcMxxMbSVq
t7qZJVzToNx6Eer3K9TvUXiR68y0TBG+dVj9Wjf6idv5UbJHGeAPNOqsK1uuwb5z
oO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4jp7Ty0N+qi8fdaCb2q
tyrYTnHHYId6bUHMBY5QZsYAaTNvYNAO96A0UaNyl42q84iTiLkJYg9SsQPad15W
7gU84Jk6rEMYdndQDvEAHpnZ1y0yA2vtySZYsbK0wj34tgTl+0/8izn7JgF4ezNH
6iQ7Z0OuDT763IrmIxBH0ZEi9YnwSYyIsr6iUYjlQIUuPFRnQYQXEdm5Xfw1pZsL
xhYoTwKCAQB9edDe4LX+0z9i4qr0iHV8H/WoyI5UD/Pc217PKkYM3+ewR9SL9D9z
TS78Sl7HgRgEmIu+MR/u5B2ePf7jkvB/oxyPwqAzJeJ72mV3Mevm27G/Ndd8lt5W
FBCGOx7ZeP4/Cv4mvPD979ix2IalDoWMSWJnpQPN+B1jGeCrUYAXQc1k/vU99gLa
8Tuu3WfBpVAsO7hAC9mu6tuLyfKVqiMOVs2aky9xLqiqW/6uIcGu+owrr+gkDDY/
JfBSUfxYKcjtJiHOEbFGrrRe93XsngmaTz/Hv9A/QLVCuJgWEHlt4WHSc+BtAtaV
9avp6VlyVNfe4KEKW7IekrI0cmfMdXkl
-----END PRIVATE KEY-----
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
},
"dependencies": {
"@motrix/nat-api": "^0.3.1",
"@vascosantos/moving-average": "^1.1.0",
"abort-controller": "^3.0.0",
"aggregate-error": "^3.1.0",
"any-signal": "^2.1.1",
Expand Down Expand Up @@ -108,7 +109,6 @@
"libp2p-utils": "^0.3.1",
"mafmt": "^9.0.0",
"merge-options": "^3.0.4",
"@vascosantos/moving-average": "^1.1.0",
"multiaddr": "^9.0.1",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
Expand Down Expand Up @@ -157,7 +157,7 @@
"libp2p-noise": "^3.0.0",
"libp2p-tcp": "^0.15.4",
"libp2p-webrtc-star": "^0.22.2",
"libp2p-websockets": "^0.15.6",
"libp2p-websockets": "^0.15.8",
"multihashes": "^4.0.2",
"nock": "^13.0.3",
"p-defer": "^3.0.0",
Expand Down

0 comments on commit 57213a8

Please sign in to comment.