Skip to content

Instantly share code, notes, and snippets.

@bencevans
Last active December 12, 2015 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencevans/4716219 to your computer and use it in GitHub Desktop.
Save bencevans/4716219 to your computer and use it in GitHub Desktop.
Barton Internet Guard

Barton Internet Guard (Obsolete)

The proxy has been updated to use a new login design however this tool has not been updated.

Barton Peveril's WiFi requires authentication... every 15 mins.

When working on projects that require an internet connection this can easily break them. This gist/script automates the login process and can be run from terminal/cron job.

Requirements

Nodejs (+ npm)

Installation

  1. Clone repo git clone https://gist.github.com/4716219.git wifiguard
  2. enter cloned dir cd wifiguard
  3. npm install request cheerio debug
  4. echo '{"username":"YOUR_NETWORK_USERNAME", "password":"YOUR_NETWORK_PASSWORD"}' > login.json
  5. node guard.js
/**
* Dependencies
*/
var http = require('http'),
fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
debug = require('debug')('guard');
var creds = JSON.parse(fs.readFileSync('./login.json'));
/**
* Check if logged into proxy
* @param {Function} callback with sigature (err, loggedIn), loggedIn = true else proxy URL
* @return {Void}
*/
function isLoggedIn(callback) {
http.get('http://www.bbc.co.uk', function(res) {
debug('Received Response');
debug('Location Header: %s', res.headers.location);
if(res.headers.location && res.headers.location.match(/^http:\/\/10\.2\.0\.101.+/))
return callback(null, res.headers.location);
return callback(null, true);
});
}
/**
* Authenticate Proxy
* @param {String} url provided location header url
* @param {String} username network username
* @param {String} password network password
* @param {Function} callback signature (err)
* @return {Void}
*/
function login(url, username, password, callback) {
request(url, function(err, res, body) {
var $ = cheerio.load(body);
var form = {};
var wanted_form = [
'ctl00$ContentPlaceHolder1$txtAuthMainUsername',
'ctl00$ContentPlaceHolder1$txtAuthMainPassword',
'ctl00$ContentPlaceHolder1$btnAuthMainSubmit',
'__VIEWSTATE',
'__EVENTTARGET',
'__EVENTARGUMENT',
'__EVENTVALIDATION'
];
$('input').each(function(num, input) {
if(wanted_form.indexOf($(input).attr('name')) !== -1)
form[$(input).attr('name')] = $(input).attr('value') || null;
});
form['ctl00$ContentPlaceHolder1$txtAuthMainUsername'] = username;
form['ctl00$ContentPlaceHolder1$txtAuthMainPassword'] = password;
var headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'en-GB,en-US;q=0.8,en;q=0.6',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie': res.headers['set-cookie'][0].split(' ')[0],
'Host':'10.2.0.101',
'Origin':'http://10.2.0.101',
'Referer':url,
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17'
};
debug('Cookie: %s', res.headers['set-cookie'][0].split(' ')[0]);
request.post(url, {
form: form,
headers:headers
}, function(err, res) {
callback(err);
});
});
}
isLoggedIn(function(err, loggedIn) {
if(err) throw err;
if(typeof loggedIn == 'string') {
debug('Logging in');
var url = loggedIn;
login(url, creds.username, creds.password, function(err) {
if(err) throw err;
isLoggedIn(function(err, loggedIn) {
if(err) throw err;
if(loggedIn === true)
console.log('Logged In');
else
console.log('Unknown Error Logging In.');
});
});
} else {
console.log('Already logged in.');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment