Added KnownDeviceManager

This commit is contained in:
Zi Xing 2021-12-19 16:31:55 -05:00
parent f6bdb97603
commit 8aa1a50cce
7 changed files with 359 additions and 0 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace khm\Classes;
use khm\Objects\Device;
use khm\Objects\KnownHost;
class Utilities
{
/**
* @param KnownHost $knownHost
* @param Device $device
* @return string
*/
public static function generateKnownDeviceId(KnownHost $knownHost, Device $device): string
{
return hash('sha1', $knownHost->IPAddress . $device->Fingerprint);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace khm\Exceptions;
use Exception;
use Throwable;
class KnownDeviceNotFoundException extends Exception
{
/**
* @param $message
* @param $code
* @param Throwable|null $previous
* @noinspection PhpMissingParamTypeInspection
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->message = $message;
$this->code = $code;
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace khm\Managers;
use khm\Classes\Utilities;
use khm\Exceptions\DatabaseException;
use khm\Exceptions\KnownDeviceNotFoundException;
use khm\khm;
use khm\Objects\Device;
use khm\Objects\KnownDevice;
use khm\Objects\KnownHost;
use msqg\QueryBuilder;
use ZiProto\ZiProto;
class KnownDevicesManager
{
/**
* @var khm
*/
private $khm;
/**
* @param khm $khm
*/
public function __construct(khm $khm)
{
$this->khm = $khm;
}
/**
* Registers a new record into the database
*
* @param Device $device
* @param KnownHost $knownHost
* @return KnownDevice
* @throws DatabaseException
* @noinspection PhpCastIsUnnecessaryInspection
*/
public function registerRecord(Device $device, KnownHost $knownHost): KnownDevice
{
$knownDevice = new KnownDevice();
$knownDevice->ID = Utilities::generateKnownDeviceId($knownHost, $device);
$knownDevice->CreatedTimestamp = time();
$knownDevice->LastSeenTimestamp = time();
$knownDevice->IPAddress = $knownHost->IPAddress;
$knownDevice->DeviceFingerprint = $device->Fingerprint;
$Query = QueryBuilder::insert_into('known_devices', [
'id' => $this->khm->getDatabase()->real_escape_string($knownDevice->ID),
'ip_address' => $this->khm->getDatabase()->real_escape_string($knownDevice->IPAddress),
'device_fingerprint' => $this->khm->getDatabase()->real_escape_string($knownDevice->DeviceFingerprint),
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($knownDevice->Properties)),
'last_seen_timestamp' => (int)$knownDevice->LastSeenTimestamp,
'created_timestamp' => (int)$knownDevice->CreatedTimestamp
]);
$QueryResults = $this->khm->getDatabase()->query($Query);
if($QueryResults == false)
{
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
}
return $knownDevice;
}
/**
* Returns an existing known device record from the database
*
* @param string $id
* @return KnownDevice
* @throws DatabaseException
* @throws KnownDeviceNotFoundException
*/
public function getRecord(string $id): KnownDevice
{
$Query = QueryBuilder::select('known_devices', [
'id',
'ip_address',
'device_fingerprint',
'properties',
'last_seen_timestamp',
'created_timestamp'
], 'id', $this->khm->getDatabase()->real_escape_string($id));
$QueryResults = $this->khm->getDatabase()->query($Query);
if($QueryResults == false)
{
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
}
$Row = $QueryResults->fetch_array(MYSQLI_ASSOC);
if ($Row == False)
{
throw new KnownDeviceNotFoundException('The known device id \'' . $id . '\' was not found');
}
$Row['properties'] = ZiProto::decode($Row['properties']);
return KnownDevice::fromArray($Row);
}
/**
* Updates the properties of an existing record
*
* @param KnownDevice $device
* @return void
* @throws DatabaseException
*/
public function updateProperties(KnownDevice $device)
{
$Query = QueryBuilder::update('known_devices', [
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($device->Properties->toArray()))
], 'id', $this->khm->getDatabase()->real_escape_string($device->ID));
$QueryResults = $this->khm->getDatabase()->query($Query);
if($QueryResults == false)
{
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
}
}
/**
* Updates the last seen of a known device record
*
* @param KnownDevice $device
* @return KnownDevice
* @throws DatabaseException
*/
public function updateLastSeen(KnownDevice $device): KnownDevice
{
$device->LastSeenTimestamp = time();
$Query = QueryBuilder::update('known_device', [
'last_seen_timestamp' => $device->LastSeenTimestamp
], 'id', $this->khm->getDatabase()->real_escape_string($device->ID));
$QueryResults = $this->khm->getDatabase()->query($Query);
if($QueryResults == false)
{
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
}
return $device;
}
}

View File

@ -0,0 +1,105 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace khm\Objects;
use khm\Objects\KnownDevice\Properties;
class KnownDevice
{
/**
* The unique ID of the known device
*
* @var string
*/
public $ID;
/**
* The IP Address of the known device
*
* @var string
*/
public $IPAddress;
/**
* The device fingerprint
*
* @var string
*/
public $DeviceFingerprint;
/**
* Properties of this known host
*
* @var Properties
*/
public $Properties;
/**
* The Unix Timestamp for when this device was last seen
*
* @var int
*/
public $LastSeenTimestamp;
/**
* The Unix Timestamp for when this device was first registered into the database
*
* @var int
*/
public $CreatedTimestamp;
public function __construct()
{
$this->Properties = new Properties();
}
/**
* Returns an array representation of the object
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->ID,
'ip_address' => $this->IPAddress,
'device_fingerprint' => $this->DeviceFingerprint,
'properties' => $this->Properties->toArray(),
'last_seen_timestamp' => $this->LastSeenTimestamp,
'created_timestamp' => $this->CreatedTimestamp
];
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return KnownDevice
*/
public static function fromArray(array $data): KnownDevice
{
$KnownDeviceObject = new KnownDevice();
if(isset($data['id']))
$KnownDeviceObject->ID = $data['id'];
if(isset($data['ip_address']))
$KnownDeviceObject->IPAddress = $data['ip_address'];
if(isset($data['device_fingerprint']))
$KnownDeviceObject->DeviceFingerprint = $data['device_fingerprint'];
if(isset($data['properties']))
$KnownDeviceObject->Properties = Properties::fromArray($data['properties']);
if(isset($data['last_seen_timestamp']))
$KnownDeviceObject->LastSeenTimestamp = (int)$data['last_seen_timestamp'];
if(isset($data['created_timestamp']))
$KnownDeviceObject->CreatedTimestamp = (int)$data['created_timestamp'];
return $KnownDeviceObject;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace khm\Objects\KnownDevice;
class Properties
{
/**
* Returns an array representation of the object
*
* @return array
*/
public function toArray(): array
{
return [];
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return Properties
*/
public static function fromArray(array $data): Properties
{
$PropertiesObject = new Properties();
return $PropertiesObject;
}
}

View File

@ -15,6 +15,7 @@
use khm\Managers\AbuseManager;
use khm\Managers\DevicesManager;
use khm\Managers\GeoManager;
use khm\Managers\KnownDevicesManager;
use khm\Managers\KnownHostsManager;
use khm\Managers\OnionManager;
use khm\Objects\AbuseCheck;
@ -78,6 +79,11 @@
*/
private $KnownHostsManager;
/**
* @var KnownDevicesManager
*/
private $KnownDevicesManager;
/**
* @throws ConfigurationNotDefinedException
*/
@ -115,6 +121,7 @@
$this->OnionManager = new OnionManager($this);
$this->DevicesManager = new DevicesManager($this);
$this->KnownHostsManager = new KnownHostsManager($this);
$this->KnownDevicesManager = new KnownDevicesManager($this);
}
/**
@ -368,4 +375,12 @@
{
return $this->KnownHostsManager;
}
/**
* @return KnownDevicesManager
*/
public function getKnownDevicesManager(): KnownDevicesManager
{
return $this->KnownDevicesManager;
}
}

View File

@ -85,6 +85,10 @@
"required": true,
"file": "Classes/Curl.php"
},
{
"required": true,
"file": "Classes/Utilities.php"
},
{
"required": true,
"file": "Classes/RegexLoader.php"
@ -121,6 +125,10 @@
"required": true,
"file": "Exceptions/GeoRecordNotFoundException.php"
},
{
"required": true,
"file": "Exceptions/KnownDeviceNotFoundException.php"
},
{
"required": true,
"file": "Exceptions/KhmResolutionException.php"
@ -169,10 +177,18 @@
"required": true,
"file": "Objects/UserAgent.php"
},
{
"required": true,
"file": "Objects/KnownDevice.php"
},
{
"required": true,
"file": "Objects/KnownHost.php"
},
{
"required": true,
"file": "Objects/KnownDevice/Properties.php"
},
{
"required": true,
"file": "Objects/UserAgentClient.php"
@ -228,6 +244,10 @@
{
"required": true,
"file": "Managers/KnownHostsManager.php"
},
{
"required": true,
"file": "Managers/KnownDevicesManager.php"
}
],
"files": [