webpeerjs 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -18
- package/dist/esm/webpeerjs.js +250 -171
- package/dist/umd/webpeerjs.js +50738 -47615
- package/package.json +5 -1
- package/src/config.js +49 -9
- package/src/peer.js +2 -5
- package/src/webpeerjs.js +159 -113
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Simple peer-to-peer with IPFS",
|
|
5
5
|
"main": "./dist/umd/webpeerjs.js",
|
|
6
6
|
"module": "./src/webpeerjs.js",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"start": "npm run dev",
|
|
20
20
|
"dev": "vite serve test",
|
|
21
|
+
"demo": "vite serve demo",
|
|
21
22
|
"eslint": "eslint ./src",
|
|
22
23
|
"eslint:fix": "eslint ./src --fix",
|
|
23
24
|
"removedir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\"",
|
|
@@ -53,11 +54,14 @@
|
|
|
53
54
|
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
54
55
|
"@helia/delegated-routing-v1-http-api-client": "^3.0.1",
|
|
55
56
|
"@libp2p/circuit-relay-v2": "^1.0.24",
|
|
57
|
+
"@libp2p/dcutr": "^1.1.0",
|
|
56
58
|
"@libp2p/identify": "^2.0.2",
|
|
57
59
|
"@libp2p/kad-dht": "^12.0.17",
|
|
58
60
|
"@libp2p/peer-id": "^4.1.2",
|
|
59
61
|
"@libp2p/pubsub-peer-discovery": "^10.0.2",
|
|
60
62
|
"@libp2p/simple-metrics": "^1.0.2",
|
|
63
|
+
"@libp2p/webrtc": "^4.1.0",
|
|
64
|
+
"@libp2p/websockets": "^8.1.0",
|
|
61
65
|
"@libp2p/webtransport": "^4.0.32",
|
|
62
66
|
"datastore-idb": "^2.1.9",
|
|
63
67
|
"libp2p": "^1.6.0"
|
package/src/config.js
CHANGED
|
@@ -11,25 +11,29 @@ export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectiv
|
|
|
11
11
|
export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
|
|
12
12
|
export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
|
|
13
13
|
export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS]
|
|
14
|
+
export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
|
|
14
15
|
export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
|
|
15
16
|
export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
|
16
17
|
export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
|
17
18
|
export const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io'
|
|
18
19
|
export const CONFIG_JOIN_ROOM_VERSION = 1
|
|
20
|
+
export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15000
|
|
21
|
+
|
|
22
|
+
// this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
|
|
23
|
+
export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
|
|
24
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
|
25
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
|
26
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
|
|
27
|
+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
|
|
28
|
+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
|
29
|
+
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
|
|
30
|
+
]
|
|
19
31
|
|
|
20
32
|
export const CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS = [
|
|
21
33
|
'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
|
22
34
|
'QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
|
23
35
|
'QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt',
|
|
24
|
-
'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ'
|
|
25
|
-
'12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
|
|
26
|
-
'12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
|
|
27
|
-
'12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
|
|
28
|
-
'12D3KooWHh98YpAkJsn3ULjMjK1n9QVkXmi8Sb3gTDMatHxCmDP5',
|
|
29
|
-
'12D3KooWS79EhkPU7ESUwgG4vyHHzW9FDNZLoWVth9b5N5NSrvaj',
|
|
30
|
-
'12D3KooWBbkCD5MpJhMc1mfPAVGEyVkQnyxPKGS7AHwDqQM2JUsk',
|
|
31
|
-
'12D3KooWKLdecs31Zmo2pLBjR9HY2vWo3VwM4eBm21Czeucbe6FL',
|
|
32
|
-
'12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
|
|
36
|
+
'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ'
|
|
33
37
|
]
|
|
34
38
|
|
|
35
39
|
export const CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS = [
|
|
@@ -39,4 +43,40 @@ export const CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS = [
|
|
|
39
43
|
export const CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS = CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS.concat(CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS)
|
|
40
44
|
|
|
41
45
|
export const CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS = [
|
|
46
|
+
{
|
|
47
|
+
"Peers": [
|
|
48
|
+
{
|
|
49
|
+
"Addrs": [
|
|
50
|
+
"/dns6/sv15.bootstrap.libp2p.io/tcp/443/wss",
|
|
51
|
+
"/dns4/sv15.bootstrap.libp2p.io/tcp/443/wss"
|
|
52
|
+
],
|
|
53
|
+
"ID": "QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
|
54
|
+
"Schema": "peer"
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"Peers": [
|
|
60
|
+
{
|
|
61
|
+
"Addrs": [
|
|
62
|
+
"/dns4/am6.bootstrap.libp2p.io/tcp/443/wss",
|
|
63
|
+
"/dns6/am6.bootstrap.libp2p.io/tcp/443/wss"
|
|
64
|
+
],
|
|
65
|
+
"ID": "QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
|
|
66
|
+
"Schema": "peer"
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"Peers": [
|
|
72
|
+
{
|
|
73
|
+
"Addrs": [
|
|
74
|
+
"/dns6/sg1.bootstrap.libp2p.io/tcp/443/wss",
|
|
75
|
+
"/dns4/sg1.bootstrap.libp2p.io/tcp/443/wss"
|
|
76
|
+
],
|
|
77
|
+
"ID": "QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
|
|
78
|
+
"Schema": "peer"
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|
|
42
82
|
]
|
package/src/peer.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
1
|
+
//this code comes from https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/blob/9d0da565f70e9b2403251c9d11dfc0b9b52babfa/src/peer.ts
|
|
2
|
+
|
|
6
3
|
import { decodeMessage, encodeMessage, message } from 'protons-runtime';
|
|
7
4
|
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc';
|
|
8
5
|
export var Peer;
|
package/src/webpeerjs.js
CHANGED
|
@@ -20,6 +20,9 @@ import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-
|
|
|
20
20
|
import { createLibp2p } from 'libp2p'
|
|
21
21
|
import { IDBDatastore } from 'datastore-idb'
|
|
22
22
|
import { webTransport } from '@libp2p/webtransport'
|
|
23
|
+
import { webSockets } from '@libp2p/websockets'
|
|
24
|
+
import { webRTC } from '@libp2p/webrtc'
|
|
25
|
+
import { dcutr } from '@libp2p/dcutr'
|
|
23
26
|
import { noise } from '@chainsafe/libp2p-noise'
|
|
24
27
|
import { yamux } from '@chainsafe/libp2p-yamux'
|
|
25
28
|
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
|
|
@@ -138,6 +141,10 @@ class webpeerjs{
|
|
|
138
141
|
|
|
139
142
|
this.id = this.#libp2p.peerId.toString()
|
|
140
143
|
|
|
144
|
+
for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
|
|
145
|
+
this.#libp2p.services.pubsub.subscribe(topic)
|
|
146
|
+
}
|
|
147
|
+
|
|
141
148
|
|
|
142
149
|
//listen to peer connect event
|
|
143
150
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
@@ -152,7 +159,7 @@ class webpeerjs{
|
|
|
152
159
|
const addr = connect.addr
|
|
153
160
|
|
|
154
161
|
if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id)){
|
|
155
|
-
if(!this.#connections.has(id)&&addr.includes('webtransport')){
|
|
162
|
+
if((!this.#connections.has(id) || (this.#connections.get(id).includes('/wss/')))&&addr.includes('webtransport')){
|
|
156
163
|
await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
|
|
157
164
|
}
|
|
158
165
|
}
|
|
@@ -206,7 +213,7 @@ class webpeerjs{
|
|
|
206
213
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
207
214
|
const topic = event.detail.topic
|
|
208
215
|
const senderPeerId = event.detail.from.toString()
|
|
209
|
-
|
|
216
|
+
|
|
210
217
|
try{
|
|
211
218
|
|
|
212
219
|
//if it is webpeer
|
|
@@ -242,15 +249,17 @@ class webpeerjs{
|
|
|
242
249
|
const addrs = this.#discoveredPeers.get(senderPeerId)
|
|
243
250
|
let mddrs = []
|
|
244
251
|
for(const addr of addrs){
|
|
252
|
+
if(!addr.includes('webrtc'))continue
|
|
245
253
|
const mddr = multiaddr(addr)
|
|
246
254
|
mddrs.push(mddr)
|
|
247
255
|
}
|
|
248
256
|
this.#dialMultiaddress(mddrs)
|
|
249
257
|
}
|
|
250
|
-
else{
|
|
258
|
+
else if(this.#connectedPeers.has(senderPeerId)){
|
|
251
259
|
const addrs = this.#connectedPeers.get(senderPeerId).addrs
|
|
252
260
|
let mddrs = []
|
|
253
261
|
for(const addr of addrs){
|
|
262
|
+
if(!addr.includes('webrtc'))continue
|
|
254
263
|
const mddr = multiaddr(addr)
|
|
255
264
|
mddrs.push(mddr)
|
|
256
265
|
}
|
|
@@ -299,8 +308,10 @@ class webpeerjs{
|
|
|
299
308
|
|
|
300
309
|
//update room members
|
|
301
310
|
if(!this.#rooms[room].members.includes(id)){
|
|
302
|
-
this.#
|
|
303
|
-
|
|
311
|
+
if(this.#connectedPeers.has(id)){
|
|
312
|
+
this.#rooms[room].members.push(id)
|
|
313
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
314
|
+
}
|
|
304
315
|
}
|
|
305
316
|
|
|
306
317
|
//inbound message
|
|
@@ -319,8 +330,10 @@ class webpeerjs{
|
|
|
319
330
|
for(const room of Object.keys(this.#rooms)){
|
|
320
331
|
//update room members
|
|
321
332
|
if(!this.#rooms[room].members.includes(id)){
|
|
322
|
-
this.#
|
|
323
|
-
|
|
333
|
+
if(this.#connectedPeers.has(id)){
|
|
334
|
+
this.#rooms[room].members.push(id)
|
|
335
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
336
|
+
}
|
|
324
337
|
}
|
|
325
338
|
}
|
|
326
339
|
}
|
|
@@ -329,7 +342,7 @@ class webpeerjs{
|
|
|
329
342
|
|
|
330
343
|
//repply announce with ping
|
|
331
344
|
if(signal == 'announce'){
|
|
332
|
-
setTimeout(()=>{this.#ping('
|
|
345
|
+
setTimeout(()=>{this.#ping('')},1000)
|
|
333
346
|
//console.log('rooms',rooms)
|
|
334
347
|
}
|
|
335
348
|
|
|
@@ -344,12 +357,7 @@ class webpeerjs{
|
|
|
344
357
|
//console.log('from '+event.detail.from.toString())
|
|
345
358
|
mkDebug(err)
|
|
346
359
|
}
|
|
347
|
-
|
|
348
|
-
const json = JSON.parse(topic)
|
|
349
|
-
const room = json.room
|
|
350
|
-
const message = new TextDecoder().decode(event.detail.data)
|
|
351
|
-
this.#rooms[room].onMessage(message)
|
|
352
|
-
}
|
|
360
|
+
|
|
353
361
|
}
|
|
354
362
|
|
|
355
363
|
})
|
|
@@ -390,6 +398,7 @@ class webpeerjs{
|
|
|
390
398
|
if(!this.#connections.has(id)){
|
|
391
399
|
let mddrs = []
|
|
392
400
|
for(const addr of addrs){
|
|
401
|
+
if(!addr.includes('webrtc'))continue
|
|
393
402
|
const mddr = multiaddr(addr)
|
|
394
403
|
mddrs.push(mddr)
|
|
395
404
|
}
|
|
@@ -449,6 +458,7 @@ class webpeerjs{
|
|
|
449
458
|
//if this disconnected peer is known bootstrap redial it
|
|
450
459
|
else if(this.#dialedKnownBootstrap.has(id)){
|
|
451
460
|
const addr = this.#connections.get(id)
|
|
461
|
+
if(addr.includes('/wss/'))return
|
|
452
462
|
let mddrs = []
|
|
453
463
|
const addrs = multiaddr(addr)
|
|
454
464
|
mddrs.push(addrs)
|
|
@@ -482,7 +492,7 @@ class webpeerjs{
|
|
|
482
492
|
const mddrs = []
|
|
483
493
|
peer.addresses.forEach((addr)=>{
|
|
484
494
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id
|
|
485
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
495
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash') && maddr.includes('webrtc')){
|
|
486
496
|
mddrs.push(maddr)
|
|
487
497
|
}
|
|
488
498
|
})
|
|
@@ -491,8 +501,29 @@ class webpeerjs{
|
|
|
491
501
|
this.#ping()
|
|
492
502
|
})
|
|
493
503
|
|
|
494
|
-
this.#libp2p.addEventListener('peer:identify', (evt) => {
|
|
504
|
+
this.#libp2p.addEventListener('peer:identify', async (evt) => {
|
|
495
505
|
//console.log('peer:identify '+evt.detail.peerId.toString(),evt.detail)
|
|
506
|
+
|
|
507
|
+
const id = evt.detail.peerId.toString()
|
|
508
|
+
|
|
509
|
+
if(config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS.includes(id)){
|
|
510
|
+
const remoteAddr = evt.detail.connection.remoteAddr.toString()
|
|
511
|
+
if(remoteAddr.includes('/wss/')){
|
|
512
|
+
let addrs = []
|
|
513
|
+
let mddrs = []
|
|
514
|
+
for(const peer of evt.detail.listenAddrs){
|
|
515
|
+
if(!peer.toString().includes('webtransport'))continue
|
|
516
|
+
const addr = peer.toString()+'/p2p/'+id
|
|
517
|
+
const mddr = multiaddr(addr)
|
|
518
|
+
addrs.push(addr)
|
|
519
|
+
mddrs.push(mddr)
|
|
520
|
+
}
|
|
521
|
+
this.#dialedKnownBootstrap.set(id,addrs)
|
|
522
|
+
await this.#libp2p.hangUp(peerIdFromString(id))
|
|
523
|
+
this.#dialMultiaddress(mddrs)
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
496
527
|
if(evt.detail.protocols.includes(config.CONFIG_PROTOCOL)){
|
|
497
528
|
//console.log('peer:identify '+evt.detail.peerId.toString(),evt)
|
|
498
529
|
|
|
@@ -625,7 +656,7 @@ class webpeerjs{
|
|
|
625
656
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
626
657
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
627
658
|
|
|
628
|
-
const topics = config.
|
|
659
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
629
660
|
|
|
630
661
|
this.#rooms[room] = {
|
|
631
662
|
onMessage : () => {},
|
|
@@ -679,6 +710,7 @@ class webpeerjs{
|
|
|
679
710
|
else{
|
|
680
711
|
this.status = 'unconnected'
|
|
681
712
|
}
|
|
713
|
+
this.#ping()
|
|
682
714
|
}
|
|
683
715
|
|
|
684
716
|
async #registerProtocol(){
|
|
@@ -771,7 +803,7 @@ class webpeerjs{
|
|
|
771
803
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
772
804
|
maxInboundStreams: 50,
|
|
773
805
|
maxOutboundStreams: 50,
|
|
774
|
-
runOnTransientConnection:
|
|
806
|
+
runOnTransientConnection:false
|
|
775
807
|
})
|
|
776
808
|
|
|
777
809
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -818,7 +850,7 @@ class webpeerjs{
|
|
|
818
850
|
|
|
819
851
|
try{
|
|
820
852
|
|
|
821
|
-
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
853
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:false})
|
|
822
854
|
|
|
823
855
|
const output = await pipe(
|
|
824
856
|
message,
|
|
@@ -835,7 +867,7 @@ class webpeerjs{
|
|
|
835
867
|
return string
|
|
836
868
|
}
|
|
837
869
|
)
|
|
838
|
-
|
|
870
|
+
//console.log(output)
|
|
839
871
|
const json = JSON.parse(output)
|
|
840
872
|
if(json.protocol == config.CONFIG_PROTOCOL){
|
|
841
873
|
const address = [addr]
|
|
@@ -877,37 +909,39 @@ class webpeerjs{
|
|
|
877
909
|
}
|
|
878
910
|
}
|
|
879
911
|
|
|
880
|
-
#findHybridPeer(){
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
912
|
+
async #findHybridPeer(){
|
|
913
|
+
|
|
914
|
+
if(!navigator.onLine)return
|
|
915
|
+
if(!this.#isDialEnabled)return
|
|
916
|
+
|
|
917
|
+
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
918
|
+
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
919
|
+
//console.log('findPeer',target)
|
|
920
|
+
const peerId = peerIdFromString(target)
|
|
921
|
+
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
|
|
922
|
+
|
|
923
|
+
//console.info(peerInfo)
|
|
924
|
+
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
|
|
925
|
+
//console.info('findPeer',event)
|
|
926
|
+
if (event.name === 'FINAL_PEER'){
|
|
927
|
+
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
|
|
928
|
+
let mddrs = []
|
|
929
|
+
let addrs = []
|
|
930
|
+
const id = event.peer.id.toString()
|
|
931
|
+
for(const mddr of event.peer.multiaddrs){
|
|
932
|
+
const peeraddr = mddr.toString()+'/p2p/'+id
|
|
933
|
+
const peermddr = multiaddr(peeraddr)
|
|
934
|
+
addrs.push(peeraddr)
|
|
935
|
+
mddrs.push(peermddr)
|
|
936
|
+
}
|
|
937
|
+
this.#dialedKnownBootstrap.set(id,addrs)
|
|
938
|
+
if(!this.#isConnected(id)){
|
|
939
|
+
this.#dialMultiaddress(mddrs)
|
|
906
940
|
}
|
|
907
941
|
}
|
|
908
942
|
}
|
|
909
943
|
}
|
|
910
|
-
}
|
|
944
|
+
}
|
|
911
945
|
}
|
|
912
946
|
|
|
913
947
|
|
|
@@ -1037,7 +1071,7 @@ class webpeerjs{
|
|
|
1037
1071
|
|
|
1038
1072
|
//announce and ping via pupsub peer discovery
|
|
1039
1073
|
async #announce(){
|
|
1040
|
-
const topics = config.
|
|
1074
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1041
1075
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1042
1076
|
const peer = {
|
|
1043
1077
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1049,7 +1083,7 @@ class webpeerjs{
|
|
|
1049
1083
|
}
|
|
1050
1084
|
}
|
|
1051
1085
|
async #ping(){
|
|
1052
|
-
const topics = config.
|
|
1086
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1053
1087
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1054
1088
|
const peer = {
|
|
1055
1089
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1251,69 +1285,47 @@ class webpeerjs{
|
|
|
1251
1285
|
if(peers == 0){
|
|
1252
1286
|
this.#dialKnownPeers()
|
|
1253
1287
|
}
|
|
1254
|
-
},
|
|
1288
|
+
},120*1000)
|
|
1255
1289
|
}
|
|
1256
1290
|
|
|
1257
1291
|
|
|
1258
1292
|
//dial to all known bootstrap peers and DNS
|
|
1259
1293
|
#dialKnownPeers(){
|
|
1260
|
-
//this.#dialKnownBootstrap()
|
|
1261
1294
|
setTimeout(()=>{
|
|
1262
1295
|
this.#dialSavedKnownID()
|
|
1263
|
-
this.#findHybridPeer()
|
|
1264
1296
|
setTimeout(()=>{this.#dialUpdateSavedKnownID()},50000)
|
|
1297
|
+
setTimeout(()=>{this.#findHybridPeer()},60000)
|
|
1265
1298
|
setTimeout(()=>{
|
|
1266
1299
|
const peers = this.#libp2p.getPeers().length
|
|
1267
1300
|
if(peers == 0){
|
|
1268
1301
|
this.#dialKnownID()
|
|
1269
|
-
this.#findHybridPeer()
|
|
1302
|
+
setTimeout(()=>{this.#findHybridPeer()},60000)
|
|
1270
1303
|
setTimeout(()=>{
|
|
1271
1304
|
const peers = this.#libp2p.getPeers().length
|
|
1272
1305
|
if(peers == 0){
|
|
1273
|
-
|
|
1274
|
-
|
|
1306
|
+
this.#dialKnownBootstrap()
|
|
1307
|
+
setTimeout(()=>{this.#findHybridPeer()},15000)
|
|
1275
1308
|
setTimeout(()=>{
|
|
1276
1309
|
const peers = this.#libp2p.getPeers().length
|
|
1277
1310
|
if(peers == 0){
|
|
1278
|
-
|
|
1279
|
-
|
|
1311
|
+
this.#dialKnownDNS()
|
|
1312
|
+
setTimeout(()=>{this.#findHybridPeer()},15000)
|
|
1313
|
+
setTimeout(()=>{
|
|
1314
|
+
const peers = this.#libp2p.getPeers().length
|
|
1315
|
+
if(peers == 0){
|
|
1316
|
+
this.#dialKnownDNSonly()
|
|
1317
|
+
setTimeout(()=>{this.#findHybridPeer()},15000)
|
|
1318
|
+
}
|
|
1319
|
+
},config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS)
|
|
1280
1320
|
}
|
|
1281
|
-
},
|
|
1321
|
+
},config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS)
|
|
1282
1322
|
}
|
|
1283
|
-
},
|
|
1323
|
+
},config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS)
|
|
1284
1324
|
}
|
|
1285
|
-
},
|
|
1325
|
+
},config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS)
|
|
1286
1326
|
},5000)
|
|
1287
1327
|
}
|
|
1288
1328
|
|
|
1289
|
-
|
|
1290
|
-
//dial based on known bootsrap peers address
|
|
1291
|
-
#dialKnownBootstrap(){
|
|
1292
|
-
|
|
1293
|
-
if(!navigator.onLine)return
|
|
1294
|
-
if(!this.#isDialEnabled)return
|
|
1295
|
-
|
|
1296
|
-
const bootstrap = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS
|
|
1297
|
-
for(const peer of bootstrap){
|
|
1298
|
-
const address = peer.Peers[0].Addrs
|
|
1299
|
-
const id = peer.Peers[0].ID
|
|
1300
|
-
let mddrs = []
|
|
1301
|
-
let addrs = []
|
|
1302
|
-
for(const addr of address){
|
|
1303
|
-
const peeraddr = addr+'/p2p/'+id
|
|
1304
|
-
const peermddr = multiaddr(peeraddr)
|
|
1305
|
-
addrs.push(peeraddr)
|
|
1306
|
-
mddrs.push(peermddr)
|
|
1307
|
-
}
|
|
1308
|
-
|
|
1309
|
-
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1310
|
-
if(!this.#isConnected(id)){
|
|
1311
|
-
this.#dialMultiaddress(mddrs)
|
|
1312
|
-
}
|
|
1313
|
-
|
|
1314
|
-
}
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
1329
|
async #dialSavedKnownID(){
|
|
1318
1330
|
|
|
1319
1331
|
if(!navigator.onLine)return
|
|
@@ -1434,10 +1446,43 @@ class webpeerjs{
|
|
|
1434
1446
|
}
|
|
1435
1447
|
}
|
|
1436
1448
|
}
|
|
1437
|
-
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
//dial based on known bootsrap peers address using Websocket expected
|
|
1452
|
+
#dialKnownBootstrap(){
|
|
1453
|
+
|
|
1454
|
+
if(!navigator.onLine)return
|
|
1455
|
+
if(!this.#isDialEnabled)return
|
|
1456
|
+
|
|
1457
|
+
const bootstrap = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS
|
|
1458
|
+
for(const peer of bootstrap){
|
|
1459
|
+
const address = peer.Peers[0].Addrs
|
|
1460
|
+
const id = peer.Peers[0].ID
|
|
1461
|
+
let mddrs = []
|
|
1462
|
+
let addrs = []
|
|
1463
|
+
for(const addr of address){
|
|
1464
|
+
if(!addr.includes('wss'))continue
|
|
1465
|
+
const peeraddr = addr+'/p2p/'+id
|
|
1466
|
+
const peermddr = multiaddr(peeraddr)
|
|
1467
|
+
addrs.push(peeraddr)
|
|
1468
|
+
mddrs.push(peermddr)
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1472
|
+
this.#isDialWebsocket = true
|
|
1473
|
+
if(!this.#isConnected(id)){
|
|
1474
|
+
this.#dialMultiaddress(mddrs)
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1438
1479
|
|
|
1439
1480
|
//dial based on known bootstrap DNS
|
|
1440
|
-
|
|
1481
|
+
async #dialKnownDNS(){
|
|
1482
|
+
|
|
1483
|
+
if(!navigator.onLine)return
|
|
1484
|
+
if(!this.#isDialEnabled)return
|
|
1485
|
+
|
|
1441
1486
|
const dnsresolver = config.CONFIG_DNS_RESOLVER
|
|
1442
1487
|
const bootstrapdns = config.CONFIG_KNOWN_BOOTSTRAP_DNS
|
|
1443
1488
|
const response = await fetch(dnsresolver+'?name='+bootstrapdns+'&type=txt')
|
|
@@ -1466,16 +1511,21 @@ class webpeerjs{
|
|
|
1466
1511
|
}
|
|
1467
1512
|
|
|
1468
1513
|
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1514
|
+
this.#isDialWebsocket = true
|
|
1469
1515
|
if(!this.#isConnected(id)){
|
|
1470
1516
|
this.#dialMultiaddress(mddrs)
|
|
1471
1517
|
}
|
|
1472
1518
|
}
|
|
1473
1519
|
|
|
1474
|
-
}
|
|
1520
|
+
}
|
|
1475
1521
|
|
|
1476
1522
|
|
|
1477
1523
|
//dial based on known bootstrap DNS using DNS resolver only
|
|
1478
|
-
|
|
1524
|
+
async #dialKnownDNSonly(){
|
|
1525
|
+
|
|
1526
|
+
if(!navigator.onLine)return
|
|
1527
|
+
if(!this.#isDialEnabled)return
|
|
1528
|
+
|
|
1479
1529
|
const dnsresolver = config.CONFIG_DNS_RESOLVER
|
|
1480
1530
|
const bootstrapdns = config.CONFIG_KNOWN_BOOTSTRAP_DNS
|
|
1481
1531
|
const response = await fetch(dnsresolver+'?name='+bootstrapdns+'&type=txt')
|
|
@@ -1488,11 +1538,11 @@ class webpeerjs{
|
|
|
1488
1538
|
const dnsaddr = '_dnsaddr.'+arr[2]
|
|
1489
1539
|
this.#dialDNSWebsocketWebtransport(id,dnsaddr)
|
|
1490
1540
|
}
|
|
1491
|
-
}
|
|
1541
|
+
}
|
|
1492
1542
|
|
|
1493
1543
|
|
|
1494
1544
|
//dial DNS with webtransport and websocket
|
|
1495
|
-
|
|
1545
|
+
async #dialDNSWebsocketWebtransport(id,dnsaddr){
|
|
1496
1546
|
const dnsresolver = config.CONFIG_DNS_RESOLVER
|
|
1497
1547
|
const response = await fetch(dnsresolver+'?name='+dnsaddr+'&type=txt')
|
|
1498
1548
|
const json = await response.json()
|
|
@@ -1516,41 +1566,34 @@ class webpeerjs{
|
|
|
1516
1566
|
this.#dialMultiaddress(mddrs)
|
|
1517
1567
|
this.#dialWebsocket(mddrs)
|
|
1518
1568
|
}
|
|
1519
|
-
}
|
|
1569
|
+
}
|
|
1520
1570
|
|
|
1521
1571
|
|
|
1522
1572
|
//dial only webtransport multiaddrs
|
|
1523
1573
|
async #dialWebtransport(multiaddrs){
|
|
1524
1574
|
const webTransportMadrs = multiaddrs.filter((maddr) => maddr.protoNames().includes('webtransport')&&maddr.protoNames().includes('certhash'))
|
|
1525
|
-
for (const
|
|
1575
|
+
for (const mddr of webTransportMadrs) {
|
|
1526
1576
|
try {
|
|
1527
|
-
//console.log(`attempting to dial webtransport multiaddr: %o`,
|
|
1528
|
-
await this.#libp2p.dial(
|
|
1577
|
+
//console.log(`attempting to dial webtransport multiaddr: %o`, mddr.toString())
|
|
1578
|
+
await this.#libp2p.dial(mddr)
|
|
1529
1579
|
return // if we succeed dialing the peer, no need to try another address
|
|
1530
1580
|
} catch (error) {
|
|
1531
|
-
//console.log(`failed to dial webtransport multiaddr: %o`,
|
|
1581
|
+
//console.log(`failed to dial webtransport multiaddr: %o`, mddr.toString())
|
|
1532
1582
|
mkDebug(error)
|
|
1533
1583
|
}
|
|
1534
1584
|
}
|
|
1535
1585
|
}
|
|
1536
|
-
|
|
1537
|
-
//dial only webtransport multiaddrs
|
|
1538
|
-
/*#dialWebtransport1(multiaddrs){
|
|
1539
|
-
const webTransportMadrs = multiaddrs.filter((maddr) => maddr.protoNames().includes('webtransport')&&maddr.protoNames().includes('certhash'))
|
|
1540
|
-
if(webTransportMadrs.length == 0)return
|
|
1541
|
-
this.#libp2p.dial(webTransportMadrs).then((data)=>{console.warn(data)},(data)=>{console.warn(data)})
|
|
1542
|
-
}*/
|
|
1543
1586
|
|
|
1544
1587
|
//dial only websocket multiaddrs
|
|
1545
1588
|
async #dialWebsocket(multiaddrs){
|
|
1546
1589
|
const webSocketMadrs = multiaddrs.filter((maddr) => maddr.protoNames().includes('wss'))
|
|
1547
|
-
for (const
|
|
1590
|
+
for (const mddr of webSocketMadrs) {
|
|
1548
1591
|
try {
|
|
1549
|
-
//console.log(`attempting to dial websocket multiaddr: %o`,
|
|
1550
|
-
await this.#libp2p.dial(
|
|
1592
|
+
//console.log(`attempting to dial websocket multiaddr: %o`, mddr)
|
|
1593
|
+
await this.#libp2p.dial(mddr)
|
|
1551
1594
|
return // if we succeed dialing the peer, no need to try another address
|
|
1552
1595
|
} catch (error) {
|
|
1553
|
-
//console.log(`failed to dial websocket multiaddr: %o`,
|
|
1596
|
+
//console.log(`failed to dial websocket multiaddr: %o`, mddr)
|
|
1554
1597
|
mkDebug(error)
|
|
1555
1598
|
}
|
|
1556
1599
|
}
|
|
@@ -1588,10 +1631,13 @@ class webpeerjs{
|
|
|
1588
1631
|
const libp2p = await createLibp2p({
|
|
1589
1632
|
addresses: {
|
|
1590
1633
|
listen: [
|
|
1634
|
+
'/webrtc'
|
|
1591
1635
|
],
|
|
1592
1636
|
},
|
|
1593
1637
|
transports:[
|
|
1594
|
-
webTransport(),
|
|
1638
|
+
webTransport(),
|
|
1639
|
+
webSockets(),
|
|
1640
|
+
webRTC(),
|
|
1595
1641
|
circuitRelayTransport({
|
|
1596
1642
|
discoverRelays: config.CONFIG_DISCOVER_RELAYS,
|
|
1597
1643
|
reservationConcurrency: 1,
|
|
@@ -1656,7 +1702,7 @@ class webpeerjs{
|
|
|
1656
1702
|
allowPublishToZeroTopicPeers: true,
|
|
1657
1703
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1658
1704
|
ignoreDuplicatePublishError: true,
|
|
1659
|
-
runOnTransientConnection:
|
|
1705
|
+
runOnTransientConnection:false,
|
|
1660
1706
|
}),
|
|
1661
1707
|
identify: identify(),
|
|
1662
1708
|
identifyPush: identifyPush(),
|
|
@@ -1665,7 +1711,7 @@ class webpeerjs{
|
|
|
1665
1711
|
peerInfoMapper: removePrivateAddressesMapper,
|
|
1666
1712
|
clientMode: false
|
|
1667
1713
|
}),
|
|
1668
|
-
|
|
1714
|
+
dcutr: dcutr()
|
|
1669
1715
|
},
|
|
1670
1716
|
peerStore: {
|
|
1671
1717
|
persistence: true,
|