diag_states_summary.php 5.61 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  Copyright (C) 2014 Deciso B.V.
  Copyright (C) 2010-2014 Jim Pingle
  Copyright (C) 2005-2009 Scott Ullrich
  Copyright (C) 2005 Colin Smith
  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.
Ad Schellevis's avatar
Ad Schellevis committed
29
*/
30
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
31 32

function addipinfo(&$iparr, $ip, $proto, $srcport, $dstport) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if (!isset($iparr[$ip]['seen'])) {
        $iparr[$ip] = array("seen" => 0, "protos" => array());
    }
    if (!isset($iparr[$ip]['protos'][$proto])) {
        $iparr[$ip]['protos'][$proto] = array("seen" => 0, 'srcports' => array(), 'dstports' => array());
    }
    $iparr[$ip]['seen']++;
    $iparr[$ip]['protos'][$proto]['seen']++;
    if (!empty($srcport)) {
        if (!isset($iparr[$ip]['protos'][$proto]['srcports'][$srcport])) {
            $iparr[$ip]['protos'][$proto]['srcports'][$srcport] = 0;
        }
        $iparr[$ip]['protos'][$proto]['srcports'][$srcport]++;
    }
    if (!empty($dstport)) {
        $iparr[$ip]['protos'][$proto]['dstports'][$dstport]++;
    }
Ad Schellevis's avatar
Ad Schellevis committed
50 51 52
}

function sort_by_ip($a, $b) {
53
    return ip2ulong($a) < ip2ulong($b) ? -1 : 1;
Ad Schellevis's avatar
Ad Schellevis committed
54 55 56
}

function build_port_info($portarr, $proto) {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    if (empty($portarr)) {
        return '';
    }
    $ports = array();
    asort($portarr);
    foreach (array_reverse($portarr, TRUE) as $port => $count) {
        $str = "";
        $service = getservbyport($port, strtolower($proto));
        $port = "{$proto}/{$port}";
        if (!empty($service)) {
            $port = "{$port} ({$service})";
        }
        $ports[] = "{$port}: {$count}";
    }
    return implode($ports, ', ');
}

$srcipinfo = array();
$dstipinfo = array();
$allipinfo = array();
$pairipinfo = array();

$states = json_decode(configd_run("filter list states json"), true);
if(isset($states['details'])) {
  foreach($states['details'] as $state) {
    if (isset($state['nat_addr']) && $states['direction'] == 'out') {
        $srcip = $state['nat_addr'] ;
        $srcport = $state['nat_port'] ;
    } else {
        $srcip = $state['src_addr'] ;
        $srcport = $state['src_port'] ;
    }
    $dstip = $state['dst_addr'] ;
    $dstport = $state['dst_port'] ;
    $proto = $state['proto'];

    addipinfo($srcipinfo, $srcip, $proto, $srcport, $dstport);
    addipinfo($dstipinfo, $dstip, $proto, $srcport, $dstport);
    addipinfo($pairipinfo, "{$srcip} -> {$dstip}", $proto, $srcport, $dstport);

    addipinfo($allipinfo, $srcip, $proto, $srcport, $dstport);
    addipinfo($allipinfo, $dstip, $proto, $srcport, $dstport);

  }
Ad Schellevis's avatar
Ad Schellevis committed
101 102 103
}


104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
function print_summary_table($label, $iparr, $sort = TRUE) {
    if ($sort) {
        uksort($iparr, "sort_by_ip");
    }
    ?>
    <section class="col-xs-12">
      <div class="content-box">
        <header class="content-box-head container-fluid">
          <h3><?=$label; ?></h3>
        </header>
        <div class="table-responsive">
          <table class="table table-striped">
            <tr>
              <td><?=gettext("IP");?></td>
              <td># <?=gettext("States");?></td>
              <td><?=gettext("Proto");?></td>
              <td># <?=gettext("States");?></td>
              <td><?=gettext("Src Ports");?></td>
              <td><?=gettext("Dst Ports");?></td>
            </tr>
    <?php
    foreach($iparr as $ip => $ipinfo) { ?>
            <tr>
              <td><?= $ip; ?></td>
              <td><?= $ipinfo['seen']; ?></td>
              <td colspan="4">&nbsp;</td>
            </tr>
<?php     foreach($ipinfo['protos'] as $proto => $protoinfo) { ?>
        <tr>
          <td colspan="2">&nbsp;</td>
          <td><?=$proto; ?></td>
          <td ><?=$protoinfo['seen']; ?></td>
          <td ><span data-toggle="tooltip" title="<?=build_port_info($protoinfo['srcports'], $proto); ?>"><?=count($protoinfo['srcports']); ?></span></td>
          <td ><span data-toggle="tooltip" title="<?=build_port_info($protoinfo['dstports'], $proto); ?>"><?=count($protoinfo['dstports']); ?></span></td>
        </tr>
        <?php } ?>
      <?php } ?>
      </table>
     </div>
   </div>
  </section>
Ad Schellevis's avatar
Ad Schellevis committed
145 146 147 148 149 150 151
<?php
}

$pgtitle = array(gettext("Diagnostics"),gettext("State Table Summary"));
include("head.inc");
echo "<body>";
include("fbegin.inc");
152 153
?>
<section class="page-content-main">
154 155
  <div class="container-fluid">
    <div class="row">
156
<?
Ad Schellevis's avatar
Ad Schellevis committed
157 158 159 160 161
print_summary_table(gettext("By Source IP"), $srcipinfo);
print_summary_table(gettext("By Destination IP"), $dstipinfo);
print_summary_table(gettext("Total per IP"), $allipinfo);
print_summary_table(gettext("By IP Pair"), $pairipinfo, FALSE);
?>
162 163
    </div>
  </div>
164 165
</section>

166
<?php include("foot.inc"); ?>