Added KnownHostsManager
This commit is contained in:
parent
fcbad59a59
commit
3201b13eec
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace khm\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class KnownHostRecordNotFoundException 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;
|
||||
}
|
||||
}
|
|
@ -116,7 +116,7 @@
|
|||
|
||||
if ($Row == False)
|
||||
{
|
||||
throw new DeviceRecordNotFoundException('The device record wa was not found');
|
||||
throw new DeviceRecordNotFoundException('The device record was not found');
|
||||
}
|
||||
|
||||
$Row['properties'] = ZiProto::decode($Row['properties']);
|
||||
|
@ -134,7 +134,7 @@
|
|||
public function updateDeviceProperties(Device $device)
|
||||
{
|
||||
$Query = QueryBuilder::update('devices', [
|
||||
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($device->toArray()))
|
||||
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($device->Properties->toArray()))
|
||||
], 'fingerprint', $this->khm->getDatabase()->real_escape_string($device->Fingerprint));
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
namespace khm\Managers;
|
||||
|
||||
use khm\Exceptions\DatabaseException;
|
||||
use khm\Exceptions\KnownHostRecordNotFoundException;
|
||||
use khm\khm;
|
||||
use khm\Objects\KnownHost;
|
||||
use msqg\QueryBuilder;
|
||||
use ZiProto\ZiProto;
|
||||
|
||||
class KnownHostsManager
|
||||
{
|
||||
/**
|
||||
* @var khm
|
||||
*/
|
||||
private $khm;
|
||||
|
||||
/**
|
||||
* @param khm $khm
|
||||
*/
|
||||
public function __construct(khm $khm)
|
||||
{
|
||||
$this->khm = $khm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new known record into the database
|
||||
*
|
||||
* @param KnownHost $knownHost
|
||||
* @return KnownHost
|
||||
* @throws DatabaseException
|
||||
* @noinspection PhpCastIsUnnecessaryInspection
|
||||
*/
|
||||
public function registerRecord(KnownHost $knownHost): KnownHost
|
||||
{
|
||||
$knownHost->CreatedTimestamp = time();
|
||||
$knownHost->LastSeenTimestamp = time();
|
||||
|
||||
$Query = QueryBuilder::insert_into('known_hosts', [
|
||||
'ip_address' => $this->khm->getDatabase()->real_escape_string($knownHost->IPAddress),
|
||||
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($knownHost->Properties)),
|
||||
'last_seen_timestamp' => (int)$knownHost->LastSeenTimestamp,
|
||||
'created_timestamp' => (int)$knownHost->CreatedTimestamp
|
||||
]);
|
||||
|
||||
$QueryResults = $this->khm->getDatabase()->query($Query);
|
||||
|
||||
if($QueryResults == false)
|
||||
{
|
||||
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
|
||||
}
|
||||
|
||||
return $knownHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an existing record from the database
|
||||
*
|
||||
* @param string $ip_address
|
||||
* @return KnownHost
|
||||
* @throws DatabaseException
|
||||
* @throws KnownHostRecordNotFoundException
|
||||
*/
|
||||
public function getRecord(string $ip_address): KnownHost
|
||||
{
|
||||
$Query = QueryBuilder::select('known_hosts', [
|
||||
'ip_address',
|
||||
'properties',
|
||||
'last_seen_timestamp',
|
||||
'created_timestamp'
|
||||
], 'ip_address', $this->khm->getDatabase()->real_escape_string($ip_address));
|
||||
|
||||
$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 KnownHostRecordNotFoundException('The known host \'' . $ip_address . '\' was not found');
|
||||
}
|
||||
|
||||
$Row['properties'] = ZiProto::decode($Row['properties']);
|
||||
|
||||
return KnownHost::fromArray($Row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the properties of a known host
|
||||
*
|
||||
* @param KnownHost $knownHost
|
||||
* @return void
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function updateProperties(KnownHost $knownHost)
|
||||
{
|
||||
$Query = QueryBuilder::update('known_hosts', [
|
||||
'properties' => $this->khm->getDatabase()->real_escape_string(ZiProto::encode($knownHost->Properties->toArray()))
|
||||
], 'ip_address', $this->khm->getDatabase()->real_escape_string($knownHost->IPAddress));
|
||||
|
||||
|
||||
$QueryResults = $this->khm->getDatabase()->query($Query);
|
||||
|
||||
if($QueryResults == false)
|
||||
{
|
||||
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the last seen of a known hsost
|
||||
*
|
||||
* @param KnownHost $knownHost
|
||||
* @return KnownHost
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function updateLastSeen(KnownHost $knownHost): KnownHost
|
||||
{
|
||||
$knownHost->LastSeenTimestamp = time();
|
||||
$Query = QueryBuilder::update('known_hosts', [
|
||||
'last_seen_timestamp' => $knownHost->LastSeenTimestamp
|
||||
], 'ip_address', $this->khm->getDatabase()->real_escape_string($knownHost->IPAddress));
|
||||
|
||||
|
||||
$QueryResults = $this->khm->getDatabase()->query($Query);
|
||||
|
||||
if($QueryResults == false)
|
||||
{
|
||||
throw new DatabaseException($Query, $this->khm->getDatabase()->error);
|
||||
}
|
||||
|
||||
return $knownHost;
|
||||
}
|
||||
}
|
|
@ -106,6 +106,11 @@
|
|||
*/
|
||||
public $CreatedTimestamp;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->Properties = new Properties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object
|
||||
*
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace khm\Objects;
|
||||
|
||||
use khm\Objects\KnownHost\Properties;
|
||||
|
||||
class KnownHost
|
||||
{
|
||||
/**
|
||||
* The IP address of the known host
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $IPAddress;
|
||||
|
||||
/**
|
||||
* Properties associated with this known host
|
||||
*
|
||||
* @var Properties
|
||||
*/
|
||||
public $Properties;
|
||||
|
||||
/**
|
||||
* The Unix Timestamp for when this host was last seen
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $LastSeenTimestamp;
|
||||
|
||||
/**
|
||||
* The Unix Timestamp for when this known host 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 [
|
||||
'ip_address' => $this->IPAddress,
|
||||
'properties' => $this->Properties->toArray(),
|
||||
'last_seen_timestamp' => $this->LastSeenTimestamp,
|
||||
'created_timestamp' => $this->CreatedTimestamp
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs object from an array representation
|
||||
*
|
||||
* @param array $data
|
||||
* @return KnownHost
|
||||
*/
|
||||
public static function fromArray(array $data): KnownHost
|
||||
{
|
||||
$KnownHostObject = new KnownHost();
|
||||
|
||||
if(isset($data['ip_address']))
|
||||
$KnownHostObject->IPAddress = $data['ip_address'];
|
||||
|
||||
if(isset($data['properties']))
|
||||
$KnownHostObject->Properties = Properties::fromArray($data['properties']);
|
||||
|
||||
if(isset($data['last_seen_timestamp']))
|
||||
$KnownHostObject->LastSeenTimestamp = (int)$data['last_seen_timestamp'];
|
||||
|
||||
if(isset($data['created_timestamp']))
|
||||
$KnownHostObject->CreatedTimestamp = (int)$data['created_timestamp'];
|
||||
|
||||
return $KnownHostObject;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace khm\Objects\KnownHost;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
use khm\Managers\AbuseManager;
|
||||
use khm\Managers\DevicesManager;
|
||||
use khm\Managers\GeoManager;
|
||||
use khm\Managers\KnownHostsManager;
|
||||
use khm\Managers\OnionManager;
|
||||
use khm\Objects\AbuseCheck;
|
||||
use khm\Objects\GeoLookup;
|
||||
|
@ -72,6 +73,11 @@
|
|||
*/
|
||||
private $DevicesManager;
|
||||
|
||||
/**
|
||||
* @var KnownHostsManager
|
||||
*/
|
||||
private $KnownHostsManager;
|
||||
|
||||
/**
|
||||
* @throws ConfigurationNotDefinedException
|
||||
*/
|
||||
|
@ -108,6 +114,7 @@
|
|||
$this->GeoManager = new GeoManager($this);
|
||||
$this->OnionManager = new OnionManager($this);
|
||||
$this->DevicesManager = new DevicesManager($this);
|
||||
$this->KnownHostsManager = new KnownHostsManager($this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -353,4 +360,12 @@
|
|||
{
|
||||
return $this->DevicesManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return KnownHostsManager
|
||||
*/
|
||||
public function getKnownHostsManager(): KnownHostsManager
|
||||
{
|
||||
return $this->KnownHostsManager;
|
||||
}
|
||||
}
|
|
@ -141,6 +141,10 @@
|
|||
"required": true,
|
||||
"file": "Exceptions/InvalidSearchMethodException.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Exceptions/KnownHostRecordNotFoundException.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "ThirdParty/TorProject.php"
|
||||
|
@ -165,6 +169,10 @@
|
|||
"required": true,
|
||||
"file": "Objects/UserAgent.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Objects/KnownHost.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Objects/UserAgentClient.php"
|
||||
|
@ -189,6 +197,10 @@
|
|||
"required": true,
|
||||
"file": "Objects/UserAgentOperatingSystem.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Objects/KnownHost/Properties.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Objects/GeoLookup.php"
|
||||
|
@ -212,6 +224,10 @@
|
|||
{
|
||||
"required": true,
|
||||
"file": "Managers/DevicesManager.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "Managers/KnownHostsManager.php"
|
||||
}
|
||||
],
|
||||
"files": [
|
||||
|
|
Loading…
Reference in New Issue