From 180303a57b2c0b88827787eac88f623895bd4fd4 Mon Sep 17 00:00:00 2001
From: Thibault Debatty <thibault.debatty@gmail.com>
Date: Wed, 31 Jul 2019 14:11:50 +0200
Subject: [PATCH] show listening udp ports

---
 app/Sensor/ListeningPorts.php | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/app/Sensor/ListeningPorts.php b/app/Sensor/ListeningPorts.php
index d55ed21..99d0e22 100644
--- a/app/Sensor/ListeningPorts.php
+++ b/app/Sensor/ListeningPorts.php
@@ -14,24 +14,33 @@ class ListeningPorts extends \App\AbstractSensor
 
     public function report()
     {
-        $record = $this->getLastRecord("netstat-listen-tcp");
-        if ($record == null) {
+        $tcp_record = $this->getLastRecord("netstat-listen-tcp");
+        $udp_record = $this->getLastRecord("netstat-listen-udp");
+        if ($tcp_record == null && $udp_record == null) {
             return "<p>No data available...</p>";
         }
 
-        $ports = $this->parse($record["netstat-listen-tcp"]);
+        $ports = array_merge(
+                $this->parse($tcp_record["netstat-listen-tcp"]),
+                $this->parse($udp_record["netstat-listen-udp"]));
+
+        usort($ports,
+                function(ListeningPort $port1, ListeningPort $port2) {
+                    return $port1->port - $port2->port;
+                });
+
         $return = "<table class='table table-sm'>";
         $return .= "<tr>"
+                . "<th>Port</th>"
                 . "<th>Proto</th>"
                 . "<th>Bind address</th>"
-                . "<th>Port</th>"
                 . "<th>Process</th>"
                 . "</tr>";
         foreach ($ports as $port) {
             $return .= "<tr>"
+                    . "<td>" . $port->port . "</td>"
                     . "<td>" . $port->proto . "</td>"
                     . "<td>" . $port->bind . "</td>"
-                    . "<td>" . $port->port . "</td>"
                     . "<td>" . $port->process . "</td>"
                     . "</tr>";
         }
@@ -49,8 +58,12 @@ class ListeningPorts extends \App\AbstractSensor
      * @param string $string
      * @return \App\Sensor\ListeningPort[]
      */
-    public function parse(string $string)
+    public function parse(?string $string)
     {
+        if ($string == null) {
+            return [];
+        }
+
         $values = [];
         preg_match_all(self::REGEXP, $string, $values);
 
-- 
GitLab