From 38795b83bf92be906080c7b36367ce4f215ff5b1 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 1 Jun 2024 12:55:27 +1000 Subject: [PATCH] Move HAproxy exceptions into their own class, and downgrade HAproxy errors since they are handled --- app/Classes/Protocol.php | 2 +- app/Classes/Protocol/Binkp.php | 2 +- app/Classes/Protocol/EMSI.php | 4 +-- app/Classes/Protocol/Zmodem.php | 4 +-- .../Sock/Exception/HAproxyException.php | 6 ++++ .../Sock/{ => Exception}/SocketException.php | 2 +- app/Classes/Sock/SocketClient.php | 31 +++++++------------ app/Classes/Sock/SocketServer.php | 9 +++++- app/Console/Commands/CommBinkpReceive.php | 2 +- app/Console/Commands/CommEMSIReceive.php | 2 +- app/Console/Commands/ServerStart.php | 3 +- app/Jobs/AddressPoll.php | 2 +- 12 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 app/Classes/Sock/Exception/HAproxyException.php rename app/Classes/Sock/{ => Exception}/SocketException.php (96%) diff --git a/app/Classes/Protocol.php b/app/Classes/Protocol.php index 336de78..28c2ea7 100644 --- a/app/Classes/Protocol.php +++ b/app/Classes/Protocol.php @@ -7,8 +7,8 @@ use Illuminate\Support\Facades\Log; use App\Classes\File\{Receive,Send}; use App\Classes\Protocol\EMSI; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; -use App\Classes\Sock\SocketException; use App\Models\{Address,Mailer,Setup,System,SystemLog}; // @todo after receiving a mail packet/file, dont acknowledge it until we can validate that we can read it properly. diff --git a/app/Classes/Protocol/Binkp.php b/app/Classes/Protocol/Binkp.php index 508998c..7d8fa1a 100644 --- a/app/Classes/Protocol/Binkp.php +++ b/app/Classes/Protocol/Binkp.php @@ -11,8 +11,8 @@ use League\Flysystem\UnreadableFileEncountered; use App\Classes\Crypt; use App\Classes\Node; use App\Classes\Protocol as BaseProtocol; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; -use App\Classes\Sock\SocketException; use App\Exceptions\{FileGrewException,InvalidFTNException}; use App\Models\{Address,Mailer}; diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index f2d8021..9ce5961 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -6,12 +6,12 @@ use Carbon\Carbon; use Illuminate\Support\Facades\Log; use App\Classes\Protocol as BaseProtocol; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; -use App\Classes\Sock\SocketException; use App\Exceptions\InvalidFTNException; -use App\Models\{Address,Mailer,Setup}; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; +use App\Models\{Address,Mailer,Setup}; use App\Traits\CRC as CRCTrait; // http://ftsc.org/docs/fsc-0056.001 diff --git a/app/Classes/Protocol/Zmodem.php b/app/Classes/Protocol/Zmodem.php index 8f178b2..fb2bc91 100644 --- a/app/Classes/Protocol/Zmodem.php +++ b/app/Classes/Protocol/Zmodem.php @@ -5,9 +5,9 @@ namespace App\Classes\Protocol; use Illuminate\Support\Facades\Log; use App\Classes\{Node,Protocol}; -use App\Classes\Protocol\Zmodem as ZmodemClass; use App\Classes\File\{Receive,Send}; -use App\Classes\Sock\{SocketClient,SocketException}; +use App\Classes\Sock\Exception\SocketException; +use App\Classes\Sock\SocketClient; use App\Interfaces\CRC as CRCInterface; use App\Interfaces\Zmodem as ZmodemInterface; use App\Models\{Address,Mailer}; diff --git a/app/Classes/Sock/Exception/HAproxyException.php b/app/Classes/Sock/Exception/HAproxyException.php new file mode 100644 index 0000000..c903277 --- /dev/null +++ b/app/Classes/Sock/Exception/HAproxyException.php @@ -0,0 +1,6 @@ +address_remote,$this->port_remote,$this->type)); - if ($this->read(5,12) !== "\x0d\x0a\x0d\x0a\x00\x0d\x0aQUIT\x0a") { - Log::error(sprintf('%s:! Failed to initialise HAPROXY connection',self::LOGKEY)); - throw new SocketException(SocketException::CANT_CONNECT,'Failed to initialise HAPROXY connection'); - } + if ($this->read(5,12) !== "\x0d\x0a\x0d\x0a\x00\x0d\x0aQUIT\x0a") + throw new HAproxyException('Failed to initialise HAPROXY connection'); // Version/Command $vc = $this->read_ch(5); - if (($x=($vc>>4)&0x7) !== 2) { - Log::error(sprintf('%s:! HAPROXY version [%d] is not handled',self::LOGKEY,$x)); - - throw new SocketException(SocketException::CANT_CONNECT,'Unknown HAPROXY version'); - } + if (($x=($vc>>4)&0x7) !== 2) + throw new HAproxyException(sprintf('Unknown HAPROXY version [%d]',$x)); switch ($x=($vc&0x7)) { // HAPROXY internal case 0: - Log::debug(sprintf('%s:! HAPROXY internal health-check',self::LOGKEY)); - throw new SocketException(SocketException::CANT_CONNECT,'Healthcheck'); + throw new HAproxyException('HAPROXY internal health-check'); // PROXY connection case 1: break; default: - Log::error(sprintf('%s:! HAPROXY command [%d] is not handled',self::LOGKEY,$x)); - - throw new SocketException(SocketException::CANT_CONNECT,'Unknown HAPROXY command'); + throw new HAproxyException(sprintf('HAPROXY command [%d] is not handled',$x)); } // Protocol/Address Family @@ -101,8 +95,7 @@ final class SocketClient { break; default: - Log::error(sprintf('%s:! HAPROXY protocol [%d] is not handled',self::LOGKEY,$x)); - throw new SocketException(SocketException::CANT_CONNECT,'Unknown HAPROXY protocol'); + throw new HAproxyException(sprintf('HAPROXY protocol [%d] is not handled',$x)); } switch ($x=($pa&0x7)) { @@ -110,8 +103,7 @@ final class SocketClient { break; default: - Log::error(sprintf('%s:! HAPROXY address family [%d] is not handled',self::LOGKEY,$x)); - throw new SocketException(SocketException::CANT_CONNECT,'Unknown HAPROXY address family'); + throw new HAproxyException(sprintf('HAPROXY address family [%d] is not handled',$x)); } $len = Arr::get(unpack('n',$this->read(5,2)),1); @@ -126,8 +118,7 @@ final class SocketClient { $dst = inet_ntop($this->read(5,16)); } else { - Log::error(sprintf('%s:! HAPROXY address len [%d:%d] is not handled',self::LOGKEY,$p,$len)); - throw new SocketException(SocketException::CANT_CONNECT,'Unknown HAPROXY address length'); + throw new HAproxyException(sprintf('HAPROXY address len [%d:%d] is not handled',$p,$len)); } $src_port = unpack('n',$this->read(5,2)); diff --git a/app/Classes/Sock/SocketServer.php b/app/Classes/Sock/SocketServer.php index 7529f16..e2b549e 100644 --- a/app/Classes/Sock/SocketServer.php +++ b/app/Classes/Sock/SocketServer.php @@ -4,6 +4,8 @@ namespace App\Classes\Sock; use Illuminate\Support\Facades\Log; +use App\Classes\Sock\Exception\{HAproxyException,SocketException}; + final class SocketServer { private const LOGKEY = 'SS-'; @@ -128,8 +130,13 @@ final class SocketServer { try { $r = new SocketClient($accept); + } catch (HAproxyException $e) { + Log::notice(sprintf('%s:! HAPROXY Exception [%s]',self::LOGKEY,$e->getMessage())); + socket_close($accept); + continue; + } catch (\Exception $e) { - Log::error(sprintf('%s:! Creating Socket client failed? [%s]',self::LOGKEY,$e->getMessage())); + Log::notice(sprintf('%s:! Creating Socket client failed? [%s]',self::LOGKEY,$e->getMessage())); socket_close($accept); continue; } diff --git a/app/Console/Commands/CommBinkpReceive.php b/app/Console/Commands/CommBinkpReceive.php index fadab09..f3341a5 100644 --- a/app/Console/Commands/CommBinkpReceive.php +++ b/app/Console/Commands/CommBinkpReceive.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use App\Classes\Protocol\Binkp; -use App\Classes\Sock\SocketException; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketServer; use App\Models\Setup; diff --git a/app/Console/Commands/CommEMSIReceive.php b/app/Console/Commands/CommEMSIReceive.php index 805aab9..58ceca5 100644 --- a/app/Console/Commands/CommEMSIReceive.php +++ b/app/Console/Commands/CommEMSIReceive.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use App\Classes\Protocol\EMSI; -use App\Classes\Sock\SocketException; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketServer; use App\Models\Setup; diff --git a/app/Console/Commands/ServerStart.php b/app/Console/Commands/ServerStart.php index af13dc1..cb1b9a9 100644 --- a/app/Console/Commands/ServerStart.php +++ b/app/Console/Commands/ServerStart.php @@ -6,7 +6,8 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use App\Classes\Protocol\{Binkp,DNS,EMSI}; -use App\Classes\Sock\{SocketException,SocketServer}; +use App\Classes\Sock\Exception\SocketException; +use App\Classes\Sock\SocketServer; use App\Models\Setup; class ServerStart extends Command diff --git a/app/Jobs/AddressPoll.php b/app/Jobs/AddressPoll.php index b761ee4..972dcc5 100644 --- a/app/Jobs/AddressPoll.php +++ b/app/Jobs/AddressPoll.php @@ -15,8 +15,8 @@ use Illuminate\Support\Facades\Notification; use App\Classes\Protocol; use App\Classes\Protocol\{Binkp,EMSI}; +use App\Classes\Sock\Exception\SocketException; use App\Classes\Sock\SocketClient; -use App\Classes\Sock\SocketException; use App\Models\{Address,Mailer,Setup}; use App\Notifications\Netmails\PollingFailed; use App\Traits\ObjectIssetFix;