captive.whump.shanti-portal/static/js/captiveportal.js

174 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-04-18 19:34:16 +00:00
// Captive portal Javascript
2016-12-14 15:37:32 +00:00
// by Stefan Midjich @ Cygate AB
2016-04-18 19:34:16 +00:00
//
2016-04-18 20:57:49 +00:00
var debug = true;
2016-12-14 15:37:32 +00:00
function getUrlParameter(sParam, default_value) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
2016-12-14 15:37:32 +00:00
return default_value;
}
2016-04-18 19:34:16 +00:00
// This function ensures the user gets redirect to the correct destination once
// all jobs have succeeded in the portal software.
function do_success() {
2016-12-14 15:37:32 +00:00
var url = getUrlParameter('url', 'www.google.com');
2016-12-13 13:52:11 +00:00
// If url does not start with http the window.location redirect
// won't work. So prefix http to url.
if (!url.startsWith('http')) {
url = 'http://' + url;
2016-12-13 13:52:11 +00:00
}
2017-11-15 22:03:45 +00:00
//console.log('success: ' + url);
$('#error-box').html('<p>If you\'re not automatically redirected <a href="https://www.google.com/">click here</a>.</p>');
2016-12-14 15:37:32 +00:00
$('#error-box').show();
$('#statusDiv').html('');
$('#approveButton').prop('disabled', false);
2016-12-14 15:37:32 +00:00
// Redirect user to the url paramter.
2016-12-13 13:12:59 +00:00
window.location = url;
2016-04-18 19:34:16 +00:00
}
2016-12-14 15:37:32 +00:00
2016-04-18 19:34:16 +00:00
// Show an error to the user
function do_error(message) {
$('#approveButton').prop('disabled', false);
2016-12-14 15:37:32 +00:00
$('#statusDiv').html('');
$('#error-box').show();
2016-12-14 15:37:32 +00:00
$('#error-box').html('<p>Failed. Reload page and try again or contact support.</p> ');
2016-04-18 19:34:16 +00:00
if (message) {
2017-11-15 22:03:45 +00:00
//console.log(message);
2016-04-18 19:34:16 +00:00
}
}
2016-12-14 15:37:32 +00:00
2016-04-18 19:34:16 +00:00
// Poll the returned jobs and ensure they all succeed
function poll_jobs(data) {
var promises = [];
if (debug) {
2017-11-15 22:03:45 +00:00
//console.log('Jobs data: ', data);
2016-04-18 20:57:49 +00:00
}
2017-11-15 22:03:45 +00:00
// Push promises into array, one for each job returned from POST /approve
for (var job in data) {
2016-04-18 19:34:16 +00:00
var job_id = data[job].id;
var api_url = '/job/' + job_id;
2016-04-18 19:34:16 +00:00
2016-04-18 20:57:49 +00:00
if (debug) {
2017-11-15 22:03:45 +00:00
//console.log('Processing job: ', data[job]);
2016-04-18 20:57:49 +00:00
}
2017-11-15 22:03:45 +00:00
// Each promise will poll the job ID for status
2016-04-18 19:34:16 +00:00
promises.push(new Promise(function(resolve, reject) {
2017-11-15 22:03:45 +00:00
var plugin_timeout = 30;
var maxRun = plugin_timeout / 2;
2016-04-18 19:34:16 +00:00
var timesRun = 0;
// Timer function that polls the API for job results
var pollJob = function() {
ajaxReq = $.get(api_url);
ajaxReq.done(function(getResponse) {
// Verify job data
var job_result = getResponse;
2016-04-18 20:57:49 +00:00
if (debug) {
2017-11-15 22:03:45 +00:00
//console.log(`Job result: ${job_result}`);
2016-04-18 20:57:49 +00:00
}
2017-11-15 22:03:45 +00:00
//console.log(job_result);
if (job_result.is_finished) {
2017-11-15 22:03:45 +00:00
console.log(`Resolved job: ${job_result.id}`);
2016-04-18 20:57:49 +00:00
resolve(job_result);
clearTimeout(timer);
return (true);
2016-04-18 19:34:16 +00:00
}
2016-04-20 16:26:40 +00:00
2017-11-15 22:03:45 +00:00
if (job_result.is_failed && job_result.meta.mandatory) {
console.log(`Job failed: ${job_result.id}`);
2016-04-20 16:26:40 +00:00
reject(job_result);
clearTimeout(timer);
return (false);
}
2017-11-15 22:03:45 +00:00
if (job_result.is_failed && !job_result.meta.mandatory) {
console.log(`Resolved non-mandatory failed job: ${job_result.id}`);
resolve(job_result);
clearTimeout(timer);
return (true);
}
2016-04-18 19:34:16 +00:00
});
ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) {
reject(XMLHttpRequest.responseText);
});
// Set timeout recursively until a certain threshold is reached
if (++timesRun == maxRun) {
clearTimeout(timer);
reject("Job polling timed out");
2016-04-18 20:57:49 +00:00
return;
2016-04-18 19:34:16 +00:00
} else {
timer = setTimeout(pollJob, 2000);
2016-04-18 19:34:16 +00:00
}
};
2017-11-15 22:03:45 +00:00
// Here I just want the pollJob function to run immediately
var timer = setTimeout(pollJob, 1000);
2016-04-18 19:34:16 +00:00
}));
}
// Run .all() on promises array until all promises resolve
2016-12-14 15:37:32 +00:00
// This is resolve() above.
2016-04-18 19:34:16 +00:00
Promise.all(promises).then(function(result) {
2016-04-18 20:57:49 +00:00
var success = true;
2017-11-15 22:03:45 +00:00
console.log(result);
2016-04-18 20:57:49 +00:00
2017-11-15 22:03:45 +00:00
if (success === true) {
// This timeout might be important if device wifi prompts seem to
// hang on the portal without redirecting on their own.
setTimeout(do_success, 2000);
2016-04-18 19:34:16 +00:00
}
2016-12-14 15:37:32 +00:00
// This is reject() above.
2016-04-18 19:34:16 +00:00
}, function(reason) {
do_error(reason);
});
}
2016-04-18 19:34:16 +00:00
// Submit the form
$('#approveForm').submit(function(event) {
var api_url = '/approve';
event.preventDefault();
2016-12-14 15:37:32 +00:00
$('#error-box').hide();
$('#approveButton').prop('disabled', true);
$('#statusDiv').html('<img src="/static/images/radio.svg" alt="Loading, please wait..." />');
if ($('#approveCheckbox').is(':checked')) {
var ajaxReq = $.post(api_url);
2016-04-18 19:34:16 +00:00
ajaxReq.done(poll_jobs);
ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) {
2016-04-18 20:57:49 +00:00
do_error(XMLHttpRequest.responseText);
});
}
2016-12-17 22:16:32 +00:00
2017-11-15 22:03:45 +00:00
});