Skip to content

Instantly share code, notes, and snippets.

@lucaelin
Last active July 6, 2019 13:13
Show Gist options
  • Save lucaelin/a08733df67f7a8098913458005c38c2b to your computer and use it in GitHub Desktop.
Save lucaelin/a08733df67f7a8098913458005c38c2b to your computer and use it in GitHub Desktop.
mikrotik captive portal - timeslot prolongation
/*
* The mikrotik captive portal has a bug, causing login-durations of less than a given amount of time
* to not be subtracted from the total time you are allowed to stay online.
* The threshold at which time is actually counted seems to vary.
* This script automagically logs you out and back in in a given interval, allowing you to keep surfing indefinitely
*/
const portalURL = `http://172.31.0.1/`;
const username = `MyUsername`;
const password = `MyPassword`;
const interval = 20 * 1000;
const fetch = require('node-fetch');
const eventuallyMD5js = fetch(portalURL + 'md5.js').then(r=>r.text());
async function update() {
const logout = await fetch(portalURL + 'logout');
const login = await fetch(portalURL + 'login').then(res=>res.text());
const challengeRegex = /hexMD5\(\'(.*)\'\)/;
const match = challengeRegex.exec(login)[0];
const challenge = match.replace(`' + document.login.password.value + '`, password);
const code = new Function(await eventuallyMD5js + '\nreturn ' + challenge);
const response = code();
const loggedin = await fetch(portalURL + 'login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `username=${username}&`
+ `password=${response}&`
+ `dst=&`
+ `popup=true`
}).then(res=>res.text());
if (loggedin.includes('You are logged in')) {
console.log('login successful');
} else {
console.error('an error occured', loggedin);
}
}
async function logout() {
console.log("Logging out...");
await fetch(portalURL + 'logout').catch(()=>console.log('logout failed!'));
}
async function backoff(e) {
console.error(e);
setTimeout(()=>update().catch(backoff),10000);
}
setInterval(()=>update().catch(backoff), interval);
update();
process.on('SIGINT', async () => {
await logout();
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment