-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery.js
More file actions
238 lines (196 loc) · 6.16 KB
/
discovery.js
File metadata and controls
238 lines (196 loc) · 6.16 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* discovery.js - LAN Device Discovery using UDP Broadcast
* Broadcasts presence and listens for other Fileway devices
*/
const dgram = require('dgram');
const os = require('os');
const EventEmitter = require('events');
const DISCOVERY_PORT = 41234;
const BROADCAST_INTERVAL = 3000; // 3 seconds
const DEVICE_TIMEOUT = 10000; // 10 seconds
class DeviceDiscovery extends EventEmitter {
constructor() {
super();
this.socket = null;
this.broadcastInterval = null;
this.devices = new Map(); // deviceId -> deviceInfo
this.myDeviceId = null;
this.myDeviceName = null;
this.myEmail = null;
this.isRunning = false;
}
/**
* Start discovery service
*/
start(deviceId, deviceName, email) {
if (this.isRunning) return;
this.myDeviceId = deviceId;
this.myDeviceName = deviceName;
this.myEmail = email;
this.socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
this.socket.on('error', (err) => {
console.error('Discovery socket error:', err);
this.socket.close();
});
this.socket.on('message', (msg, rinfo) => {
this.handleMessage(msg, rinfo);
});
this.socket.on('listening', () => {
this.socket.setBroadcast(true);
console.log('Discovery service listening on port', DISCOVERY_PORT);
this.startBroadcasting();
});
this.socket.bind(DISCOVERY_PORT);
this.isRunning = true;
// Start cleanup interval
this.cleanupInterval = setInterval(() => {
this.cleanupStaleDevices();
}, 5000);
}
/**
* Stop discovery service
*/
stop() {
if (!this.isRunning) return;
if (this.broadcastInterval) {
clearInterval(this.broadcastInterval);
this.broadcastInterval = null;
}
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
}
if (this.socket) {
this.socket.close();
this.socket = null;
}
this.devices.clear();
this.isRunning = false;
}
/**
* Update email (after login)
*/
updateEmail(email) {
this.myEmail = email;
}
/**
* Update device name
*/
updateDeviceName(name) {
this.myDeviceName = name;
}
/**
* Start broadcasting presence
*/
startBroadcasting() {
const broadcast = () => {
if (!this.socket || !this.myEmail) return;
const message = JSON.stringify({
type: 'presence',
deviceId: this.myDeviceId,
deviceName: this.myDeviceName,
email: this.myEmail,
timestamp: Date.now()
});
const broadcastAddresses = this.getBroadcastAddresses();
broadcastAddresses.forEach(addr => {
this.socket.send(message, DISCOVERY_PORT, addr, (err) => {
if (err) console.error('Broadcast error:', err);
});
});
};
// Broadcast immediately
broadcast();
// Then broadcast periodically
this.broadcastInterval = setInterval(broadcast, BROADCAST_INTERVAL);
}
/**
* Get all broadcast addresses for local networks
*/
getBroadcastAddresses() {
const addresses = [];
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
// Skip internal and non-IPv4 addresses
if (iface.internal || iface.family !== 'IPv4') continue;
// Calculate broadcast address
const ip = iface.address.split('.').map(Number);
const mask = iface.netmask.split('.').map(Number);
const broadcast = ip.map((octet, i) => (octet | (~mask[i] & 255)));
addresses.push(broadcast.join('.'));
}
}
// Fallback to default broadcast
if (addresses.length === 0) {
addresses.push('255.255.255.255');
}
return addresses;
}
/**
* Handle incoming discovery message
*/
handleMessage(msg, rinfo) {
try {
const data = JSON.parse(msg.toString());
if (data.type !== 'presence') return;
if (data.deviceId === this.myDeviceId) return; // Ignore own messages
const deviceInfo = {
deviceId: data.deviceId,
deviceName: data.deviceName,
email: data.email,
ip: rinfo.address,
lastSeen: Date.now()
};
const isNew = !this.devices.has(data.deviceId);
this.devices.set(data.deviceId, deviceInfo);
if (isNew) {
this.emit('deviceFound', deviceInfo);
}
this.emit('devicesUpdated', this.getDeviceList());
} catch (err) {
// Ignore malformed messages
}
}
/**
* Remove devices that haven't been seen recently
*/
cleanupStaleDevices() {
const now = Date.now();
let changed = false;
for (const [deviceId, info] of this.devices) {
if (now - info.lastSeen > DEVICE_TIMEOUT) {
this.devices.delete(deviceId);
this.emit('deviceLost', info);
changed = true;
}
}
if (changed) {
this.emit('devicesUpdated', this.getDeviceList());
}
}
/**
* Get list of all discovered devices
*/
getDeviceList() {
return Array.from(this.devices.values());
}
/**
* Find device by email
*/
findDeviceByEmail(email) {
for (const device of this.devices.values()) {
if (device.email === email) {
return device;
}
}
return null;
}
/**
* Find device by ID
*/
findDeviceById(deviceId) {
return this.devices.get(deviceId) || null;
}
}
module.exports = DeviceDiscovery;