-
Notifications
You must be signed in to change notification settings - Fork 0
/
easylink.js
96 lines (89 loc) · 2.45 KB
/
easylink.js
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
const dgram = require("dgram");
const EASYLINK_PORT = 50000;
const makeInfo = (ssid, password) => {
const data = new Uint8Array(128);
// Packet size info
data.set([
3 + ssid.length + password.length + 5 + 2,
ssid.length,
password.length
]);
let offset = 3;
// SSID
data.set(Array.from(ssid, (c) => c.charCodeAt(0)), offset);
offset += ssid.length;
// PW
data.set(Array.from(password, (c) => c.charCodeAt(0)), offset);
offset += password.length;
// Is sendip - some magic happened that turned 1000 into this.
data.set([35, 0, 0, 3, 232], offset);
offset += 5;
// Calculate checksums
let copy = 0;
for(let i = 0; i < offset; ++i) {
copy = copy + (data[i] & 255);
}
data.set([(65535 & copy) >> 8, copy & 255], offset);
return data;
};
const sendEasyLinkPacket = (socket, size, smallMTU = false) => {
if(smallMTU) {
if(size > 1280) {
size -= 1280;
}
if(size < 64) {
size += 176;
}
}
const data = new Array(size);
data.fill(0);
return new Promise((resolve, reject) => {
socket.send(new Uint8Array(data), EASYLINK_PORT, "255.255.255.255", (err) => {
if(err) {
reject(err);
}
else {
resolve();
}
});
});
}
const wait = (time) => new Promise((resolve) => {
setTimeout(resolve, time);
});
const sendInfo = async (socket, info, sleep = 10) => {
for(let i = 1450; i <= 1452; ++i) {
await wait(sleep);
await sendEasyLinkPacket(socket, i);
}
let j = 1;
let k = 0;
for(let b = 0; b < info[0]; ++b) {
const len = (j * 256) + (info[b] & 255);
await wait(sleep);
await sendEasyLinkPacket(socket, len);
if(b % 4 == 3) {
++k;
const kinfo = k + 1280;
await wait(sleep);
await sendEasyLinkPacket(socket, kinfo);
}
++j;
if(j == 5) {
j = 1;
}
}
}
exports.sendWifiInfo = async (ssid, password, timeout = 10000) => {
const socket = dgram.createSocket({
type: 'udp4'
});
socket.on('error', console.error);
await new Promise((resolve) => socket.bind(resolve));
socket.setBroadcast(true);
const start = Date.now();
while(Date.now() - start < timeout) {
await sendInfo(socket, makeInfo(ssid, password));
}
socket.close();
};