From 614edcfe00a0cecb35e5160af68246d84f26e216 Mon Sep 17 00:00:00 2001 From: r4sas Date: Wed, 29 Jun 2022 18:38:53 +0000 Subject: [PATCH] [bob] check for existing session Signed-off-by: r4sas --- checker.php | 23 ++++++++++++++--------- lib/bob.php | 30 ++++++++++++++++++------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/checker.php b/checker.php index 9d1fef3..5ae2ff9 100644 --- a/checker.php +++ b/checker.php @@ -25,15 +25,20 @@ $hosts = $STH->fetchAll(PDO::FETCH_KEY_PAIR); echo "[BOB] Starting session for checking" . PHP_EOL; $bob = new BOB($options); -$bob->setnick(); -$bob->options(); -$bob->newkeys(); -$bob->intun(); -$bob->start(); - -/* Sleep 10 seconds awaitng tunnels get built */ -echo "[BOB] Session started, awaiting 10 seconds for tunnels" . PHP_EOL; -sleep(10); +if ($bob->setnick()) { + $bob->options(); + $bob->newkeys(); + $bob->intun(); + $bob->start(); + + /* Sleep 10 seconds awaitng tunnels get built */ + echo "[BOB] Session started, awaiting 10 seconds for tunnels" . PHP_EOL; + sleep(10); + +} else { + echo "[BOB] Stale session is found, using it" . PHP_EOL; + $bob->getnick(); +} /* Start async checker tasks */ Loop::run(function () use (&$results, $hosts, $options) { diff --git a/lib/bob.php b/lib/bob.php index 8224fe8..1e98e45 100644 --- a/lib/bob.php +++ b/lib/bob.php @@ -50,13 +50,17 @@ class BOB { socket_close($this->sock); } - public function setnick() { + public function setnick(): bool { $command = "setnick " . $this->options["bob_nick"] . "\n"; socket_write($this->sock, $command, strlen($command)); $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); - if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + if(!preg_match('/^OK/', $response)) { + //echo "[BOB] setnick response: " . $response; + return false; + } + + return true; } public function options() { @@ -68,7 +72,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] options response: " . $response; } } @@ -78,7 +82,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] newkeys response: " . $response; } public function start() { @@ -87,7 +91,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] start response: " . $response; } public function intun() { @@ -96,14 +100,14 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] inhost response: " . $response; $command = "inport " . rand(1024, 65535) . "\n"; socket_write($this->sock, $command, strlen($command)); $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] inport response: " . $response; } public function getnick() { @@ -112,7 +116,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] getnick response: " . $response; } public function lookup(string $address): bool { @@ -122,6 +126,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(preg_match('/^OK/', $response)) + //echo "[BOB] lookup response: " . $response; return true; return false; @@ -134,6 +139,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(preg_match('/^OK/', $response)) + //echo "[BOB] lookup response: " . $response; return true; return false; @@ -145,7 +151,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] stop response: " . $response; } public function clear() { @@ -154,7 +160,7 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] clear response: " . $response; } public function zap() { @@ -163,6 +169,6 @@ class BOB { $response = socket_read($this->sock, 1024, PHP_NORMAL_READ); if(!preg_match('/^OK/', $response)) - echo "BOB response: " . $response; + echo "[BOB] zap response: " . $response; } }