From 1b228a58c952b174df38499ed87b09fd9cde6b76 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 2 Nov 2024 16:35:22 +1100 Subject: [PATCH] Attempt to work out a packets domain from a nodes addresses for looking at a default --- app/Classes/FTN/Packet.php | 36 ++++++++++++++++---------- app/Classes/File/Receive.php | 4 +-- app/Console/Commands/PacketInfo.php | 2 +- app/Console/Commands/PacketProcess.php | 2 +- app/Jobs/PacketProcess.php | 12 ++++----- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/Classes/FTN/Packet.php b/app/Classes/FTN/Packet.php index 544c78a..30235c0 100644 --- a/app/Classes/FTN/Packet.php +++ b/app/Classes/FTN/Packet.php @@ -3,6 +3,7 @@ namespace App\Classes\FTN; use Carbon\Carbon; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; @@ -11,7 +12,7 @@ use Symfony\Component\HttpFoundation\File\File; use App\Classes\FTN as FTNBase; use App\Exceptions\InvalidPacketException; -use App\Models\{Address,Domain,Echomail,Netmail,Software,System,Zone}; +use App\Models\{Address,Echomail,Netmail,Software,System,Zone}; use App\Notifications\Netmails\{EchomailBadAddress,NetmailBadAddress}; /** @@ -87,11 +88,11 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable * @param mixed $f File handler returning packet data * @param string $name * @param int $size - * @param Domain|null $domain + * @param System|null $so - The system that sent us the packet, used to figure out domains if the packet is for a different zone * @return Packet * @throws InvalidPacketException */ - public static function process(mixed $f,string $name,int $size,Domain $domain=NULL): self + public static function process(mixed $f,string $name,int $size,System $so=NULL): self { Log::debug(sprintf('%s:+ Opening Packet [%s] with size [%d]',self::LOGKEY,$name,$size)); @@ -140,22 +141,29 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable Log::info(sprintf('%s:- Packet [%s] is a [%s] packet',self::LOGKEY,$o->name,get_class($o))); - // Work out the packet zone - if ($o->fz && ($o->fd || $domain)) { - $o->zone = Zone::select('zones.*') - ->join('domains',['domains.id'=>'zones.domain_id']) - ->where('zone_id',$o->fz) - ->where('name',$o->fd ?: $domain->name) - ->single(); + if ($o->fz && ($o->fd || $so)) { + Log::alert(sprintf('%s:! No domain in the packet, work it out from the system [%d] for zone [%d]',self::LOGKEY,$so->name,$o->fz)); + + if (($x=$so->zones->where('zone_id',$o->fz)->unique('domain_id'))->count() === 1) { + $o->zone = $x->pop(); + + } else { + Log::alert(sprintf('%s:! Node [%s] has two zones with [%d]',self::LOGKEY,$so->name,$o->fz)); + } } // If zone is not set, then we need to use a default zone - the messages may not be from this zone. if (empty($o->zone)) { Log::alert(sprintf('%s:! We couldnt work out the packet zone, so we have fallen back to the default for [%d]',self::LOGKEY,$o->fz)); - $o->zone = Zone::where('zone_id',$o->fz) - ->where('default',TRUE) - ->singleOrFail(); + try { + $o->zone = Zone::where('zone_id',$o->fz) + ->where('default',TRUE) + ->singleOrFail(); + + } catch (ModelNotFoundException $e) { + throw new InvalidPacketException(sprintf('%s:! We couldnt work out the packet zone, and there isnt a default for[%d]',self::LOGKEY,$o->fz)); + } } Log::info(sprintf('%s:- Packet Dated [%s] from [%s] to [%s]',self::LOGKEY,$o->date,$o->fftn_t,$o->tftn_t)); @@ -186,7 +194,7 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable } // If we get here - throw new InvalidPacketException(sprintf('Cannot determine END of message/packet: %s|%s',get_class($o),hex_dump($message)));; + throw new InvalidPacketException(sprintf('Cannot determine END of message/packet: %s|%s',get_class($o),hex_dump($message))); } if ($msgbuf) diff --git a/app/Classes/File/Receive.php b/app/Classes/File/Receive.php index ad35b17..05cdc59 100644 --- a/app/Classes/File/Receive.php +++ b/app/Classes/File/Receive.php @@ -128,9 +128,9 @@ class Receive extends Base // If packet is greater than a size, lets queue it if ($this->queue || ($this->receiving->size > config('fido.queue_size',0))) { Log::info(sprintf('%s:- Packet [%s] will be sent to the queue for processing because its [%d] size, or queue forced',self::LOGKEY,$this->receiving->full_name,$this->receiving->size)); - PacketProcess::dispatch($this->receiving->rel_name,$this->ao->zone->domain,FALSE,$rcvd_time); + PacketProcess::dispatch($this->receiving->rel_name,$this->ao->system,FALSE,$rcvd_time); } else - PacketProcess::dispatchSync($this->receiving->rel_name,$this->ao->zone->domain,TRUE,$rcvd_time); + PacketProcess::dispatchSync($this->receiving->rel_name,$this->ao->system,TRUE,$rcvd_time); } catch (\Exception $e) { Log::error(sprintf('%s:! Got error dispatching packet [%s] (%d:%s-%s).',self::LOGKEY,$this->receiving->rel_name,$e->getLine(),$e->getFile(),$e->getMessage())); diff --git a/app/Console/Commands/PacketInfo.php b/app/Console/Commands/PacketInfo.php index cb7f078..5a413bb 100644 --- a/app/Console/Commands/PacketInfo.php +++ b/app/Console/Commands/PacketInfo.php @@ -50,7 +50,7 @@ class PacketInfo extends Command } foreach ($f as $packet) { - $pkt = Packet::process($packet,$x=$f->itemName(),$f->itemSize(),$a?->zone->domain); + $pkt = Packet::process($packet,$x=$f->itemName(),$f->itemSize(),$a?->system); $this->alert(sprintf('File Name: %s',$x)); diff --git a/app/Console/Commands/PacketProcess.php b/app/Console/Commands/PacketProcess.php index a447d13..5d632ac 100644 --- a/app/Console/Commands/PacketProcess.php +++ b/app/Console/Commands/PacketProcess.php @@ -78,7 +78,7 @@ class PacketProcess extends Command return self::FAILURE; } - Job::dispatchSync($rel_name,$ao->zone->domain,$this->option('dontqueue')); + Job::dispatchSync($rel_name,$ao->system,$this->option('dontqueue')); return self::SUCCESS; } diff --git a/app/Jobs/PacketProcess.php b/app/Jobs/PacketProcess.php index fcdae4a..ab64e3e 100644 --- a/app/Jobs/PacketProcess.php +++ b/app/Jobs/PacketProcess.php @@ -17,7 +17,7 @@ use League\Flysystem\UnableToMoveFile; use App\Classes\File; use App\Classes\FTN\Packet; use App\Exceptions\InvalidPacketException; -use App\Models\{Domain,Echomail,Netmail}; +use App\Models\{Echomail,Netmail,System}; use App\Notifications\Netmails\PacketPasswordInvalid; class PacketProcess implements ShouldQueue @@ -27,15 +27,15 @@ class PacketProcess implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private string $filename; - private Domain $do; + private System $so; private Carbon $rcvd_time; private bool $interactive; private bool $nobot; - public function __construct(string $filename,Domain $do,bool $interactive=TRUE,Carbon $rcvd_time=NULL,bool $nobot=FALSE) + public function __construct(string $filename,System $so,bool $interactive=TRUE,Carbon $rcvd_time=NULL,bool $nobot=FALSE) { $this->filename = $filename; - $this->do = $do; + $this->so = $so; $this->interactive = $interactive; $this->rcvd_time = $rcvd_time ?: Carbon::now(); $this->nobot = $nobot; @@ -71,7 +71,7 @@ class PacketProcess implements ShouldQueue foreach ($f as $packet) { try { - $pkt = Packet::process($packet,Arr::get(stream_get_meta_data($packet),'uri'),$f->itemSize(),$this->do); + $pkt = Packet::process($packet,Arr::get(stream_get_meta_data($packet),'uri'),$f->itemSize(),$this->so); // Check that the packet is from a system that is defined in the DB if (! $pkt->fftn) { @@ -81,7 +81,7 @@ class PacketProcess implements ShouldQueue } if (! our_nodes($pkt->fftn->zone->domain)->contains($pkt->fftn)) { - Log::error(sprintf('%s:! Packet [%s] is from a system that is not configured with us? [%s] for [%s]',self::LOGKEY,$this->filename,$pkt->fftn_t,$this->do->name)); + Log::error(sprintf('%s:! Packet [%s] is from a system that is not configured with us? [%s]',self::LOGKEY,$this->filename,$pkt->fftn_t)); // @todo Notification::route('netmail',$pkt->fftn)->notify(new UnexpectedPacketFromYou($this->filename)); // @todo Parse the packet for netmails and process them. We'll only accept netmails to us, and ignore all others