-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·124 lines (109 loc) · 2.89 KB
/
app.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
var url = require('url'),
querystring = require('querystring'),
http = require('http'),
scraper = require('scraper'),
refreshinterval = 10; // in seconds
var hotspots = {
cartel: {
check: function ($) {
if ($("title").html().toLowerCase().indexOf("cartel") != -1) {
return true;
}
return false;
},
credentials: {
auth_user: "cartel",
auth_pass: "cartel",
redirurl: "http://google.com/",
accept: "Continue"
},
postOptions: {
path: "/"
}
}
};
function checkForRedirect () {
var checkOptions = {
host: "s3.amazonaws.com",
path: "/brianshaler/status.json",
method: "GET"
};
http.request(checkOptions, isThisARedirect).on('error', onError).end();
}
function isThisARedirect (res) {
if (res.statusCode == 301 || res.statusCode == 302) {
console.log("Redirect? HOW SUSPICIOUS.");
redirecting(res);
}
else {
console.log("No redirect, nothing to see here...");
}
}
function redirecting (res) {
var _url = url.parse(res.headers.location);
console.log("Scrape: "+res.headers.location);
scraper(res.headers.location, function(err, $) {
if (err) {throw err}
var found = false;
for (var k in hotspots) {
if (!found && hotspots[k].check($)) {
console.log(k.toUpperCase()+" WANTS YOU TO LOG IN");
connectToNetwork(hotspots[k].credentials, mergeObjects(_url, hotspots[k].postOptions));
return;
}
}
console.log("Unrecognized login form");
});
}
function connectToNetwork (loginFormData, loginFormOptions) {
loginFormData = querystring.stringify(loginFormData);
loginFormOptions.method = "POST",
loginFormOptions.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': loginFormData.length
};
// Set up the request
var post_req = http.request(loginFormOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log('Response: ' + chunk);
});
res.on('error', onError);
res.on('close', function () {
console.log("Logged in!");
});
});
post_req.on('error', onError);
// post the data
post_req.write(loginFormData);
post_req.end();
}
function onError (e) {
console.log(e);
}
function mergeObjects (obj1, obj2) {
obj = {};
for (k in obj1) {
obj[k] = obj1[k];
}
for (k in obj2) {
obj[k] = obj2[k];
}
return obj;
}
// process command line arguments
process.argv.forEach(function (val, index, array) {
if(val == "-h" || val == "--help")
{
console.log("To change refresh interval: -t [number of seconds]\r\n\r\n");
}
if(val == "-t"){
if(process.argv[index+1] > 0) {
refreshinterval = process.argv[index+1];
}
}
}
);
console.log("Cool! I'll check the wifi every "+refreshinterval+" seconds, just keep me running...");
setInterval(checkForRedirect, refreshinterval*1000);
checkForRedirect();