-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
125 lines (112 loc) · 2.98 KB
/
main.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
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
const http = require("http");
const md5 = require("./lib/md5");
const wifiName = require("./lib/wifi-name");
const utils = require("./lib/utils");
const os = require("os");
function getHashCode(response, password) {
let hash =
/document\.sendin\.password\.value = hexMD5\('(.{4})' \+ document\.login\.password\.value \+ '(.{64})'\);/g.exec(
response,
);
if (!hash && !hash.length) {
utils.die(
"Could not find hash in response. Are you already logged in or trying to log into another website?",
);
}
return md5.hexMD5(eval(`"${hash[1]}"`) + password + eval(`"${hash[2]}"`));
}
function displayResponse(response) {
if (response.includes("You are logged in")) {
console.log("Successfully logged in!");
} else {
utils.die(`Could not log in! See below for the response.\n${response}`);
}
}
function login(config, response) {
const password = getHashCode(response, config.password);
const postData = `username=${config.username}&password=${password}`;
const options = {
hostname: config.hostname,
port: 80,
path: "/login",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(postData),
},
};
const request = http.request(options, (res) => {
if (res.statusCode !== 200) {
utils.die(
`POST to ${config.hostname} returned status code ${res.statusCode}.`,
);
}
let result = "";
res.on("data", (data) => {
result += data.toString();
});
res.on("end", () => {
displayResponse(result);
});
});
request.on("error", (err) => {
utils.die(err.message);
});
request.write(postData);
request.end();
}
function initLogin(config) {
console.log(`SSID: ${config.ssid}`);
console.log(
`Logging into ${config.hostname} with username ${config.username}...`,
);
const request = http.get(`http://${config.hostname}/login`, (res) => {
if (res.statusCode !== 200) {
utils.die(
`GET to ${config.hostname} returned status code ${res.statusCode}.`,
);
}
let result = "";
res.on("data", (data) => {
result += data.toString();
});
res.on("end", () => {
login(config, result);
});
});
request.on("error", (err) => {
utils.die(err.message);
});
request.end();
}
function main() {
let configs;
try {
configs = require(
`${os.homedir()}/.config/mikrotik-captive-autologin/config.json`,
);
} catch (err) {
utils.die(err.message);
}
if (configs.length < 1) {
utils.die("No configuration found so Wi-Fi was not logged in.");
}
for (idx in configs) {
let config = configs[idx];
if (
!config.ssid ||
!config.hostname ||
!config.username ||
!config.password
) {
utils.die(
"Configuration is not valid or one of the arguments is not supplied.",
);
}
if (config.ssid === wifiName.sync()) {
initLogin(config);
break;
}
}
}
module.exports = { main };