webpeerjs 0.1.3 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpeerjs",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "WebPEER.js is decentralized P2P JS library for communication between applications in browser.",
5
5
  "main": "./dist/umd/webpeerjs.js",
6
6
  "module": "./src/webpeerjs.js",
package/src/config.js CHANGED
@@ -1,11 +1,12 @@
1
1
  //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
2
+
2
3
  const prefix = 'webpeerjs'
3
4
  export const CONFIG_PREFIX = prefix
4
5
  export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
5
6
  export const CONFIG_BLOCKSTORE_PATH = prefix+'-blockstore'
6
7
  export const CONFIG_DATASTORE_PATH = prefix+'-datastore'
7
8
  export const CONFIG_DBSTORE_PATH = prefix+'-dbstore'
8
- export const CONFIG_MAX_CONNECTIONS = 100
9
+ export const CONFIG_MAX_CONNECTIONS = 50
9
10
  export const CONFIG_MIN_CONNECTIONS = 0
10
11
  export const CONFIG_DISCOVER_RELAYS = 1
11
12
  export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
@@ -20,7 +21,16 @@ export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
20
21
  export const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io'
21
22
  export const CONFIG_JOIN_ROOM_VERSION = 1
22
23
  export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15000
23
- export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = true
24
+ export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = false
25
+ export const CONFIG_WEBRTC_STUN_URLS = 'stun:stun.l.google.com:19302'
26
+ export const CONFIG_WEBRTC_STUN_URLS_BACKUP = 'stun:global.stun.twilio.com:3478'
27
+ export const CONFIG_WEBRTC_TURN_HOST = 'dHVybjpyZWxheTEuZXhwcmVzc3R1cm4uY29tOjM0Nzg='
28
+ export const CONFIG_WEBRTC_TURN_USER = 'ZWZJSllZNjdDNElRMzFZQUlP'
29
+ export const CONFIG_WEBRTC_TURN_PWD = 'Vk01SmdhODlkYjJaWU9aSA=='
30
+ export const CONFIG_WEBRTC_TURN_HOST_BACKUP = 'dHVybjpzdGFuZGFyZC5yZWxheS5tZXRlcmVkLmNhOjgw'
31
+ export const CONFIG_WEBRTC_TURN_USER_BACKUP = 'ZmZlNmIxOThjOGMxYjM5ODg1OWFiOGY4'
32
+ export const CONFIG_WEBRTC_TURN_PWD_BACKUP = 'aWpkQjVTcTIwREVsZzdDRg=='
33
+ export const CONFIG_MESSAGE_SIZE_LIMIT = 10240 // 10KB
24
34
 
25
35
  // this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
26
36
  export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
package/src/utils.js CHANGED
@@ -1,4 +1,5 @@
1
1
  //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
2
+
2
3
  import * as config from './config'
3
4
  import { Peer as PBPeer } from './peer'
4
5
  import { Key } from 'interface-datastore'
package/src/webpeerjs.js CHANGED
@@ -1,4 +1,5 @@
1
1
  //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
2
+
2
3
  import * as config from './config'
3
4
  import {
4
5
  mkErr,
@@ -700,9 +701,9 @@ class webpeerjs{
700
701
  const msgId = (new Date()).getTime()
701
702
  const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
702
703
  const arr = uint8ArrayFromString(data)
703
- const sizelimit = 102400 // 100KB
704
+ const sizelimit = config.CONFIG_MESSAGE_SIZE_LIMIT
704
705
  if(arr.byteLength > sizelimit){
705
- throw mkErr('data too large')
706
+ throw mkErr('message too large')
706
707
  }
707
708
  const peer = {
708
709
  publicKey: this.#libp2p.peerId.publicKey,
@@ -1032,7 +1033,7 @@ class webpeerjs{
1032
1033
 
1033
1034
 
1034
1035
  //add multiaddr address to queue list
1035
- #dialMultiaddress(mddrs){
1036
+ async #dialMultiaddress(mddrs){
1036
1037
  if(mddrs.length>0){
1037
1038
 
1038
1039
  const id = mddrs[0].toString().split('/').pop()
@@ -1050,10 +1051,21 @@ class webpeerjs{
1050
1051
  const limitCount = config.CONFIG_MAX_CONNECTIONS / 2
1051
1052
 
1052
1053
  if(this.#webPeersId.includes(id)){
1053
- if(webPeerCount>limitCount)return
1054
+ if(webPeerCount>limitCount){
1055
+ return
1056
+ }
1054
1057
  }
1055
1058
  else{
1056
- if(nodePeerCount>limitCount)return
1059
+ if(nodePeerCount>limitCount){
1060
+ //close random peers
1061
+ let peers = []
1062
+ for(const peer of this.#libp2p.getPeers()){
1063
+ peers.push(peer.toString())
1064
+ }
1065
+ const randomKey = Math.floor(Math.random() * peers.length)
1066
+ const randompeerid = peers[randomKey]
1067
+ await this.#libp2p.hangUp(peerIdFromString(randompeerid))
1068
+ }
1057
1069
  }
1058
1070
 
1059
1071
  if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
@@ -1361,13 +1373,13 @@ class webpeerjs{
1361
1373
  #dialKnownPeers(){
1362
1374
  setTimeout(()=>{
1363
1375
  this.#dialSavedKnownID()
1364
- setTimeout(()=>{this.#dialUpdateSavedKnownID()},50000)
1365
- setTimeout(()=>{this.#findHybridPeer()},60000)
1376
+ setTimeout(()=>{this.#dialUpdateSavedKnownID()},20000)
1377
+ setTimeout(()=>{this.#findHybridPeer()},30000)
1366
1378
  setTimeout(()=>{
1367
1379
  const peers = this.#libp2p.getPeers().length
1368
1380
  if(peers == 0){
1369
1381
  this.#dialKnownID()
1370
- setTimeout(()=>{this.#findHybridPeer()},60000)
1382
+ setTimeout(()=>{this.#findHybridPeer()},30000)
1371
1383
  setTimeout(()=>{
1372
1384
  const peers = this.#libp2p.getPeers().length
1373
1385
  if(peers == 0){
@@ -1701,6 +1713,102 @@ class webpeerjs{
1701
1713
  listenaddress.push('/webrtc')
1702
1714
  }
1703
1715
 
1716
+ const turnurls = atob(config.CONFIG_WEBRTC_TURN_HOST)
1717
+ const turnusername = atob(config.CONFIG_WEBRTC_TURN_USER)
1718
+ const turncredential = atob(config.CONFIG_WEBRTC_TURN_PWD)
1719
+
1720
+ const turnurlsbackup = atob(config.CONFIG_WEBRTC_TURN_HOST_BACKUP)
1721
+ const turnusernamebackup = atob(config.CONFIG_WEBRTC_TURN_USER_BACKUP)
1722
+ const turncredentialbackup = atob(config.CONFIG_WEBRTC_TURN_PWD_BACKUP)
1723
+
1724
+ const ice = await new Promise((resolve)=>{
1725
+
1726
+ let stun = false
1727
+ let turn = false
1728
+
1729
+ const timeout = setTimeout(()=>{
1730
+ let ice = []
1731
+ if(stun){
1732
+ ice.push(stun)
1733
+ }else{
1734
+ const backup = {
1735
+ urls: config.CONFIG_WEBRTC_STUN_URLS_BACKUP
1736
+ }
1737
+ ice.push(backup)
1738
+ }
1739
+ if(turn){
1740
+ ice.push(turn)
1741
+ }else{
1742
+ const backup = {
1743
+ urls: turnurlsbackup,
1744
+ username: turnusernamebackup,
1745
+ credential: turncredentialbackup
1746
+ }
1747
+ ice.push(backup)
1748
+ }
1749
+ resolve(ice)
1750
+ },5000)
1751
+
1752
+ function check(){
1753
+ if(stun && turn){
1754
+ let ice = []
1755
+ ice.push(stun)
1756
+ ice.push(turn)
1757
+ clearTimeout(timeout)
1758
+ resolve(ice)
1759
+ }
1760
+ }
1761
+
1762
+ //test ice servers
1763
+
1764
+ const iceServers = [
1765
+ {
1766
+ urls: config.CONFIG_WEBRTC_STUN_URLS
1767
+ },
1768
+ {
1769
+ urls: turnurls,
1770
+ username: turnusername,
1771
+ credential: turncredential
1772
+ }
1773
+ ];
1774
+
1775
+ const pc = new RTCPeerConnection({
1776
+ iceServers
1777
+ });
1778
+
1779
+ pc.onicecandidate = (e) => {
1780
+ if (!e.candidate) return;
1781
+
1782
+ //console.log(e.candidate.candidate);
1783
+
1784
+ // stun works
1785
+ if(e.candidate.type == "srflx"){
1786
+ //console.log('publicip',e.candidate.address);
1787
+ stun = {
1788
+ urls: config.CONFIG_WEBRTC_STUN_URLS
1789
+ }
1790
+ check()
1791
+ }
1792
+
1793
+ // turn works
1794
+ if(e.candidate.type == "relay"){
1795
+ turn = {
1796
+ urls: turnurls,
1797
+ username: turnusername,
1798
+ credential: turncredential
1799
+ }
1800
+ check()
1801
+ }
1802
+ };
1803
+
1804
+ pc.onicecandidateerror = (e) => {
1805
+ //console.error(e);
1806
+ };
1807
+
1808
+ pc.createDataChannel('webpeerjs');
1809
+ pc.createOffer().then(offer => pc.setLocalDescription(offer));
1810
+ })
1811
+
1704
1812
  //create libp2p instance
1705
1813
  const libp2p = await createLibp2p({
1706
1814
  addresses: {
@@ -1711,11 +1819,7 @@ class webpeerjs{
1711
1819
  webSockets(),
1712
1820
  webRTC({
1713
1821
  rtcConfiguration: {
1714
- iceServers: [
1715
- {
1716
- urls: ['stun:stun.l.google.com:19302', 'stun:global.stun.twilio.com:3478'],
1717
- },
1718
- ],
1822
+ iceServers: ice,
1719
1823
  },
1720
1824
  }),
1721
1825
  circuitRelayTransport({