Skip to content

Instantly share code, notes, and snippets.

@Equinoxs
Created March 9, 2025 17:41
Show Gist options
  • Save Equinoxs/f32e2d32a1cdbaf0b1907fbed1e6cc93 to your computer and use it in GitHub Desktop.
Save Equinoxs/f32e2d32a1cdbaf0b1907fbed1e6cc93 to your computer and use it in GitHub Desktop.
Freebox OS wake on lan script
let fbx_ip = "192.168.1.254";
let pass = "XXXXXXXXXXXX";
let mac = "00:11:22:33:44:55";
let crypto = require("crypto");
function getchallenge(challengeTable) {
var s = "", i;
for (i in challengeTable) {
s += "" + eval(challengeTable[i]);
}
return s;
}
function sha1(data) {
var sum = crypto.createHash("sha1");
sum.update(data);
return sum.digest("hex");
}
function hmacsha1(data, key) {
var hmac = crypto.createHmac("sha1", key);
hmac.update(data);
return hmac.digest("hex");
}
async function login() {
let response = await fetch("http://" + fbx_ip + "/api/latest/login/?_=" + Date.now().toString(), {
method: "GET",
headers: {
"X-FBX-FREEBOX0S": "1"
}
});
let json = await response.json();
if (json.logged_in) return;
challenge = getchallenge(json.result.challenge);
password_salt = json.result.password_salt;
let h_pass = sha1(password_salt + pass);
let hash = hmacsha1(challenge, h_pass.toString());
response = await fetch("http://" + fbx_ip + "/api/latest/login/", {
method: "POST",
headers: {
"X-FBX-FREEBOX0S": "1",
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams({
"password": hash
})
});
json = await response.json();
let cookie = response.headers.getSetCookie()[0];
if (!json.success) return false;
response = await fetch("http://" + fbx_ip + "/api/latest/login/", {
method: "GET",
headers: {
"X-FBX-FREEBOX0S": "1",
cookie: cookie
}
});
json = await response.json();
if (json.result.logged_in) return cookie;
return false;
}
async function wake() {
let cookie = await login();
const response = await fetch("http://" + fbx_ip + "/api/latest/lan/wol/pub/", {
"headers": {
cookie: cookie,
"X-Fbx-App-Id": "fr.freebox.mafreebox",
"X-Fbx-Freebox0S": "1",
"Content-Type": "application/json",
},
"body": JSON.stringify({mac: mac, password: ""}),
"method": "POST",
})
let json = await response.json();
console.log(`Machine ${ mac } ${ json.success ? "has been woken up" : "is still asleep"}.`);
}
wake();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment