Skip to content

Commit

Permalink
Merge pull request #136 from StarfilesFileSharing/alpha
Browse files Browse the repository at this point in the history
Forward signalling messages only to necessary peers
  • Loading branch information
QuixThe2nd authored Nov 2, 2024
2 parents ecd5b0d + 5c20038 commit 2fe372e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/peers/RTCPeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };

Expand Down
12 changes: 9 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Utils from "./utils.ts";
import { join } from "https://deno.land/[email protected]/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<string, Promise<Response>>();
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<Response> => {
console.log(`Received Request: ${req.url}`);
Expand All @@ -20,11 +21,16 @@ export const handleRequest = async (req: Request, client: Hydrafiles): Promise<R
try {
if (req.headers.get("upgrade") === "websocket") {
const { socket, response } = Deno.upgradeWebSocket(req);
sockets.push(socket);
sockets.push({ socket, id: 0 });

socket.addEventListener("message", ({ data }) => {
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;
}
}
});

Expand Down

0 comments on commit 2fe372e

Please sign in to comment.