Implemented preliminary Server 2 Server communications and added connection-id injection into API commands.
This commit is contained in:
121
lib/modules/Server2Server/Server2Server.API.php
Normal file
121
lib/modules/Server2Server/Server2Server.API.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
class Server2Server {
|
||||
|
||||
|
||||
|
||||
|
||||
public function loadInst() {
|
||||
|
||||
static $inst = null;
|
||||
if ($inst === null) {
|
||||
$inst = new self;
|
||||
}
|
||||
return $inst;
|
||||
|
||||
}
|
||||
|
||||
public function module_loaded()
|
||||
{
|
||||
static $attempted = null;
|
||||
$processUser = posix_getpwuid(posix_geteuid());
|
||||
if($attempted == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if(isset($this->needs_root) and $this->needs_root != "false")
|
||||
{
|
||||
if($processUser['name'] == "root")
|
||||
{
|
||||
self::API_Register();
|
||||
$attempted = true;
|
||||
} else {
|
||||
$attempted = true;
|
||||
}
|
||||
} else {
|
||||
self::API_Register();
|
||||
$attempted = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function API_Register()
|
||||
{
|
||||
apiql::register("!S2S/!QUERY[json]", "Server2Server::BeginS2SConnection");
|
||||
#return true;
|
||||
}
|
||||
|
||||
function BeginS2SConnection($sql)
|
||||
{
|
||||
|
||||
$connectionId = $sql['QUERY']['connection-id'];
|
||||
if(!isset($sql['QUERY']['server']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
# Let's find the client object;
|
||||
global $clients;
|
||||
foreach($clients as $client) {
|
||||
if($client->id == $connectionId) {
|
||||
$connection = $client;
|
||||
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
global $loop;
|
||||
|
||||
$dnsResolverFactory = new React\Dns\Resolver\Factory();
|
||||
$dns = $dnsResolverFactory->createCached(gethostbyname($sql['QUERY']['server']), $loop);
|
||||
|
||||
$S2SConnection = new React\SocketClient\Connector($loop, $dns);
|
||||
$S2SConnection->create($sql['QUERY']['server'], 1337)->then(function (React\Stream\Stream $stream) use ($sql, &$connection, $connectionId) {
|
||||
LogEcho("Connection ".$connectionId." requested connection to Overwatchd instance: ".$sql['QUERY']['server'].":1337, Query: ".$sql['QUERY']['query'],"Server2Server-Comms");
|
||||
$buf = "";
|
||||
$sent = false;
|
||||
$stream->on('data', function($data) use ($sql,&$connection,$connectionId,$stream,&$sent) {
|
||||
|
||||
#$data = $buf;
|
||||
LogEcho("Data to connection $connectionId: $data", "S2S-DEBUG");
|
||||
|
||||
/*
|
||||
if(is_json($data))
|
||||
{
|
||||
|
||||
$data_from_server = json_decode($data, true);
|
||||
$ResponseArray = array("response_type"=>"success",
|
||||
"response_data"=>$data_from_server,
|
||||
"response_code"=>$data_from_server['response_code']);
|
||||
$response = json_encode($ResponseArray);
|
||||
echo(var_dump($response).PHP_EOL);
|
||||
$connection->write($response);
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
$connection->write(apiql::query($data));
|
||||
}*/
|
||||
if(isset($sql['QUERY']['exec']) && $sql['QUERY']['exec'] == true)
|
||||
{
|
||||
$connection->write(apiql::query($data));
|
||||
|
||||
} else {
|
||||
$connection->write($data);
|
||||
}
|
||||
|
||||
$sent = true;
|
||||
});
|
||||
if ( base64_encode(base64_decode($sql['QUERY']['query'], true)) !== $sql['QUERY']['query']){
|
||||
#LogEcho("Raw data from conection: $connection->id, $connection->lastAPI","S2S-COMMS");
|
||||
$sql['QUERY']['query'] = base64_encode($sql['QUERY']['query']);
|
||||
}
|
||||
$stream->write($sql['QUERY']['query']);
|
||||
//$stream->end();
|
||||
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user