Last active
February 1, 2017 13:22
-
-
Save shunghsiyu/e18b84524fd5a2ce78c3ab4f25e465aa to your computer and use it in GitHub Desktop.
This is a script to be used together with Phantomjs to automatically login to CHT wifi access-points.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = 'http://captive.apple.com/'; | |
var fs = require('fs'); | |
var file = fs.open('credential', 'r'); | |
var credentials = file.read().split('\n'); | |
file.close(); | |
// How often (in milliseconds) the program should check the login status. | |
var checkInterval = 5000; | |
// How long (in millisecond) should be program wait after the submit button | |
// on the login form is pressed before closing the page object. | |
var waitTimeout = 5000; | |
// Config object with the credentials that is necessary to login to the | |
// wifi acess-point. | |
var loginConfig = { | |
loginType: 2, | |
username: credentials[0], | |
passwd: credentials[1] | |
} | |
// Regular expressions used to check what webpage is currently displayed in the | |
// page object. | |
var loginUrl = new RegExp('https:\\/\\/authweb.hinet.net\\/auth\\/auth_login'); | |
var successUrl = new RegExp('http:\\/\\/authweb.hinet.net\\/auth\\/auth_success'); | |
function fillCredential (page) { | |
page.evaluate(function (config) { | |
seltype(config.loginType); | |
var optionIndex = config.loginType - 1; | |
var inputUsername = document.getElementsByName('cht_user')[optionIndex]; | |
var inputPasswd = document.getElementsByName('passwd')[optionIndex]; | |
inputUsername.value = config.username; | |
inputPasswd.value = config.passwd; | |
document.getElementById('submit').click(); | |
}, loginConfig); | |
console.log('Submit button clicked...'); | |
}; | |
function checkStatus (status) { | |
// Unless there is a problem with the Wifi connection, the status should | |
// alway be 'success'. | |
if (status !== 'success') { | |
console.log('Something is wrong with the wifi connection!'); | |
} | |
}; | |
function login() { | |
var page = require('webpage').create(); | |
var timerId = 0; | |
// Since the browser is redirected to the login page, using callback from | |
// `page.on` won't work, as the callback will be fired before arriving at | |
// the page with the login form. | |
// We need to rely on `page.onLoadFinished` to check the page's url and | |
// determine what to do next. | |
page.onLoadFinished = function() { | |
// If the current page is the one with login form, then fill in the | |
// credentials and click the submit button. | |
if (page.url.match(loginUrl)) { | |
console.log('Filling the credentials...'); | |
fillCredential(page); | |
// Close the page object after the `waitTimeout` millisecond. | |
// This servers two purpose, first it keeps a reference to the | |
// page object so it won't be GCed (which is the most import | |
// reason), second it make sure the page object is closed to | |
// release the memory. | |
timerId = setTimeout(function() { | |
page.close(); | |
}, waitTimeout); | |
// If the current page is one of the successful page (meaning login | |
// has succeed), print a success message and close the page object. | |
} else if (page.url.match(successUrl) || page.url === url) { | |
clearTimeout(timerId); | |
console.log('Login Success, now at ' + page.url); | |
page.close(); | |
} else { | |
console.log('Currently at ' + page.url); | |
} | |
} | |
page.open(url, checkStatus); | |
} | |
// Check the login status at an interval, and login if necessary. | |
setInterval(login, checkInterval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment