From e9017a5f00a472318ac7b63e2b5fa3c9b73142c0 Mon Sep 17 00:00:00 2001 From: Nico Krause Date: Tue, 26 Mar 2024 12:50:15 +0500 Subject: [PATCH] added useWebRTC and useWebsocket toggle button so both transports can be tested independently --- src/config.js | 12 ++--- src/lib/components/Settings.svelte | 30 ++++++++----- src/lib/network/p2p-operations.js | 70 +++++++++++++++++++++++++++--- src/stores.js | 3 ++ 4 files changed, 92 insertions(+), 23 deletions(-) diff --git a/src/config.js b/src/config.js index 3099c59..670a7ce 100644 --- a/src/config.js +++ b/src/config.js @@ -86,11 +86,11 @@ export const config = { protocol: '/ipfs/kad/1.0.0', peerInfoMapper: removePrivateAddressesMapper })*/ - dht: kadDHT({ - protocolPrefix: "/svelte-pubsub", - maxInboundStreams: 5000, - maxOutboundStreams: 5000, - clientMode: true, - }) + // dht: kadDHT({ + // protocolPrefix: "/svelte-pubsub", + // maxInboundStreams: 5000, + // maxOutboundStreams: 5000, + // clientMode: true, + // }) } } \ No newline at end of file diff --git a/src/lib/components/Settings.svelte b/src/lib/components/Settings.svelte index c8b28de..1014cda 100644 --- a/src/lib/components/Settings.svelte +++ b/src/lib/components/Settings.svelte @@ -1,20 +1,26 @@ + $:window.localStorage.setItem('useWebRTC', $useWebRTC.toString()); + $:window.localStorage.setItem('useWebSocket', $useWebSocket.toString()); + + console.log("useWebRTC",$useWebRTC) + @@ -27,12 +33,6 @@ - - - - - - @@ -49,6 +49,14 @@ size="sm" value={$connectedPeers} /> + + + + + + + + - - diff --git a/src/lib/network/p2p-operations.js b/src/lib/network/p2p-operations.js index 7ab1925..07147e4 100644 --- a/src/lib/network/p2p-operations.js +++ b/src/lib/network/p2p-operations.js @@ -5,6 +5,10 @@ import { LevelBlockstore } from "blockstore-level" import { LevelDatastore } from "datastore-level"; import { bitswap } from '@helia/block-brokers' import { config } from "../../config.js"; +import { webSockets } from "@libp2p/websockets"; +import * as filters from "@libp2p/websockets/filters"; +import { webRTC, webRTCDirect } from "@libp2p/webrtc"; +import { webTransport } from "@libp2p/webtransport"; import { libp2p, helia, @@ -15,8 +19,10 @@ import { dbMyAddressBook, subscription, connectedPeers, - followList, dbMessages, selectedTab, syncedDevices, + followList, dbMessages, selectedTab, syncedDevices,useWebRTC, useWebSocket } from "../../stores.js"; + +import { get } from 'svelte/store'; import { confirm } from "../components/addressModal.js" import {notify, sha256} from "../../utils/utils.js"; import { getIdentityAndCreateOrbitDB } from "$lib/network/getIdendityAndCreateOrbitDB.js"; @@ -27,6 +33,10 @@ let datastore = new LevelDatastore("./helia-data") let messageQueue = {}; let activeConfirmations = {}; +let _libp2p; /** @type {import('libp2p').Libp2p} Value of to the libp2p store variable containing the libp2p instance*/ +libp2p.subscribe((val) => { + _libp2p = val +}); /** * The PubSub topic where deContact is publishing and subscribing @@ -133,6 +143,60 @@ async function handleMessage(dContactMessage) { await processMessageQueue(); } + +// Function to restart libp2p with selected transports +async function restartLibp2p() { + const webRTCEnabled = get(useWebRTC); + const webSocketEnabled = get(useWebSocket); + + // Assuming you have a function to stop the current libp2p instance + if (_libp2p) { + await _libp2p.stop(); + } + + + // Clear existing transports before adding new ones + config.transports = []; + + if (webRTCEnabled) { + const webRTCConfig = webRTC({ + rtcConfiguration: { + iceServers: [{ + urls: [ + 'stun:stun.l.google.com:19302', + 'stun:global.stun.twilio.com:3478' + ] + }] + } + }); + config.transports.push(webRTCConfig, webRTCDirect()); + } + + if (webSocketEnabled) { + const webSocketConfig = webSockets({filter: filters.all}); + config.transports.push(webSocketConfig); + } + console.log("restarting libp2p with config",config) + _libp2p = await createLibp2p(config) + +} + +// React to changes in the toggle states with initial call prevention +let isInitialCall = true; + +useWebRTC.subscribe(() => { + if (!isInitialCall) { + restartLibp2p(); + } +}); + +useWebSocket.subscribe(() => { + if (!isInitialCall) { + restartLibp2p(); + } +}); + +isInitialCall = false; /** * We want to open a confirmation dialog for each sender sending a pub sub message. (in case it's coming in same time) * @returns {Promise} @@ -420,10 +484,6 @@ export async function writeMyAddressIntoRequesterDB(requesterDB) { } } -let _libp2p; /** @type {import('libp2p').Libp2p} Value of to the libp2p store variable containing the libp2p instance*/ -libp2p.subscribe((val) => { - _libp2p = val -}); /** * Subscription to the helia store variable containing the Helia instance diff --git a/src/stores.js b/src/stores.js index f903682..fe89019 100644 --- a/src/stores.js +++ b/src/stores.js @@ -68,6 +68,9 @@ export const recordsSynced = writable(0) export const showNotification = writable() export const notificationMessage = writable() +export const useWebRTC = writable(window.localStorage.getItem('useWebRTC') !== null ? window.localStorage.getItem('useWebRTC') === 'true' : true); +export const useWebSocket = writable(window.localStorage.getItem('useWebSocket') === 'true' ? true : false); // Default off + export const followList = writable([] ) export const qrCodeOpen = writable(false) export const qrCodeData = writable()