Spaces:
Sleeping
Sleeping
Download server.js from Konstantin31bro/kos: direct link, hf CLI and curl.
- Browser
- Download file 5.02 kB
-
https://huggingface.co/spaces/Konstantin31bro/kos/resolve/main/server.js
- Command line
-
hf download hf://spaces/Konstantin31bro/kos/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Konstantin31bro/kos/resolve/main/server.js
5.02 kB
| /* | |
| Cipher — WebSocket relay server | |
| Не хранит и не может прочитать сообщения — только пересылает зашифрованные блобы. | |
| Deploy: Hugging Face (порт 7860) | |
| */ | |
| const http = require('http'); | |
| const { WebSocketServer } = require('ws'); | |
| const PORT = 7860; // фиксированный порт для Hugging Face | |
| // In-memory stores (сбрасываются при рестарте) | |
| const users = new Map(); // email → { id, name, email, saltHex, hash } | |
| const sockets = new Map(); // userId → ws | |
| const rooms = new Map(); // chatId → Set<userId> | |
| const server = http.createServer((req, res) => { | |
| // Health check + CORS preflight | |
| res.setHeader('Access-Control-Allow-Origin', '*'); | |
| res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS'); | |
| res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); | |
| if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; } | |
| // Health endpoint | |
| if (req.url === '/health') { res.writeHead(200); res.end('ok'); return; } | |
| // Get salt for email (GET /salt?email=...) | |
| if (req.url.startsWith('/salt?email=')) { | |
| const email = decodeURIComponent(req.url.split('=')[1]); | |
| const u = users.get(email); | |
| if (!u) { | |
| res.writeHead(404, { 'Content-Type': 'application/json' }); | |
| res.end(JSON.stringify({ error: 'user not found' })); | |
| return; | |
| } | |
| json(res, 200, { saltHex: u.saltHex }); | |
| return; | |
| } | |
| // REST: register / login / users | |
| if (req.method === 'POST') { | |
| let body = ''; | |
| req.on('data', d => body += d); | |
| req.on('end', () => { | |
| try { | |
| const data = JSON.parse(body); | |
| if (req.url === '/register') return handleRegister(res, data); | |
| if (req.url === '/login') return handleLogin(res, data); | |
| if (req.url === '/users') return handleUsers(res, data); | |
| } catch { } | |
| res.writeHead(400); res.end('bad request'); | |
| }); | |
| return; | |
| } | |
| res.writeHead(404); res.end(); | |
| }); | |
| function json(res, code, obj) { | |
| res.writeHead(code, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }); | |
| res.end(JSON.stringify(obj)); | |
| } | |
| function handleRegister(res, { id, name, email, saltHex, hash }) { | |
| if (!email || !hash) return json(res, 400, { error: 'missing fields' }); | |
| if (users.has(email)) return json(res, 409, { error: 'email taken' }); | |
| users.set(email, { id, name, email, saltHex, hash }); | |
| json(res, 200, { ok: true }); | |
| } | |
| function handleLogin(res, { email, hash }) { | |
| const u = users.get(email); | |
| if (!u) return json(res, 404, { error: 'not found' }); | |
| if (u.hash !== hash) return json(res, 401, { error: 'wrong password' }); | |
| json(res, 200, { user: { id: u.id, name: u.name, email: u.email, saltHex: u.saltHex } }); | |
| } | |
| function handleUsers(res, { query }) { | |
| const q = (query || '').toLowerCase(); | |
| const result = []; | |
| for (const u of users.values()) { | |
| if (u.email.includes(q)) result.push({ id: u.id, name: u.name, email: u.email }); | |
| } | |
| json(res, 200, { users: result }); | |
| } | |
| // WebSocket | |
| const wss = new WebSocketServer({ server }); | |
| wss.on('connection', (ws) => { | |
| let uid = null; | |
| ws.on('message', (raw) => { | |
| let msg; | |
| try { msg = JSON.parse(raw); } catch { return; } | |
| // Auth | |
| if (msg.t === 'auth') { | |
| uid = msg.uid; | |
| sockets.set(uid, ws); | |
| broadcast({ t: 'online', uid }, uid); | |
| // Send currently online users to newcomer | |
| const online = [...sockets.keys()]; | |
| ws.send(JSON.stringify({ t: 'online_list', uids: online })); | |
| return; | |
| } | |
| if (!uid) return; | |
| // Join chat room | |
| if (msg.t === 'join') { | |
| const { cid, members } = msg; | |
| if (!rooms.has(cid)) rooms.set(cid, new Set()); | |
| const room = rooms.get(cid); | |
| members.forEach(m => room.add(m)); | |
| return; | |
| } | |
| // Relay encrypted message to room members | |
| if (msg.t === 'msg') { | |
| const { cid } = msg.msg; | |
| const room = rooms.get(cid); | |
| if (!room) return; | |
| const out = JSON.stringify(msg); | |
| for (const memberId of room) { | |
| if (memberId === uid) continue; | |
| const sock = sockets.get(memberId); | |
| if (sock && sock.readyState === 1) sock.send(out); | |
| } | |
| return; | |
| } | |
| // Typing indicator | |
| if (msg.t === 'typing') { | |
| const room = rooms.get(msg.cid); | |
| if (!room) return; | |
| const out = JSON.stringify(msg); | |
| for (const memberId of room) { | |
| if (memberId === uid) continue; | |
| const sock = sockets.get(memberId); | |
| if (sock && sock.readyState === 1) sock.send(out); | |
| } | |
| return; | |
| } | |
| }); | |
| ws.on('close', () => { | |
| if (uid) { | |
| sockets.delete(uid); | |
| broadcast({ t: 'offline', uid }, uid); | |
| } | |
| }); | |
| }); | |
| function broadcast(msg, exceptUid) { | |
| const out = JSON.stringify(msg); | |
| for (const [id, sock] of sockets) { | |
| if (id === exceptUid) continue; | |
| if (sock.readyState === 1) sock.send(out); | |
| } | |
| } | |
| server.listen(PORT, () => console.log(`Cipher relay on :${PORT}`)); |