From 5c20038a2e8663c5ec82c1c266942bd8402051f8 Mon Sep 17 00:00:00 2001 From: "Parsa Yazdani (Quix)" Date: Sat, 2 Nov 2024 20:32:42 +1100 Subject: [PATCH] Forward signalling messages only to necessary peers --- deno.jsonc | 2 +- src/peers/RTCPeers.ts | 2 +- src/server.ts | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/deno.jsonc b/deno.jsonc index e43a97d..9d232c6 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,6 @@ { "name": "@starfiles/hydrafiles", - "version": "0.7.26", + "version": "0.7.27", "description": "The web privacy layer.", "main": "src/hydrafiles.ts", "exports": { diff --git a/src/peers/RTCPeers.ts b/src/peers/RTCPeers.ts index 5ba772b..ee48a69 100644 --- a/src/peers/RTCPeers.ts +++ b/src/peers/RTCPeers.ts @@ -17,7 +17,7 @@ function extractIPAddress(sdp: string): string { return ipAddresses.filter((ip) => ip !== "0.0.0.0")[0] ?? ipAddresses[0]; } -type Message = { announce: true; from: number } | { offer: RTCSessionDescription; from: number; to: number } | { answer: RTCSessionDescription; from: number; to: number } | { iceCandidate: RTCIceCandidate; from: number; to: number }; +export type Message = { announce: true; from: number } | { offer: RTCSessionDescription; from: number; to: number } | { answer: RTCSessionDescription; from: number; to: number } | { iceCandidate: RTCIceCandidate; from: number; to: number }; type PeerConnection = { conn: RTCPeerConnection; channel: RTCDataChannel }; type PeerConnections = { [id: string]: { offered?: PeerConnection; answered?: PeerConnection } }; diff --git a/src/server.ts b/src/server.ts index 3727b48..cc38816 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,10 +6,11 @@ import Utils from "./utils.ts"; import { join } from "https://deno.land/std@0.224.0/path/mod.ts"; import type Base64 from "npm:base64"; import { HTTPPeer } from "./peers/HTTPPeers.ts"; +import { Message } from "./peers/RTCPeers.ts"; export const hashLocks = new Map>(); const cachedHostnames: { [key: string]: { body: string; headers: Headers } } = {}; -const sockets: WebSocket[] = []; +const sockets: { id: number; socket: WebSocket }[] = []; export const handleRequest = async (req: Request, client: Hydrafiles): Promise => { console.log(`Received Request: ${req.url}`); @@ -20,11 +21,16 @@ export const handleRequest = async (req: Request, client: Hydrafiles): Promise { + const message = JSON.parse(data) as Message; for (let i = 0; i < sockets.length; i++) { - if (sockets[i] !== socket && sockets[i].readyState === 1) sockets[i].send(data); + if (sockets[i].socket !== socket && (!("to" in message) || message.to === sockets[i].id)) { + if (sockets[i].socket.readyState === 1) sockets[i].socket.send(data); + } else if ("from" in message) { + sockets[i].id = message.from; + } } });