128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
|
|
class Server2Server {
|
|
|
|
|
|
|
|
|
|
public function loadInst() {
|
|
|
|
static $inst = null;
|
|
if ($inst === null) {
|
|
$inst = new self;
|
|
}
|
|
return $inst;
|
|
|
|
}
|
|
|
|
public function module_loaded()
|
|
{
|
|
static $attempted = null;
|
|
|
|
if(file_exists("/etc"))
|
|
{
|
|
$processUser = posix_getpwuid(posix_geteuid());
|
|
} else {
|
|
$processUser = "root";
|
|
}
|
|
|
|
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) {
|
|
\Overwatch\SDK\v1\Utility::Log("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;
|
|
\Overwatch\SDK\v1\Utility::Log("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']){
|
|
#\Overwatch\SDK\v1\Utility::Log("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;
|
|
}
|
|
|
|
} |