clrghouz/app/Classes/Node.php

281 lines
6.3 KiB
PHP
Raw Normal View History

2021-04-01 10:59:15 +00:00
<?php
namespace App\Classes;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use App\Models\Address;
2021-04-01 10:59:15 +00:00
/**
* Object representing the node we are communicating with
*
* @property int aka_authed
* @property int aka_num
* @property string ftn
* @property string password
* @property Carbon node_time
* @property int session_time
* @property int ver_major
* @property int ver_minor
*/
class Node
{
// Remote Version
private int $version_major = 0;
private int $version_minor = 0;
private Carbon $start_time; // The time our connection started
// @todo Change this to Carbon
private string $node_time; // Current node's time
private Collection $ftns; // The FTNs of the remote system
private Collection $ftns_authed; // The FTNs we have validated
2021-07-17 05:48:07 +00:00
private bool $authed; // Have we authenticated the remote.
2021-04-01 10:59:15 +00:00
private int $options; // This nodes capabilities/options
public function __construct()
{
$this->options = 0;
2021-07-17 05:48:07 +00:00
$this->authed = FALSE;
2021-04-01 10:59:15 +00:00
$this->start_time = Carbon::now();
$this->ftns = collect();
$this->ftns_authed = collect();
}
/**
* @throws Exception
*/
public function __get($key)
{
switch ($key) {
case 'address':
return ($x=$this->ftns_authed)->count() ? $x->first() : $this->ftns->first();
2021-04-01 10:59:15 +00:00
// Number of AKAs the remote has
case 'aka_num':
return $this->ftns->count();
2021-07-17 05:48:07 +00:00
// The authenticated remote addresses
case 'aka_remote_authed':
2021-07-17 05:48:07 +00:00
return $this->ftns_authed;
case 'aka_remote':
return $this->ftns;
2021-07-17 05:48:07 +00:00
// Have we authenticated the remote
2021-04-01 10:59:15 +00:00
case 'aka_authed':
2021-07-17 05:48:07 +00:00
return $this->authed;
2021-04-01 10:59:15 +00:00
// The nodes password
case 'password':
2021-07-17 05:48:07 +00:00
return ($this->ftns->count() && ($x=$this->ftns->first()->session('sespass'))) ? $x : '-';
2021-04-01 10:59:15 +00:00
// Return how long our session has been connected
case 'session_time':
return Carbon::now()->diffInSeconds($this->start_time);
case 'system':
case 'sysop':
case 'location':
case 'phone':
case 'flags':
case 'message':
case 'files':
case 'netmail':
// The current session speed
case 'speed':
// The time our session started.
case 'start_time':
case 'software':
return $this->{$key};
// Client version
case 'ver_major':
return $this->version_major;
case 'ver_minor':
return $this->version_minor;
default:
throw new Exception('Unknown key: '.$key);
}
}
/**
* @throws Exception
*/
public function __set($key,$value)
{
switch ($key) {
case 'ftn':
if (! is_object($value) OR ! $value instanceof Address)
throw new Exception('Not an Address object: '.(is_object($value) ? get_class($value) : serialize($value)));
2021-04-01 10:59:15 +00:00
// Ignore any duplicate FTNs that we get
if ($this->ftns->search(function($item) use ($value) { return $item->id === $value->id; }) !== FALSE) {
Log::debug(sprintf('%s: - Ignoring Duplicate FTN [%s]',__METHOD__,$value->ftn));
break;
}
$this->ftns->push($value);
2021-04-01 10:59:15 +00:00
break;
case 'system':
case 'sysop':
case 'location':
case 'phone':
case 'flags':
case 'message':
case 'files':
case 'netmail':
case 'software':
case 'speed':
case 'start_time':
case 'node_time':
case 'ver_major':
case 'ver_minor':
$this->{$key} = $value;
break;
default:
throw new Exception('Unknown variable: '.$key);
}
}
/**
* Authenticate the AKAs that the node provided
*
* @param string $password
* @param string $challenge
* @return int
* @throws Exception
*/
public function auth(string $password,string $challenge=''): int
{
Log::debug(sprintf('%s: + Start [%s]',__METHOD__,$password));
// Make sure we havent been here already
if ($this->ftns_authed->count())
throw new Exception('Already authed');
foreach ($this->ftns as $o) {
if (! $o->session('sespass'))
2021-04-01 10:59:15 +00:00
continue;
// If we have challenge, then we are doing MD5
$exp_pwd = $challenge ? $this->md5_challenge($o->session('sespass'),$challenge) : $o->session('sespass');
2021-04-01 10:59:15 +00:00
if ($exp_pwd === $password) {
$o->system->last_session = Carbon::now();
$o->system->save();
2021-04-01 10:59:15 +00:00
$this->ftns_authed->push($o);
2021-07-17 05:48:07 +00:00
$this->authed = TRUE;
}
2021-04-01 10:59:15 +00:00
}
Log::debug(sprintf('%s: = End [%d]',__METHOD__,$this->ftns_authed->count()));
return $this->ftns_authed->count();
}
/**
* When we originate a connection and need to send our MD5 Challenge response
*
* @param string $challenge
* @return string
* @throws Exception
*/
public function get_md5chal(string $challenge): string
{
return $this->md5_challenge($this->password,$challenge);
}
/**
* Return the remotes BINKP version as a int
*
* @return int
*/
public function get_versionint(): int
{
return $this->ver_major*100+$this->ver_minor;
}
/**
* Calculate the response to an MD5 challenge, using the nodes password
*
* @param $pwd
* @param $challenge
* @return string
*/
private function md5_challenge($pwd,$challenge): string
{
$x = $pwd.str_repeat(chr(0x00),64-strlen($pwd));
return md5($this->str_xor($x,0x5c).md5($this->str_xor($x,0x36).$challenge,true));
}
/**
* When we originate a call to a node, we need to store the node we are connecting with in the ftns_authed, so
* authentication proceeds when we send our M_pwd
*
* @param Address $o
2021-04-01 10:59:15 +00:00
*/
public function originate(Address $o): void
2021-04-01 10:59:15 +00:00
{
$this->ftns_authed->push($o);
}
/**
* Check that our received FTNs match who we called
*
* @return bool
*/
public function originate_check(): bool
{
2021-07-17 05:48:07 +00:00
// If we have already authed, we wont do it again
if ($this->authed)
return TRUE;
2021-04-01 10:59:15 +00:00
if ($this->ftns_authed->count() !== 1 || ! $this->ftns->count())
return FALSE;
$ftn = $this->ftns_authed->first()->ftn;
return $this->ftns->search(function($item) use ($ftn) {
if ($item->ftn == $ftn) {
$item->system->last_session = Carbon::now();
$item->system->save();
2021-07-17 05:48:07 +00:00
$this->authed = TRUE;
return TRUE;
}
2021-04-01 10:59:15 +00:00
}) !== FALSE;
}
public function optionClear(int $key): void
{
$this->options &= ~$key;
}
public function optionGet(int $key): int
{
return ($this->options & $key);
}
public function optionSet(int $key): void
{
$this->options |= $key;
}
private function str_xor(string $string,int $val): string
{
$result = '';
for ($i=0;$i<strlen($string);$i++)
$result .= chr(ord($string[$i]) ^ $val);
return $result;
}
}