|
/** |
|
* 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.'); |
|
} |
|
|
|
}); |