From acbd943da1579985dcd9fef05030bc486189c776 Mon Sep 17 00:00:00 2001 From: plumbeo Date: Fri, 2 Mar 2018 11:59:43 +0100 Subject: [PATCH] Captive portal: add a traffic quota option Add a new option to disconnect users after they exceed a traffic quota (sum of downloaded data and uploaded data). --- src/etc/inc/captiveportal.inc | 17 ++++++++++++++++- src/usr/local/www/services_captiveportal.php | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/etc/inc/captiveportal.inc b/src/etc/inc/captiveportal.inc index 5cf6eb87a5..b464c0a6d3 100644 --- a/src/etc/inc/captiveportal.inc +++ b/src/etc/inc/captiveportal.inc @@ -755,8 +755,14 @@ function captiveportal_prune_old() { $idletimeout = $cpcfg['idletimeout'] * 60; } + /* check for entries exceeding their traffic quota */ + $trafficquota = 0; + if (!empty($cpcfg['trafficquota']) && is_numeric($cpcfg['trafficquota'])) { + $trafficquota = $cpcfg['trafficquota'] * 1048576; + } + /* Is there any job to do? */ - if (!$timeout && !$idletimeout && !isset($cpcfg['reauthenticate']) && + if (!$timeout && !$idletimeout && !$trafficquota && !isset($cpcfg['reauthenticate']) && !isset($cpcfg['radiussession_timeout']) && !isset($vcpcfg['enable'])) { return; } @@ -830,6 +836,15 @@ function captiveportal_prune_old() { } } + /* traffic quota */ + if (!$timedout && $trafficquota > 0) { + $volume = getVolume($cpentry[2], $cpentry[3]); + if (($volume['input_bytes'] + $volume['output_bytes']) > $trafficquota) { + $timedout = true; + $term_cause = 10; // NAS-Request + } + } + if ($timedout) { captiveportal_disconnect($cpentry, $radiusservers, $term_cause, $stop_time); captiveportal_logportalauth($cpentry[4], $cpentry[3], $cpentry[2], "TIMEOUT"); diff --git a/src/usr/local/www/services_captiveportal.php b/src/usr/local/www/services_captiveportal.php index 237c8b4df9..93a7e33c93 100644 --- a/src/usr/local/www/services_captiveportal.php +++ b/src/usr/local/www/services_captiveportal.php @@ -145,6 +145,7 @@ if ($a_cp[$cpzone]) { $pconfig['maxprocperip'] = $a_cp[$cpzone]['maxprocperip']; $pconfig['timeout'] = $a_cp[$cpzone]['timeout']; $pconfig['idletimeout'] = $a_cp[$cpzone]['idletimeout']; + $pconfig['trafficquota'] = $a_cp[$cpzone]['trafficquota']; $pconfig['freelogins_count'] = $a_cp[$cpzone]['freelogins_count']; $pconfig['freelogins_resettimeout'] = $a_cp[$cpzone]['freelogins_resettimeout']; $pconfig['freelogins_updatetimeouts'] = isset($a_cp[$cpzone]['freelogins_updatetimeouts']); @@ -281,6 +282,10 @@ if ($_POST['save']) { $input_errors[] = gettext("The idle timeout must be at least 1 minute."); } + if ($_POST['trafficquota'] && (!is_numeric($_POST['trafficquota']) || ($_POST['trafficquota'] < 1))) { + $input_errors[] = gettext("The traffic quota must be at least 1 megabyte."); + } + if ($_POST['freelogins_count'] && (!is_numeric($_POST['freelogins_count']))) { $input_errors[] = gettext("The pass-through credit count must be a number or left blank."); } else if ($_POST['freelogins_count'] && is_numeric($_POST['freelogins_count']) && ($_POST['freelogins_count'] >= 1)) { @@ -353,6 +358,7 @@ if ($_POST['save']) { $newcp['maxprocperip'] = $_POST['maxprocperip'] ? $_POST['maxprocperip'] : false; $newcp['timeout'] = $_POST['timeout']; $newcp['idletimeout'] = $_POST['idletimeout']; + $newcp['trafficquota'] = $_POST['trafficquota']; $newcp['freelogins_count'] = $_POST['freelogins_count']; $newcp['freelogins_resettimeout'] = $_POST['freelogins_resettimeout']; $newcp['freelogins_updatetimeouts'] = $_POST['freelogins_updatetimeouts'] ? true : false; @@ -569,6 +575,14 @@ $section->addInput(new Form_Input( ))->setHelp('Clients will be disconnected after this amount of time, regardless of activity. They may log in again immediately, though. ' . 'Leave this field blank for no hard timeout (not recommended unless an idle timeout is set).'); +$section->addInput(new Form_Input( + 'trafficquota', + 'Traffic quota (Megabytes)', + 'number', + $pconfig['trafficquota'] +))->setHelp('Clients will be disconnected after exceeding this amount of traffic, inclusive of both downloads and uploads. They may log in again immediately, though. ' . + 'Leave this field blank for no traffic quota.'); + $section->addInput(new Form_Input( 'freelogins_count', 'Pass-through credits per MAC address.', @@ -1234,6 +1248,7 @@ events.push(function() { hideInput('maxprocperip', hide); hideInput('idletimeout', hide); hideInput('timeout', hide); + hideInput('trafficquota', hide); hideInput('freelogins_count', hide); hideInput('freelogins_resettimeout', hide); hideCheckbox('freelogins_updatetimeouts', hide);