From b991dd8ab261bd48915e7eaf6c1eac6cdaa50fc3 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 7 Jan 2016 12:57:36 +0100 Subject: [PATCH] (legacy) refactor status_graph.php, consolidate ifstats.php and bandwidth_by_ip.php into legacy_traffic_stats.php, leave graph.php as it is for now (although replacing should be a good idea) --- src/www/bandwidth_by_ip.php | 143 --------- src/www/graph.php | 2 +- src/www/ifstats.php | 51 ---- src/www/legacy_traffic_stats.php | 94 ++++++ src/www/status_graph.php | 510 +++++++++++-------------------- 5 files changed, 273 insertions(+), 527 deletions(-) delete mode 100644 src/www/bandwidth_by_ip.php delete mode 100644 src/www/ifstats.php create mode 100644 src/www/legacy_traffic_stats.php diff --git a/src/www/bandwidth_by_ip.php b/src/www/bandwidth_by_ip.php deleted file mode 100644 index 2f67be3b3..000000000 --- a/src/www/bandwidth_by_ip.php +++ /dev/null @@ -1,143 +0,0 @@ - diff --git a/src/www/graph.php b/src/www/graph.php index a3deb4071..200e6330b 100644 --- a/src/www/graph.php +++ b/src/www/graph.php @@ -81,7 +81,7 @@ $error_text = sprintf(gettext('Cannot get data about interface %s'), htmlspecial $height=100; //SVG internal height : do not modify $width=200; //SVG internal width : do not modify -$fetch_link = "ifstats.php?if=" . htmlspecialchars($ifnum); +$fetch_link = "legacy_traffic_stats.php?if=" . htmlspecialchars($ifnum); /********* Graph DATA **************/ print('' . "\n");?> diff --git a/src/www/ifstats.php b/src/www/ifstats.php deleted file mode 100644 index 516074eaf..000000000 --- a/src/www/ifstats.php +++ /dev/null @@ -1,51 +0,0 @@ - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -require_once('guiconfig.inc'); -require_once("interfaces.inc"); - -$if = $_GET['if']; - -$realif = get_real_interface($if); -if (!$realif) { - $realif = $if; // Need for IPSec case interface. -} - -$ifinfo = legacy_interface_stats($realif); - -$temp = gettimeofday(); -$timing = (double)$temp["sec"] + (double)$temp["usec"] / 1000000.0; - -header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" ); -header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT" ); -header("Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1 -header("Cache-Control: post-check=0, pre-check=0", FALSE ); -header("Pragma: no-cache"); // HTTP/1.0 - -echo "$timing|" . $ifinfo['bytes received'] . "|" . $ifinfo['bytes transmitted'] . "\n"; diff --git a/src/www/legacy_traffic_stats.php b/src/www/legacy_traffic_stats.php new file mode 100644 index 000000000..d11e4cb3a --- /dev/null +++ b/src/www/legacy_traffic_stats.php @@ -0,0 +1,94 @@ + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ +$omit_nocacheheaders = true; +require_once('guiconfig.inc'); +require_once('interfaces.inc'); +require_once('pfsense-utils.inc'); + +//get interface IP and break up into an array +$real_interface = get_real_interface($_GET['if']); +if (!does_interface_exist($real_interface)) { + echo gettext("Wrong Interface"); + exit; +} elseif (!empty($_GET['act']) && $_GET['act'] == "top") { + // + // find top bandwitdh users + // (parts copied from bandwidth_by_ip.php) + // + + //get interface subnet + $netmask = find_interface_subnet($real_interface); + $intsubnet = gen_subnet(find_interface_ip($real_interface), $netmask) . "/$netmask"; + $cmd_args = ""; + switch (!empty($_GET['filter']) ? $_GET['filter'] : "") { + case "local": + $cmd_args .= " -c " . $intsubnet . " "; + break; + case "remote": + default: + $cmd_args .= " -lc 0.0.0.0/0 "; + break; + } + if (!empty($_GET['sort']) && $_GET['sort'] == "out") { + $cmd_args .= " -T "; + } else { + $cmd_args .= " -R "; + } + + $listedIPs = array(); + $cmd_action = "/usr/local/bin/rate -i {$real_interface} -nlq 1 -Aba 20 {$cmd_args} | tr \"|\" \" \" | awk '{ printf \"%s:%s:%s:%s:%s\\n\", $1, $2, $4, $6, $8 }'"; + exec($cmd_action, $listedIPs); + + $result = array(); + for ($idx = 2 ; $idx < count($listedIPs) ; ++$idx) { + $fields = explode(':', $listedIPs[$idx]); + if (!empty($_GET['hostipformat'])) { + $addrdata = gethostbyaddr($fields[0]); + if ($_GET['hostipformat'] == 'hostname' && $addrdata != $fields[0]){ + $addrdata = explode(".", $fields)[0]; + } + } else { + $addrdata = $fields[0]; + } + $result[] = array('host' => $addrdata, 'in' => $fields[1], 'out' => $fields[2]); + } + // return json traffic + echo json_encode($result); +} else { + // collect interface statistics + // original source ifstats.php + $ifinfo = legacy_interface_stats($real_interface); + $temp = gettimeofday(); + $timing = (double)$temp["sec"] + (double)$temp["usec"] / 1000000.0; + echo "$timing|" . $ifinfo['bytes received'] . "|" . $ifinfo['bytes transmitted'] . "\n"; +} + + +exit; diff --git a/src/www/status_graph.php b/src/www/status_graph.php index 7c1c9d4b7..f1963daf7 100644 --- a/src/www/status_graph.php +++ b/src/www/status_graph.php @@ -1,364 +1,210 @@ . - All rights reserved. + Copyright (C) 2014-2016 Deciso B.V. + Copyright (C) 2004 Scott Ullrich + Copyright (C) 2003-2004 Manuel Kasper . + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ require_once("guiconfig.inc"); -if ($_POST['width']) - $width = $_POST['width']; -else - $width = "100%"; - -if ($_POST['height']) - $height = $_POST['height']; -else - $height = "200"; - // Get configured interface list $ifdescrs = get_configured_interface_with_descr(); -if (isset($config['ipsec']['enable'])) - $ifdescrs['enc0'] = "IPsec"; +if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable'])) { + $ifdescrs['enc0'] = "IPsec"; +} foreach (array('server', 'client') as $mode) { - if (isset($config['openvpn']["openvpn-{$mode}"])) { - foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) { - if (!isset($setting['disable'])) { - $ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']); - } - } - } + if (isset($config['openvpn']["openvpn-{$mode}"])) { + foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) { + if (!isset($setting['disable'])) { + $ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']); + } + } + } } -if ($_GET['if']) { - $curif = $_GET['if']; - $found = false; - foreach($ifdescrs as $descr => $ifdescr) { - if ($descr == $curif) { - $found = true; - break; - } - } - if ($found === false) { - header("Location: status_graph.php"); - exit; - } -} else { - if (empty($ifdescrs["wan"])) { - /* Handle the case when WAN has been disabled. Use the first key in ifdescrs. */ - reset($ifdescrs); - $curif = key($ifdescrs); - } - else { - $curif = "wan"; - } -} -if ($_GET['sort']) { - $cursort = $_GET['sort']; -} else { - $cursort = ""; -} -if ($_GET['filter']) { - $curfilter = $_GET['filter']; -} else { - $curfilter = ""; -} -if ($_GET['hostipformat']) { - $curhostipformat = $_GET['hostipformat']; -} else { - $curhostipformat = ""; + +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + // load initial form data + $pconfig = array(); + $pconfig['if'] = array_keys($ifdescrs)[0]; + foreach($ifdescrs as $descr => $ifdescr) { + if ($descr == $_GET['if']) { + $pconfig['if'] = $descr; + break; + } + } + $pconfig['sort'] = !empty($_GET['sort']) ? $_GET['sort'] : ""; + $pconfig['filter'] = !empty($_GET['filter']) ? $_GET['filter'] : ""; + $pconfig['hostipformat'] = !empty($_GET['hostipformat']) ? $_GET['hostipformat'] : ""; +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + header("Location: status_graph.php"); + exit; } +legacy_html_escape_form_data($pconfig); include("head.inc"); - ?> - - - -
-
-
- - 0) print_input_errors($input_errors); ?> - -
- -
- -
-
- - - - +
+
+
+
+
+ +
+
+ + + - - - - - - - - - - -
-
-
+ + + + + + + + + + + + + + + + + + + +
+ +
+
+
+
+
+
+
+ + + + + + + +
+
+
+
+ + + + + + + + + + +
+
+
+
+

',''); ?>

+
+
+
+ + + - - - -
- -
- - -
-
- - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
-
-

',''); ?>

-
-
-
- - - - -