2015-08-03 11:35:01 +02:00
|
|
|
|
<?php
|
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
define('DOCUMENT_ROOT', dirname(__FILE__));
|
|
|
|
|
require_once(DOCUMENT_ROOT.'/bootstrap.php');
|
|
|
|
|
|
|
|
|
|
gc_enable();
|
|
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
|
//memprof_enable();
|
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
|
$bootstrap = new Bootstrap();
|
|
|
|
|
$booted = $bootstrap->boot();
|
|
|
|
|
|
|
|
|
|
$loop = React\EventLoop\Factory::create();
|
|
|
|
|
|
|
|
|
|
$dnsResolverFactory = new React\Dns\Resolver\Factory();
|
|
|
|
|
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
|
|
|
|
|
|
|
|
|
|
$connector = new React\SocketClient\Connector($loop, $dns);
|
|
|
|
|
$stdin = new React\Stream\Stream(STDIN, $loop);
|
|
|
|
|
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." widgets before : ".\sizeToCleanSize(memory_get_usage())."\n");
|
|
|
|
|
|
|
|
|
|
// We load and register all the widgets
|
|
|
|
|
$wrapper = WidgetWrapper::getInstance();
|
|
|
|
|
$wrapper->registerAll(true);
|
|
|
|
|
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." widgets : ".\sizeToCleanSize(memory_get_usage())."\n");
|
|
|
|
|
|
|
|
|
|
$conn = null;
|
|
|
|
|
|
|
|
|
|
$parser = new \Moxl\Parser;
|
|
|
|
|
|
|
|
|
|
$buffer = '';
|
|
|
|
|
|
2015-09-22 08:18:52 +02:00
|
|
|
|
function handleSSLErrors($errno, $errstr) {
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." : ".colorize($errstr, 'red')."\n");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
|
$stdin_behaviour = function ($data) use (&$conn, $loop, &$buffer, &$connector, &$xmpp_behaviour, &$parser) {
|
|
|
|
|
if(substr($data, -1) == "") {
|
|
|
|
|
$messages = explode("", $buffer . substr($data, 0, -1));
|
|
|
|
|
$buffer = '';
|
|
|
|
|
|
|
|
|
|
foreach ($messages as $message) {
|
|
|
|
|
#fwrite(STDERR, colorize($message, 'yellow')." : ".colorize('received from the browser', 'green')."\n");
|
|
|
|
|
|
|
|
|
|
$msg = json_decode($message);
|
|
|
|
|
|
|
|
|
|
if(isset($msg)) {
|
|
|
|
|
if($msg->func == 'message' && $msg->body != '') {
|
|
|
|
|
$msg = $msg->body;
|
|
|
|
|
} elseif($msg->func == 'unregister') {
|
|
|
|
|
\Moxl\Stanza\Stream::end();
|
|
|
|
|
} elseif($msg->func == 'register') {
|
|
|
|
|
$cd = new \Modl\ConfigDAO();
|
|
|
|
|
$config = $cd->get();
|
|
|
|
|
|
2015-08-10 23:42:45 +02:00
|
|
|
|
$port = 5222;
|
|
|
|
|
$dns = \Moxl\Utils::resolveHost($msg->host);
|
2015-09-10 15:41:45 +02:00
|
|
|
|
if(isset($dns->target) && $dns->target != null) $msg->host = $dns->target;
|
|
|
|
|
if(isset($dns->port) && $dns->port != null) $port = $dns->port;
|
2015-08-03 11:35:01 +02:00
|
|
|
|
#fwrite(STDERR, colorize('open a socket to '.$domain, 'yellow')." : ".colorize('sent to XMPP', 'green')."\n");
|
2015-08-10 23:42:45 +02:00
|
|
|
|
$connector->create($msg->host, $port)->then($xmpp_behaviour);
|
2015-08-03 11:35:01 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rpc = new \RPC();
|
|
|
|
|
$rpc->handle_json($msg);
|
|
|
|
|
|
2015-08-19 11:28:28 +02:00
|
|
|
|
$msg = \RPC::commit();
|
2015-08-03 11:35:01 +02:00
|
|
|
|
\RPC::clear();
|
|
|
|
|
|
|
|
|
|
if(!empty($msg)) {
|
2015-08-19 11:28:28 +02:00
|
|
|
|
//echo json_encode($msg)."";
|
|
|
|
|
echo base64_encode(gzcompress(json_encode($msg), 9))."";
|
|
|
|
|
//fwrite(STDERR, colorize(json_encode($msg), 'yellow')." : ".colorize('sent to the browser', 'green')."\n");
|
2015-08-03 11:35:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$xml = \Moxl\API::commit();
|
|
|
|
|
\Moxl\API::clear();
|
|
|
|
|
|
|
|
|
|
if(!empty($xml) && $conn) {
|
|
|
|
|
$conn->write(trim($xml));
|
|
|
|
|
#fwrite(STDERR, colorize(trim($xml), 'yellow')." : ".colorize('sent to XMPP', 'green')."\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$buffer .= $data;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$xmpp_behaviour = function (React\Stream\Stream $stream) use (&$conn, $loop, &$stdin, $stdin_behaviour, $parser) {
|
|
|
|
|
$conn = $stream;
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." : ".colorize('linker launched', 'blue')."\n");
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." launched : ".\sizeToCleanSize(memory_get_usage())."\n");
|
|
|
|
|
|
|
|
|
|
$stdin->removeAllListeners('data');
|
|
|
|
|
$stdin->on('data', $stdin_behaviour);
|
|
|
|
|
|
|
|
|
|
// We define a huge buffer to prevent issues with SSL streams, see https://bugs.php.net/bug.php?id=65137
|
2015-12-15 11:54:49 +01:00
|
|
|
|
$conn->bufferSize = 1024*32;
|
2015-08-03 11:35:01 +02:00
|
|
|
|
$conn->on('data', function($message) use (&$conn, $loop, $parser) {
|
|
|
|
|
if(!empty($message)) {
|
|
|
|
|
$restart = false;
|
|
|
|
|
|
|
|
|
|
if($message == '</stream:stream>') {
|
|
|
|
|
$conn->close();
|
|
|
|
|
$loop->stop();
|
|
|
|
|
} elseif($message == "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
|
|
|
|
|
|| $message == '<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>') {
|
2015-11-13 13:37:39 +01:00
|
|
|
|
$session = \Sessionx::start();
|
2015-08-03 11:35:01 +02:00
|
|
|
|
stream_set_blocking($conn->stream, 1);
|
2015-11-13 13:37:39 +01:00
|
|
|
|
stream_context_set_option($conn->stream, 'ssl', 'SNI_enabled', false);
|
|
|
|
|
stream_context_set_option($conn->stream, 'ssl', 'peer_name', $session->host);
|
2015-08-24 17:50:34 +02:00
|
|
|
|
stream_context_set_option($conn->stream, 'ssl', 'allow_self_signed', true);
|
2015-09-10 15:41:45 +02:00
|
|
|
|
#stream_context_set_option($conn->stream, 'ssl', 'verify_peer_name', false);
|
|
|
|
|
#stream_context_set_option($conn->stream, 'ssl', 'verify_peer', false);
|
2015-11-13 13:37:39 +01:00
|
|
|
|
|
2015-09-22 08:18:52 +02:00
|
|
|
|
set_error_handler('handleSSLErrors');
|
2015-08-03 11:35:01 +02:00
|
|
|
|
$out = stream_socket_enable_crypto($conn->stream, 1, STREAM_CRYPTO_METHOD_TLS_CLIENT);
|
2015-09-22 08:18:52 +02:00
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
|
|
if($out !== true) {
|
|
|
|
|
$loop->stop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." : ".colorize('TLS enabled', 'blue')."\n");
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
|
|
$restart = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#fwrite(STDERR, colorize($message, 'yellow')." : ".colorize('received', 'green')."\n");
|
|
|
|
|
#fwrite(STDERR, colorize(getenv('sid'), 'yellow')." widgets : ".\sizeToCleanSize(memory_get_usage())."\n");
|
|
|
|
|
|
|
|
|
|
\Moxl\API::clear();
|
|
|
|
|
\RPC::clear();
|
|
|
|
|
|
|
|
|
|
if(!$parser->parse($message)) {
|
|
|
|
|
fwrite(STDERR, colorize(getenv('sid'), 'yellow')." ".$parser->getError()."\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($restart) {
|
|
|
|
|
$session = \Sessionx::start();
|
|
|
|
|
\Moxl\Stanza\Stream::init($session->host);
|
|
|
|
|
stream_set_blocking($conn->stream, 0);
|
|
|
|
|
$restart = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msg = \RPC::commit();
|
|
|
|
|
|
|
|
|
|
if(!empty($msg)) {
|
2015-09-10 15:41:45 +02:00
|
|
|
|
//echo json_encode($msg)."";
|
2015-08-03 11:35:01 +02:00
|
|
|
|
echo base64_encode(gzcompress(json_encode($msg), 9))."";
|
2015-08-19 11:28:28 +02:00
|
|
|
|
//fwrite(STDERR, colorize(json_encode($msg).' '.strlen($msg), 'yellow')." : ".colorize('sent to browser', 'green')."\n");
|
2015-08-03 11:35:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
|
\RPC::clear();
|
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
|
$xml = \Moxl\API::commit();
|
|
|
|
|
|
|
|
|
|
if(!empty($xml)) {
|
|
|
|
|
$conn->write(trim($xml));
|
|
|
|
|
#fwrite(STDERR, colorize(trim($xml), 'yellow')." : ".colorize('sent to XMPP', 'green')."\n");
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
|
\Moxl\API::clear();
|
|
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
|
$loop->tick();
|
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
|
gc_collect_cycles();
|
2015-12-15 11:54:49 +01:00
|
|
|
|
//fwrite(STDERR, colorize(getenv('sid'), 'yellow')." end data : ".\sizeToCleanSize(memory_get_usage())."\n");
|
|
|
|
|
//memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));
|
2015-11-13 13:37:39 +01:00
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$conn->on('error', function($msg) use ($conn, $loop) {
|
|
|
|
|
#fwrite(STDERR, colorize(serialize($msg), 'red')." : ".colorize('error', 'green')."\n");
|
|
|
|
|
$loop->stop();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$conn->on('close', function($msg) use ($conn, $loop) {
|
|
|
|
|
#fwrite(STDERR, colorize(serialize($msg), 'red')." : ".colorize('closed', 'green')."\n");
|
|
|
|
|
$loop->stop();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// And we say that we are ready !
|
|
|
|
|
$obj = new \StdClass;
|
|
|
|
|
$obj->func = 'registered';
|
|
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
|
fwrite(STDERR, 'registered');
|
|
|
|
|
|
2015-08-19 11:28:28 +02:00
|
|
|
|
//fwrite(STDERR, colorize(json_encode($obj).' '.strlen($obj), 'yellow')." : ".colorize('obj sent to browser', 'green')."\n");
|
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
|
echo base64_encode(gzcompress(json_encode($obj), 9))."";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$stdin->on('data', $stdin_behaviour);
|
|
|
|
|
$stdin->on('error', function() use($loop) { $loop->stop(); } );
|
|
|
|
|
$stdin->on('close', function() use($loop) { $loop->stop(); } );
|
|
|
|
|
|
|
|
|
|
$loop->run();
|