From 3b86f76f51ac25aacfdeca252f4d07e4012c4c8b 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/Console/Commands/PacketInfo.php | 2 +- app/Jobs/PacketProcess.php | 12 +++++----- 3 files changed, 29 insertions(+), 21 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/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/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