From 510acd01f6bdc4d93090bf597ae0407a6bd5635f Mon Sep 17 00:00:00 2001
From: 1800alex <1800alex@gmail.com>
Date: Mon, 6 Nov 2017 08:38:21 -0500
Subject: [PATCH 1/5] Added support for checking if binary process is running
---
js/esm.js | 4 +--
libs/Utils/Misc.php | 46 +++++++++++++++++++-----
libs/services.php | 87 +++++++++++++++++++++++++++++++++++++++------
3 files changed, 116 insertions(+), 21 deletions(-)
diff --git a/js/esm.js b/js/esm.js
index b61eb57..7ab3628 100644
--- a/js/esm.js
+++ b/js/esm.js
@@ -282,7 +282,7 @@ esm.getServices = function() {
html += '
';
html += ''+label_status+' | ';
html += ''+data[line].name+' | ';
- html += ''+data[line].port+' | ';
+ html += ''+data[line].detail+' | ';
html += '
';
$box.append(html);
@@ -368,4 +368,4 @@ esm.mapping = {
network: esm.getNetwork,
ping: esm.getPing,
services: esm.getServices
-};
\ No newline at end of file
+};
diff --git a/libs/Utils/Misc.php b/libs/Utils/Misc.php
index 7aab276..7237ecb 100644
--- a/libs/Utils/Misc.php
+++ b/libs/Utils/Misc.php
@@ -171,15 +171,10 @@ public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3)
{
$handle = @fsockopen($host, $port, $errno, $errstr, $timeout);
- if (!$handle)
- {
- return false;
- }
- else
- {
- fclose($handle);
+ if ($handle)
return true;
- }
+ else
+ return false;
}
elseif ($protocol == 'udp')
{
@@ -207,4 +202,37 @@ public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3)
return false;
}
-}
\ No newline at end of file
+
+ /**
+ * Checks if we are able to test for running processes on this OS
+ *
+ * @return bool True if the available else false
+ */
+ public static function checkIfProcessRunningAvailable()
+ {
+ if (is_executable("/bin/pidof"))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Checks if a process is running (using /bin/pidof)
+ *
+ * @param string $process Process name to check
+ * @return bool True if the process is running else false
+ */
+ public static function checkIfProcessRunning($process)
+ {
+ exec("/bin/pidof $process", $response);
+
+ if ($response)
+ {
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/libs/services.php b/libs/services.php
index 61411e4..0fa39fb 100644
--- a/libs/services.php
+++ b/libs/services.php
@@ -9,6 +9,8 @@
$show_port = $Config->get('services:show_port');
+$check_binary_allowed = Misc::checkIfProcessRunningAvailable();
+
if (count($Config->get('services:list')) > 0)
{
foreach ($Config->get('services:list') as $service)
@@ -16,20 +18,85 @@
$host = $service['host'];
$port = $service['port'];
$name = $service['name'];
+ $binary = $service['binary'];
$protocol = isset($service['protocol']) && in_array($service['protocol'], $available_protocols) ? $service['protocol'] : 'tcp';
- if (Misc::scanPort($host, $port, $protocol))
+ $check_port = false;
+ $check_binary = false;
+
+ if ( (isset($host)) && (isset($port)) && (isset($protocol)) )
+ {
+ $check_port = true;
+ }
+
+ if ($check_binary_allowed === true)
+ {
+ if (isset($binary))
+ {
+ $check_binary = true;
+ }
+ }
+
+ $port_status = 1;
+ $binary_status = 1;
+ $status = 0;
+
+ if ($check_port === true)
+ {
+ if (Misc::scanPort($host, $port, $protocol))
+ $port_status = 1;
+ else
+ $port_status = 0;
+ }
+
+ if ($check_binary === true)
+ {
+ if (Misc::checkIfProcessRunning($binary))
+ $binary_status = 1;
+ else
+ $binary_status = 0;
+ }
+
+ if ( ($port_status > 0) && ($binary_status > 0) )
+ {
$status = 1;
- else
- $status = 0;
-
- $datas[] = array(
- 'port' => $show_port === true ? $port : '',
- 'name' => $name,
- 'status' => $status,
- );
+ }
+
+ $detail = "";
+
+ if ( ($check_port === true) && ($check_binary === true) )
+ {
+ if($show_port === true)
+ {
+ $detail = $protocol . ":" . $port . " and " . $binary;
+ }
+ else
+ {
+ $detail = $binary;
+ }
+ }
+ elseif($check_port)
+ {
+ if($show_port === true)
+ {
+ $detail = $protocol . ":" . $port;
+ }
+ }
+ elseif($check_binary)
+ {
+ $detail = $binary;
+ }
+
+ if ( ($check_port === true) || ($check_binary === true) )
+ {
+ $datas[] = array(
+ 'detail' => $detail,
+ 'name' => $name,
+ 'status' => $status,
+ );
+ }
}
}
-echo json_encode($datas);
\ No newline at end of file
+echo json_encode($datas);
From 5e6f1c34483b87c3415ffecacf60cdbd6edd4b70 Mon Sep 17 00:00:00 2001
From: 1800alex <1800alex@gmail.com>
Date: Mon, 6 Nov 2017 08:40:52 -0500
Subject: [PATCH 2/5] Added support for checking if binary process is running
---
conf/esm.config.json | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/conf/esm.config.json b/conf/esm.config.json
index e40b3ea..30ff455 100644
--- a/conf/esm.config.json
+++ b/conf/esm.config.json
@@ -61,9 +61,14 @@
},
{
"name": "SSH",
+ "binary": "sshd",
"host": "localhost",
"port": 22,
"protocol": "tcp"
+ },
+ {
+ "name": "Init Service",
+ "binary": "init"
}
]
}
From d8fb9847f6162a241cded9cb30a6914e9e4a5f4f Mon Sep 17 00:00:00 2001
From: 1800alex <1800alex@gmail.com>
Date: Mon, 6 Nov 2017 08:47:11 -0500
Subject: [PATCH 3/5] Resolved conflict with old version
---
libs/Utils/Misc.php | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/libs/Utils/Misc.php b/libs/Utils/Misc.php
index 7237ecb..593b9e0 100644
--- a/libs/Utils/Misc.php
+++ b/libs/Utils/Misc.php
@@ -156,6 +156,7 @@ public static function pluralize($nb, $plural = 's', $singular = '')
}
+
/**
* Checks if a port is open (TCP or UPD)
*
@@ -171,10 +172,15 @@ public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3)
{
$handle = @fsockopen($host, $port, $errno, $errstr, $timeout);
- if ($handle)
- return true;
- else
+ if (!$handle)
+ {
return false;
+ }
+ else
+ {
+ fclose($handle);
+ return true;
+ }
}
elseif ($protocol == 'udp')
{
From 8eb1afeb5e12b53189181a96c73ff638b796e068 Mon Sep 17 00:00:00 2001
From: 1800alex <1800alex@gmail.com>
Date: Mon, 6 Nov 2017 08:47:54 -0500
Subject: [PATCH 4/5] Resolved conflict with old version
---
libs/Utils/Misc.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/libs/Utils/Misc.php b/libs/Utils/Misc.php
index 593b9e0..311fd21 100644
--- a/libs/Utils/Misc.php
+++ b/libs/Utils/Misc.php
@@ -138,7 +138,6 @@ public static function whichCommand($cmds, $args = '', $returnWithArgs = true)
return $return;
}
-
/**
* Allows to pluralize a word based on a number
* Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
From 6fa39d1b8ebcdebdd6c62a8acf5e2d0e6359eb90 Mon Sep 17 00:00:00 2001
From: 1800alex <1800alex@gmail.com>
Date: Mon, 6 Nov 2017 08:53:35 -0500
Subject: [PATCH 5/5] Added missing === true
---
libs/services.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libs/services.php b/libs/services.php
index 0fa39fb..a284333 100644
--- a/libs/services.php
+++ b/libs/services.php
@@ -75,14 +75,14 @@
$detail = $binary;
}
}
- elseif($check_port)
+ elseif($check_port === true)
{
if($show_port === true)
{
$detail = $protocol . ":" . $port;
}
}
- elseif($check_binary)
+ elseif($check_binary === true)
{
$detail = $binary;
}