Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
171 lines (152 loc) · 4.92 KB
/
Copy pathserver.js
File metadata and controls
171 lines (152 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* LanBeam signaling server.
*
* This server ONLY relays WebRTC signaling (SDP offers/answers and ICE
* candidates) so two devices can find each other. File bytes travel
* directly between devices over an encrypted WebRTC DataChannel and
* NEVER touch this server. Nothing is stored, logged, or uploaded.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { WebSocketServer } = require('ws');
const PORT = process.env.PORT || 3000;
const PUBLIC_DIR = path.join(__dirname, 'public');
const ROOM_TTL_MS = 30 * 60 * 1000; // rooms die 30 min after creation
const MIME = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
};
// code -> { sender, receiver, createdAt }
const rooms = new Map();
function makeCode() {
// unambiguous characters only (no 0/O, 1/I/l)
const alphabet = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789';
let code = '';
do {
code = Array.from(crypto.randomBytes(6))
.map((b) => alphabet[b % alphabet.length])
.join('');
} while (rooms.has(code));
return code;
}
function peerOf(room, ws) {
if (!room) return null;
return ws === room.sender ? room.receiver : room.sender;
}
function destroyRoom(code) {
const room = rooms.get(code);
if (!room) return;
for (const peer of [room.sender, room.receiver]) {
if (peer && peer.readyState === 1) {
peer.send(JSON.stringify({ type: 'peer-left' }));
}
}
rooms.delete(code);
}
// ---- static file server ----
const server = http.createServer((req, res) => {
let urlPath = decodeURIComponent(req.url.split('?')[0]);
if (urlPath === '/') urlPath = '/index.html';
const filePath = path.normalize(path.join(PUBLIC_DIR, urlPath));
// prevent path traversal
if (!filePath.startsWith(PUBLIC_DIR) || !fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
return;
}
const ext = path.extname(filePath).toLowerCase();
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
fs.createReadStream(filePath).pipe(res);
});
// ---- signaling ----
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
ws.roomCode = null;
ws.role = null;
ws.on('message', (raw) => {
let msg;
try {
msg = JSON.parse(raw.toString());
} catch {
return;
}
if (msg.type === 'create') {
const code = makeCode();
rooms.set(code, { sender: ws, receiver: null, createdAt: Date.now() });
ws.roomCode = code;
ws.role = 'sender';
ws.send(JSON.stringify({ type: 'created', code }));
return;
}
if (msg.type === 'join') {
const code = String(msg.code || '').toUpperCase().trim();
const room = rooms.get(code);
if (!room) {
ws.send(JSON.stringify({ type: 'error', message: 'Room not found. Check the code and try again.' }));
return;
}
if (room.receiver) {
ws.send(JSON.stringify({ type: 'error', message: 'This room already has a receiver.' }));
return;
}
room.receiver = ws;
ws.roomCode = code;
ws.role = 'receiver';
ws.send(JSON.stringify({ type: 'joined', code }));
if (room.sender && room.sender.readyState === 1) {
room.sender.send(JSON.stringify({ type: 'peer-joined', role: 'receiver' }));
}
return;
}
if (msg.type === 'signal') {
const room = rooms.get(ws.roomCode);
const peer = peerOf(room, ws);
if (peer && peer.readyState === 1) {
peer.send(JSON.stringify({ type: 'signal', payload: msg.payload }));
}
return;
}
});
ws.on('close', () => {
if (!ws.roomCode) return;
const room = rooms.get(ws.roomCode);
if (!room) return;
const peer = peerOf(room, ws);
if (peer && peer.readyState === 1) {
peer.send(JSON.stringify({ type: 'peer-left' }));
peer.close();
}
rooms.delete(ws.roomCode);
});
});
// sweep stale rooms
setInterval(() => {
const now = Date.now();
for (const [code, room] of rooms) {
if (now - room.createdAt > ROOM_TTL_MS) destroyRoom(code);
}
}, 60 * 1000);
function lanIPs() {
const { networkInterfaces } = require('os');
const ips = [];
for (const ifaces of Object.values(networkInterfaces())) {
for (const i of ifaces || []) {
if (i.family === 'IPv4' && !i.internal) ips.push(i.address);
}
}
return ips;
}
server.listen(PORT, () => {
console.log('\n ⚡ LanBeam is live\n');
console.log(` Local: http://localhost:${PORT}`);
for (const ip of lanIPs()) console.log(` Network: http://${ip}:${PORT}`);
console.log('\n Files move device-to-device over encrypted WebRTC.');
console.log(' This server only relays signaling — it never sees your files.\n');
});