/* * HTTP tunnel over WebRTC - P2P class * Author: Konstantinos Drakontidis * Email: gedra100sh@gmail.com */ import { v4 } from "uuid"; import { WebSocket } from "ws"; import config from "../config/config" export class P2P { private server_instance: WebSocket | null; private clients: Map; public constructor() { this.server_instance = null; this.clients = new Map(); } setServer(server_instance: WebSocket): void { this.server_instance = server_instance; } getServer(): WebSocket | null { return this.server_instance; } addClient(client: WebSocket): string { const id = v4(); this.clients.set(id, { instance: client, timeout: null }); return id; } getClient(id: string, client: WebSocket): WebSocket | null { const client_instance = this.clients.get(id)?.instance; if (client_instance == client) { return client_instance; } return null; } getPeer(id: string, client: WebSocket): WebSocket | null { if (client == this.server_instance) { const client_instance = this.clients.get(id)?.instance; return (client_instance != undefined) ? client_instance : null; } else { return this.server_instance; } } removeClient(id: string) { return this.clients.delete(id); } getId(client: WebSocket): string | null { var key = null; this.clients.forEach((v, k) => { if (v.instance == client) { key = k; return; } }); return key; } clear() { this.clients.forEach((v) => { v.instance.close(3001, "Server shutdown."); }) this.server_instance = null; this.clients.clear(); } setClientTimeout(id: string, p2p: P2P, ws_client: WebSocket, callback: (id: string, p2p: P2P, ws_client: WebSocket) => void) { const instance = this.clients.get(id); if (instance) { this.clients.set(id, { ...instance, timeout: setTimeout(() => { callback(id, p2p, ws_client) }, config.timeout) }); } } clearClientTimeout(id: string) { const timeout = this.clients.get(id)?.timeout; if (timeout && (timeout != null)) { clearTimeout(timeout); } } getMapSize(): number { return this.clients.size; } };