webpeerjs 0.2.0 → 0.2.2
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 +26 -26
- package/package.json +2 -2
- package/src/config.js +5 -5
- package/src/webpeer.js +184 -131
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# WebPEER
|
|
2
2
|
|
|
3
|
-
WebPEER is a P2P Network that Runs in a Standard Browser.
|
|
3
|
+
WebPEER is a P2P Network that Runs in a Standard Browser. It allows developers to build peer-to-peer webapps without relying on centralized servers. Aims to return internet control back to users.
|
|
4
4
|
|
|
5
5
|
[>DEMO<](https://nuzulul.github.io/webpeerjs/demo/chat.html)
|
|
6
6
|
|
|
@@ -13,12 +13,14 @@ WebPEER Network run over [`libp2p gossipsub`](https://docs.libp2p.io/concepts/se
|
|
|
13
13
|
|
|
14
14
|
> However, as a cooperative protocol, it may be possible for peers to interfere with the message routing algorithm in a way that disrupts the flow of messages through the network.
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## Benefit
|
|
17
17
|
|
|
18
|
-
* ✅ Distributed P2P
|
|
18
|
+
* ✅ Distributed P2P Network
|
|
19
19
|
* ✅ Scalable Peers
|
|
20
|
-
* ✅
|
|
21
|
-
* ✅ Broadcast
|
|
20
|
+
* ✅ Accessible in Standard Browser
|
|
21
|
+
* ✅ Broadcast Channel Available
|
|
22
|
+
* ✅ No Need Server
|
|
23
|
+
* ✅ Freedom
|
|
22
24
|
|
|
23
25
|
## Ideas
|
|
24
26
|
|
|
@@ -26,25 +28,24 @@ WebPEER Network run over [`libp2p gossipsub`](https://docs.libp2p.io/concepts/se
|
|
|
26
28
|
* Voting / Polling
|
|
27
29
|
* Collaborative activity
|
|
28
30
|
* IoT
|
|
29
|
-
*
|
|
31
|
+
* Social media
|
|
30
32
|
* Remote control
|
|
31
33
|
* Multiplayer games
|
|
32
|
-
*
|
|
34
|
+
* Distributed web
|
|
33
35
|
* Signalling protocol
|
|
34
36
|
* Location tracker
|
|
35
|
-
*
|
|
37
|
+
* Activity tracker.
|
|
38
|
+
* Chat messenger
|
|
36
39
|
|
|
37
40
|
## Try it out!
|
|
38
41
|
|
|
39
|
-
* Go to a deployed chat demo at : [
|
|
42
|
+
* Go to a deployed chat demo at : [p2pchat](https://nuzulul.github.io/webpeerjs/demo/chat.html) .
|
|
40
43
|
* Open the app on another device.
|
|
41
44
|
* Both your devices should connected.
|
|
42
45
|
* Now start sending message.
|
|
43
46
|
|
|
44
47
|
## Browser Support
|
|
45
|
-

|
|
46
|
-
--- | --- | --- | --- | --- | --- |
|
|
47
|
-
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
|
|
48
|
+
     
|
|
48
49
|
|
|
49
50
|
## Quickstart
|
|
50
51
|
|
|
@@ -68,7 +69,7 @@ CDN :
|
|
|
68
69
|
</script>
|
|
69
70
|
```
|
|
70
71
|
|
|
71
|
-
##
|
|
72
|
+
## Usage
|
|
72
73
|
|
|
73
74
|
```
|
|
74
75
|
import { createWebPEER } from 'webpeerjs'
|
|
@@ -102,35 +103,34 @@ Create a new peer node.
|
|
|
102
103
|
|
|
103
104
|
`config` - Configuration object contains:
|
|
104
105
|
|
|
105
|
-
- `networkName` - Unique identifier of
|
|
106
|
+
- `networkName` - Unique identifier name of the network to build.
|
|
106
107
|
|
|
107
|
-
- `rtcConfiguration` - **(optional)** Custom [rtcConfiguration](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection) for WebRTC
|
|
108
|
+
- `rtcConfiguration` - **(optional)** Custom [rtcConfiguration](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection) for WebRTC connection.
|
|
108
109
|
|
|
109
110
|
### `peer.id`
|
|
110
111
|
|
|
111
|
-
Get the unique ID of
|
|
112
|
+
Get the unique ID of this peer node.
|
|
112
113
|
|
|
113
114
|
### `peer.status`
|
|
114
115
|
|
|
115
|
-
Get the node status, returns `
|
|
116
|
+
Get the peer node status, returns `connecting` or `connected`.
|
|
116
117
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
Get all connected peers.
|
|
118
|
+
- `connecting` - Currently not connected and is trying to connect to the network.
|
|
119
|
+
- `connected` - Currently connected to the network.
|
|
120
120
|
|
|
121
121
|
### `room = peer.joinRoom(namespace)`
|
|
122
122
|
|
|
123
|
-
Join to
|
|
123
|
+
Join to a room, returns an object.
|
|
124
124
|
|
|
125
|
-
- `room.sendMessage()` -
|
|
126
|
-
- `romm.onMessage((message,
|
|
127
|
-
- `room.onMembersChange((members)=>{})` - Listen on room members update.
|
|
125
|
+
- `room.sendMessage()` - Method to broadcast message to the room.
|
|
126
|
+
- `romm.onMessage((message,peer_id)=>{})` - Listen on incoming broadcast message.
|
|
127
|
+
- `room.onMembersChange((members)=>{})` - Listen on the room members update.
|
|
128
128
|
|
|
129
|
-
##
|
|
129
|
+
## See Also
|
|
130
130
|
|
|
131
131
|
- [p2p.js](https://github.com/nuzulul/p2p.js) - Alternative simple api WebRTC library with auto matchmaking without signaling server.
|
|
132
132
|
|
|
133
133
|
## License
|
|
134
134
|
|
|
135
|
-
[MIT
|
|
135
|
+
[MIT](https://github.com/nuzulul/webpeerjs/blob/main/LICENSE) (c) 2024 [Nuzulul Zulkarnain](https://github.com/nuzulul)
|
|
136
136
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "P2P Network that Runs in a Standard Browser",
|
|
5
|
+
"description": "WebPEER is P2P Network that Runs in a Standard Browser",
|
|
6
6
|
"main": "./src/webpeer.js",
|
|
7
7
|
"module": "./src/webpeer.js",
|
|
8
8
|
"exports": {
|
package/src/config.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
2
|
|
|
3
|
-
const prefix = 'webpeer
|
|
3
|
+
const prefix = 'webpeer'
|
|
4
4
|
export const CONFIG_PREFIX = prefix
|
|
5
5
|
export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
|
|
6
6
|
export const CONFIG_BLOCKSTORE_PATH = prefix+'-blockstore'
|
|
7
7
|
export const CONFIG_DATASTORE_PATH = prefix+'-datastore'
|
|
8
8
|
export const CONFIG_DBSTORE_PATH = prefix+'-dbstore'
|
|
9
|
-
export const CONFIG_MAX_CONNECTIONS =
|
|
9
|
+
export const CONFIG_MAX_CONNECTIONS = 50
|
|
10
10
|
export const CONFIG_MIN_CONNECTIONS = 0
|
|
11
11
|
export const CONFIG_DISCOVER_RELAYS = 1
|
|
12
12
|
export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
|
|
@@ -20,12 +20,12 @@ export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
|
|
20
20
|
export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
|
21
21
|
export const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io'
|
|
22
22
|
export const CONFIG_JOIN_ROOM_VERSION = 1
|
|
23
|
-
export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS =
|
|
23
|
+
export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15*1000
|
|
24
24
|
export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = false
|
|
25
|
-
export const
|
|
26
|
-
export const CONFIG_DEBUG_ENABLED = true
|
|
25
|
+
export const CONFIG_DEBUG_ENABLED = false
|
|
27
26
|
export const CONFIG_AUTODIAL_MAX_ERROR_LIMIT = 45
|
|
28
27
|
export const CONFIG_DIAL_MAX_ERROR_LIMIT = 55
|
|
28
|
+
export const CONFIG_DIAL_WEBSOCKET_FIRST = true;
|
|
29
29
|
|
|
30
30
|
// this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
|
|
31
31
|
export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
|
package/src/webpeer.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
import * as config from './config.js'
|
|
4
4
|
import {
|
|
5
5
|
mkErr,
|
|
@@ -97,9 +97,6 @@ class webpeerjs{
|
|
|
97
97
|
//message tracker avoid double
|
|
98
98
|
#msgIdtracker
|
|
99
99
|
|
|
100
|
-
//inbound message time tracker
|
|
101
|
-
#msgTimeTracker
|
|
102
|
-
|
|
103
100
|
//map of peer exchange data
|
|
104
101
|
#peerexchangedata
|
|
105
102
|
|
|
@@ -113,9 +110,6 @@ class webpeerjs{
|
|
|
113
110
|
//callback to websocket dialable
|
|
114
111
|
#onWebsocketFn
|
|
115
112
|
|
|
116
|
-
//time tracker for sending message
|
|
117
|
-
#sendMessageTimeTracker
|
|
118
|
-
|
|
119
113
|
id
|
|
120
114
|
status
|
|
121
115
|
IPFS
|
|
@@ -149,8 +143,6 @@ class webpeerjs{
|
|
|
149
143
|
this.#lastTimeConnectToNetwork = new Date().getTime()
|
|
150
144
|
this.#lastTimeReceiveData = new Date().getTime()
|
|
151
145
|
this.#onConnectQueue = []
|
|
152
|
-
this.#msgTimeTracker = new Map()
|
|
153
|
-
this.#sendMessageTimeTracker = 0
|
|
154
146
|
|
|
155
147
|
this.peers = (function(f) {
|
|
156
148
|
return f
|
|
@@ -160,7 +152,7 @@ class webpeerjs{
|
|
|
160
152
|
return libp2p.status
|
|
161
153
|
})(this.#libp2p);
|
|
162
154
|
|
|
163
|
-
this.status = '
|
|
155
|
+
this.status = 'connecting'
|
|
164
156
|
|
|
165
157
|
this.IPFS = (function(libp2p,discoveredPeers) {
|
|
166
158
|
const obj = {libp2p,discoveredPeers}
|
|
@@ -191,7 +183,13 @@ class webpeerjs{
|
|
|
191
183
|
}
|
|
192
184
|
this.#dialMultiaddress(mddrs)
|
|
193
185
|
}
|
|
194
|
-
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const broadcast = {
|
|
189
|
+
id : this.id,
|
|
190
|
+
address : this.address
|
|
191
|
+
}
|
|
192
|
+
signalingserver.send(broadcast);
|
|
195
193
|
|
|
196
194
|
setInterval(()=>{
|
|
197
195
|
const broadcast = {
|
|
@@ -199,7 +197,7 @@ class webpeerjs{
|
|
|
199
197
|
address : this.address
|
|
200
198
|
}
|
|
201
199
|
signalingserver.send(broadcast);
|
|
202
|
-
},
|
|
200
|
+
},15*1000)
|
|
203
201
|
|
|
204
202
|
|
|
205
203
|
|
|
@@ -446,17 +444,11 @@ class webpeerjs{
|
|
|
446
444
|
|
|
447
445
|
//inbound message
|
|
448
446
|
//use #msgIdtracker to prevent double message
|
|
449
|
-
//use #msgTimeTracker to limit inbound message count /s
|
|
450
447
|
if(message){
|
|
451
448
|
const msgID = msgId+id
|
|
452
|
-
let oldmsgtime = 0
|
|
453
449
|
let newmsgtime = new Date().getTime()
|
|
454
|
-
if(this.#
|
|
455
|
-
//const msgtimelimit = 1000
|
|
456
|
-
const msgtimelimit = -1
|
|
457
|
-
if(!this.#msgIdtracker.has(msgID) && newmsgtime-oldmsgtime>msgtimelimit){
|
|
450
|
+
if(!this.#msgIdtracker.has(msgID)){
|
|
458
451
|
this.#msgIdtracker.set(msgID,newmsgtime)
|
|
459
|
-
this.#msgTimeTracker.set(id,newmsgtime)
|
|
460
452
|
this.#rooms[room].onMessage(message,id)
|
|
461
453
|
}
|
|
462
454
|
}
|
|
@@ -590,21 +582,21 @@ class webpeerjs{
|
|
|
590
582
|
let count = this.#trackDisconnect.get(id)
|
|
591
583
|
count++
|
|
592
584
|
this.#trackDisconnect.set(id,count)
|
|
585
|
+
|
|
593
586
|
//console.log(this.#trackDisconnect)
|
|
594
587
|
if(count>5){
|
|
595
588
|
if(config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS.includes(id)){
|
|
596
589
|
return
|
|
597
590
|
}
|
|
598
|
-
}
|
|
599
|
-
else if(count>10){
|
|
600
591
|
if(this.#dbstoreData.has(id)){
|
|
601
592
|
this.#dbstoreData.delete(id)
|
|
602
593
|
}
|
|
603
594
|
|
|
604
595
|
if(!this.#webPeersId.includes(id) && !this.#dialedKnownBootstrap.has(id)){
|
|
605
596
|
return
|
|
606
|
-
}
|
|
597
|
+
}
|
|
607
598
|
}
|
|
599
|
+
|
|
608
600
|
}
|
|
609
601
|
else{
|
|
610
602
|
this.#trackDisconnect.set(id,0)
|
|
@@ -667,8 +659,14 @@ class webpeerjs{
|
|
|
667
659
|
const addrs = []
|
|
668
660
|
peer.addresses.forEach((address)=>{
|
|
669
661
|
const addr = address.multiaddr.toString()+'/p2p/'+id
|
|
670
|
-
if(
|
|
662
|
+
if(config.CONFIG_DIAL_WEBSOCKET_FIRST){
|
|
663
|
+
|
|
671
664
|
addrs.push(addr)
|
|
665
|
+
|
|
666
|
+
}else{
|
|
667
|
+
if(addr.includes('webtransport') && addr.includes('certhash')){
|
|
668
|
+
addrs.push(addr)
|
|
669
|
+
}
|
|
672
670
|
}
|
|
673
671
|
})
|
|
674
672
|
if(!config.CONFIG_RUN_ON_TRANSIENT_CONNECTION)addrs.reverse()
|
|
@@ -676,6 +674,22 @@ class webpeerjs{
|
|
|
676
674
|
this.address = addrs
|
|
677
675
|
this.#ping()
|
|
678
676
|
this.#peerDiscoveryHybrid()
|
|
677
|
+
|
|
678
|
+
if(addrs.length > 0){
|
|
679
|
+
const broadcast = {
|
|
680
|
+
id : this.id,
|
|
681
|
+
address : this.address
|
|
682
|
+
}
|
|
683
|
+
signalingserver.send(broadcast);
|
|
684
|
+
this.status = 'connected'
|
|
685
|
+
}else{
|
|
686
|
+
if(this.#connectedPeers.size > 0){
|
|
687
|
+
this.status = 'connected'
|
|
688
|
+
}else{
|
|
689
|
+
this.status = 'connecting'
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
679
693
|
})
|
|
680
694
|
|
|
681
695
|
|
|
@@ -752,10 +766,11 @@ class webpeerjs{
|
|
|
752
766
|
|
|
753
767
|
this.#registerProtocol()
|
|
754
768
|
|
|
755
|
-
|
|
769
|
+
//get on signal from metrics
|
|
756
770
|
onMetrics((data)=>{
|
|
757
771
|
const signal = metrics(data)
|
|
758
772
|
this.#isDialEnabled = signal.isDialEnabled
|
|
773
|
+
if(!this.#isDialEnabled){console.log('dial disabled')};
|
|
759
774
|
onDialFn(signal.isAutoDialEnabled)
|
|
760
775
|
|
|
761
776
|
})
|
|
@@ -780,6 +795,14 @@ class webpeerjs{
|
|
|
780
795
|
this.#garbageCollectionMsgIdTracker()
|
|
781
796
|
},10e3)
|
|
782
797
|
|
|
798
|
+
//if no address and peers after timeout dial bootstarp with websocket
|
|
799
|
+
/*setTimeout(()=>{
|
|
800
|
+
if(this.address.length === 0 && this.#connectedPeersArr.length === 0){
|
|
801
|
+
console.log('backup');
|
|
802
|
+
this.#dialKnownDNS();
|
|
803
|
+
}
|
|
804
|
+
},90e3)*/
|
|
805
|
+
|
|
783
806
|
|
|
784
807
|
/*setTimeout(async()=>{
|
|
785
808
|
try{
|
|
@@ -830,17 +853,17 @@ class webpeerjs{
|
|
|
830
853
|
|
|
831
854
|
joinRoom = room => {
|
|
832
855
|
if (this.#rooms[room]) {
|
|
833
|
-
return
|
|
834
|
-
this.#rooms[room].sendMessage,
|
|
835
|
-
this.#rooms[room].listenMessage,
|
|
836
|
-
this.#rooms[room].onMembersChange
|
|
837
|
-
|
|
856
|
+
return {
|
|
857
|
+
sendMessage : this.#rooms[room].sendMessage,
|
|
858
|
+
onMessage : this.#rooms[room].listenMessage,
|
|
859
|
+
onMembersChange : this.#rooms[room].onMembersChange
|
|
860
|
+
}
|
|
838
861
|
|
|
839
862
|
|
|
840
863
|
}
|
|
841
864
|
|
|
842
865
|
if (!room) {
|
|
843
|
-
throw mkErr('
|
|
866
|
+
throw mkErr('Room name is required')
|
|
844
867
|
}
|
|
845
868
|
|
|
846
869
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
@@ -859,18 +882,9 @@ class webpeerjs{
|
|
|
859
882
|
onMessage : () => {},
|
|
860
883
|
listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
|
|
861
884
|
sendMessage : async (message) => {
|
|
862
|
-
const now = (new Date()).getTime()
|
|
863
|
-
//if(now-this.#sendMessageTimeTracker < 1000){
|
|
864
|
-
//throw mkErr('can not send more than 1 message/s')
|
|
865
|
-
//}
|
|
866
|
-
this.#sendMessageTimeTracker = now
|
|
867
885
|
const msgId = crypto.randomUUID();
|
|
868
886
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
869
887
|
const arr = uint8ArrayFromString(data)
|
|
870
|
-
const sizelimit = config.CONFIG_MESSAGE_SIZE_LIMIT
|
|
871
|
-
if(arr.byteLength > sizelimit){
|
|
872
|
-
throw mkErr(`Message too large more than ${sizelimit} byte`);
|
|
873
|
-
}
|
|
874
888
|
const peer = {
|
|
875
889
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
876
890
|
addrs: [arr],
|
|
@@ -976,12 +990,18 @@ class webpeerjs{
|
|
|
976
990
|
const item = {id:peer[0],address:peer[1].addrs}
|
|
977
991
|
this.#connectedPeersArr.push(item)
|
|
978
992
|
}
|
|
993
|
+
|
|
994
|
+
//update status
|
|
979
995
|
if(this.#connectedPeers.size > 0){
|
|
980
|
-
this.status = 'connected'
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
996
|
+
this.status = 'connected';
|
|
997
|
+
}else{
|
|
998
|
+
if(this.address.length > 0){
|
|
999
|
+
this.status = 'connected';
|
|
1000
|
+
}else{
|
|
1001
|
+
this.status = 'connecting';
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
985
1005
|
this.#ping()
|
|
986
1006
|
}
|
|
987
1007
|
|
|
@@ -1193,44 +1213,6 @@ class webpeerjs{
|
|
|
1193
1213
|
}
|
|
1194
1214
|
}
|
|
1195
1215
|
|
|
1196
|
-
async #findHybridPeer(){
|
|
1197
|
-
|
|
1198
|
-
return;
|
|
1199
|
-
|
|
1200
|
-
if(!navigator.onLine)return
|
|
1201
|
-
if(!this.#isDialEnabled)return
|
|
1202
|
-
if(this.#connectedPeers.size > 5)return
|
|
1203
|
-
|
|
1204
|
-
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1205
|
-
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
1206
|
-
//console.log('findPeer',target)
|
|
1207
|
-
const peerId = peerIdFromString(target)
|
|
1208
|
-
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
|
|
1209
|
-
|
|
1210
|
-
//console.info(peerInfo)
|
|
1211
|
-
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
|
|
1212
|
-
//console.info('findPeer',event)
|
|
1213
|
-
if (event.name === 'FINAL_PEER'){
|
|
1214
|
-
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
|
|
1215
|
-
let mddrs = []
|
|
1216
|
-
let addrs = []
|
|
1217
|
-
const id = event.peer.id.toString()
|
|
1218
|
-
for(const mddr of event.peer.multiaddrs){
|
|
1219
|
-
const peeraddr = mddr.toString()+'/p2p/'+id
|
|
1220
|
-
const peermddr = multiaddr(peeraddr)
|
|
1221
|
-
addrs.push(peeraddr)
|
|
1222
|
-
mddrs.push(peermddr)
|
|
1223
|
-
}
|
|
1224
|
-
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1225
|
-
if(!this.#isConnected(id)){
|
|
1226
|
-
this.#dialMultiaddress(mddrs)
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
1216
|
|
|
1235
1217
|
//check the last seen in web peer
|
|
1236
1218
|
#trackLastSeen(){
|
|
@@ -1595,6 +1577,8 @@ class webpeerjs{
|
|
|
1595
1577
|
}
|
|
1596
1578
|
}
|
|
1597
1579
|
}
|
|
1580
|
+
|
|
1581
|
+
|
|
1598
1582
|
//track for good connection
|
|
1599
1583
|
async #connectionTracker(){
|
|
1600
1584
|
|
|
@@ -1628,41 +1612,104 @@ class webpeerjs{
|
|
|
1628
1612
|
}
|
|
1629
1613
|
|
|
1630
1614
|
|
|
1631
|
-
//dial to all known bootstrap peers
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1615
|
+
//dial to all known bootstrap peers
|
|
1616
|
+
|
|
1617
|
+
#dialKnownPeers(method){
|
|
1618
|
+
|
|
1619
|
+
const peers = this.#libp2p.getPeers().length;
|
|
1620
|
+
|
|
1621
|
+
if(!method){
|
|
1622
|
+
if(config.CONFIG_DIAL_WEBSOCKET_FIRST){
|
|
1623
|
+
this.#isDialWebsocket = true
|
|
1624
|
+
this.#onWebsocketFn(true)
|
|
1625
|
+
setTimeout(()=>this.#dialKnownPeers('default'), 5*1000);
|
|
1626
|
+
}else{
|
|
1627
|
+
setTimeout(()=>this.#dialKnownPeers('webtransport'), 5*1000);
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
if(method === 'default' && peers === 0){
|
|
1632
|
+
this.#dialDefaultBootstarp();
|
|
1633
|
+
setTimeout(()=>this.#dialKnownPeers('webtransport'), config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS);
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
if(method === 'webtransport' && peers === 0){
|
|
1637
|
+
this.#dialSavedKnownID();
|
|
1638
|
+
setTimeout(()=>this.#dialUpdateSavedKnownID(),20*1000);
|
|
1639
|
+
setTimeout(()=>this.#dialKnownPeers('id'), config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS);
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
if(method === 'id' && peers === 0){
|
|
1643
|
+
this.#dialKnownID();
|
|
1644
|
+
setTimeout(()=>this.#dialKnownPeers('bootstarp'), config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS);
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
if(method === 'bootstarp' && peers === 0){
|
|
1648
|
+
this.#dialKnownBootstrap();
|
|
1649
|
+
setTimeout(()=>this.#dialKnownPeers('dns'), config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS);
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
if(method === 'dns' && peers === 0){
|
|
1653
|
+
this.#dialKnownDNS();
|
|
1654
|
+
setTimeout(()=>this.#dialKnownPeers('dnsonly'), config.CONFIG_TIMEOUT_DIAL_KNOWN_PEERS);
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
if(method === 'dnsonly' && peers === 0){
|
|
1658
|
+
this.#dialKnownDNSonly();
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
async #findHybridPeer(){
|
|
1664
|
+
|
|
1665
|
+
this.#dialKnownID();
|
|
1666
|
+
return;
|
|
1667
|
+
|
|
1668
|
+
if(!navigator.onLine)return
|
|
1669
|
+
if(!this.#isDialEnabled)return
|
|
1670
|
+
if(this.#connectedPeers.size > 5)return
|
|
1671
|
+
|
|
1672
|
+
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1673
|
+
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
1674
|
+
//console.log('findPeer',target)
|
|
1675
|
+
const peerId = peerIdFromString(target)
|
|
1676
|
+
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
|
|
1677
|
+
|
|
1678
|
+
//console.info(peerInfo)
|
|
1679
|
+
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
|
|
1680
|
+
//console.info('findPeer',event)
|
|
1681
|
+
if (event.name === 'FINAL_PEER'){
|
|
1682
|
+
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
|
|
1683
|
+
let mddrs = []
|
|
1684
|
+
let addrs = []
|
|
1685
|
+
const id = event.peer.id.toString()
|
|
1686
|
+
for(const mddr of event.peer.multiaddrs){
|
|
1687
|
+
const peeraddr = mddr.toString()+'/p2p/'+id
|
|
1688
|
+
const peermddr = multiaddr(peeraddr)
|
|
1689
|
+
addrs.push(peeraddr)
|
|
1690
|
+
mddrs.push(peermddr)
|
|
1691
|
+
}
|
|
1692
|
+
this.#dialedKnownBootstrap.set(id,addrs)
|
|
1693
|
+
if(!this.#isConnected(id)){
|
|
1694
|
+
this.#dialMultiaddress(mddrs)
|
|
1661
1695
|
}
|
|
1662
|
-
}
|
|
1696
|
+
}
|
|
1663
1697
|
}
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
async #dialDefaultBootstarp(){
|
|
1703
|
+
for(const addr of config.CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES){
|
|
1704
|
+
const mddr = multiaddr(addr)
|
|
1705
|
+
try {
|
|
1706
|
+
//console.log(`attempting to dial websocket multiaddr: %o`, mddr)
|
|
1707
|
+
await this.#libp2p.dial(mddr)
|
|
1708
|
+
} catch (error) {
|
|
1709
|
+
//console.log(`failed to dial websocket multiaddr: %o`, mddr)
|
|
1710
|
+
mkDebug(error)
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1666
1713
|
}
|
|
1667
1714
|
|
|
1668
1715
|
async #dialSavedKnownID(){
|
|
@@ -1697,11 +1744,11 @@ class webpeerjs{
|
|
|
1697
1744
|
const id = peer.ID
|
|
1698
1745
|
let mddrs = []
|
|
1699
1746
|
let addrs = []
|
|
1700
|
-
for(const
|
|
1701
|
-
const
|
|
1702
|
-
const
|
|
1703
|
-
addrs.push(
|
|
1704
|
-
mddrs.push(
|
|
1747
|
+
for(const rawaddr of address){
|
|
1748
|
+
const addr = rawaddr.toString()+'/p2p/'+id.toString()
|
|
1749
|
+
const mddr = multiaddr(addr)
|
|
1750
|
+
addrs.push(addr)
|
|
1751
|
+
mddrs.push(mddr)
|
|
1705
1752
|
}
|
|
1706
1753
|
|
|
1707
1754
|
this.#dialedKnownBootstrap.set(id,addrs)
|
|
@@ -1712,18 +1759,26 @@ class webpeerjs{
|
|
|
1712
1759
|
}
|
|
1713
1760
|
}
|
|
1714
1761
|
|
|
1762
|
+
|
|
1763
|
+
//Update saved bootstrap address
|
|
1715
1764
|
async #dialUpdateSavedKnownID(){
|
|
1716
1765
|
|
|
1717
1766
|
if(!navigator.onLine)return
|
|
1718
1767
|
if(!this.#isDialEnabled)return
|
|
1719
1768
|
|
|
1720
1769
|
let firsttime = true
|
|
1770
|
+
|
|
1721
1771
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1772
|
+
|
|
1722
1773
|
if(this.#dbstoreData.has(target)){
|
|
1723
1774
|
firsttime = false
|
|
1724
1775
|
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1779
|
+
|
|
1725
1780
|
if(!this.#connections.has(target) && (this.#dbstoreData.has(target) || firsttime)){
|
|
1726
|
-
|
|
1781
|
+
|
|
1727
1782
|
const api = config.CONFIG_DELEGATED_API
|
|
1728
1783
|
const delegatedClient = delegatedRoutingV1HttpApiClient({url:api})({logger: defaultLogger()})
|
|
1729
1784
|
const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
|
|
@@ -1737,11 +1792,11 @@ class webpeerjs{
|
|
|
1737
1792
|
const id = peer.ID
|
|
1738
1793
|
let mddrs = []
|
|
1739
1794
|
let addrs = []
|
|
1740
|
-
for(const
|
|
1741
|
-
const
|
|
1742
|
-
const
|
|
1743
|
-
addrs.push(
|
|
1744
|
-
mddrs.push(
|
|
1795
|
+
for(const rawaddr of address){
|
|
1796
|
+
const addr = rawaddr.toString()+'/p2p/'+id.toString()
|
|
1797
|
+
const mddr = multiaddr(addr)
|
|
1798
|
+
addrs.push(addr)
|
|
1799
|
+
mddrs.push(mddr)
|
|
1745
1800
|
}
|
|
1746
1801
|
|
|
1747
1802
|
this.#dialedKnownBootstrap.set(id,addrs)
|
|
@@ -1863,7 +1918,7 @@ class webpeerjs{
|
|
|
1863
1918
|
|
|
1864
1919
|
//dial based on known bootstrap DNS using DNS resolver only
|
|
1865
1920
|
async #dialKnownDNSonly(){
|
|
1866
|
-
console.log('#dialKnownDNSonly()')
|
|
1921
|
+
//console.log('#dialKnownDNSonly()')
|
|
1867
1922
|
if(!navigator.onLine)return
|
|
1868
1923
|
if(!this.#isDialEnabled)return
|
|
1869
1924
|
|
|
@@ -2018,13 +2073,11 @@ const createWebPEER = async (configuration) => {
|
|
|
2018
2073
|
listen: listenaddress,
|
|
2019
2074
|
},
|
|
2020
2075
|
transports:[
|
|
2021
|
-
webTransport(),
|
|
2076
|
+
//webTransport(),
|
|
2022
2077
|
webSockets(),
|
|
2023
2078
|
webRTC(webrtcconfig),
|
|
2024
2079
|
circuitRelayTransport({
|
|
2025
|
-
|
|
2026
|
-
reservationConcurrency: 1,
|
|
2027
|
-
maxReservationQueueLength: 3
|
|
2080
|
+
reservationConcurrency: config.CONFIG_DISCOVER_RELAYS
|
|
2028
2081
|
}),
|
|
2029
2082
|
],
|
|
2030
2083
|
connectionManager: {
|