-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
54 lines (46 loc) · 1.81 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Session\SessionProvider;
use React\EventLoop\Factory as LoopFactory;
use Socketty\BasicAuthenticator;
use Socketty\BasicAuthorizer;
use Socketty\ClientFactory;
use Socketty\CommandSpawner;
use Socketty\Socketty;
use Socketty\TerminalFactory;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
date_default_timezone_set('Europe/Oslo');
$loop = LoopFactory::create();
$app = new \Ratchet\App('localhost', 8080, '127.0.0.1', $loop);
$handler = new \Monolog\Handler\StreamHandler('php://stdout', \Psr\Log\LogLevel::INFO);
$processor = new \Monolog\Processor\MemoryUsageProcessor();
$logger = new Monolog\Logger('socketty', array($handler), array($processor));
$authenticator = new BasicAuthenticator('username');
$authorizer = new BasicAuthorizer();
$enabledCommands = [
'ssh',
'top',
'ping'
];
$spawner = new CommandSpawner($enabledCommands);
$terminalFactory = new TerminalFactory($loop, $spawner);
$clientFactory = new ClientFactory($loop, $logger, $terminalFactory, $authorizer);
$socketty = new Socketty($logger, $loop, $clientFactory, $authenticator);
$memcached = new Memcached();
$memcached->addServer('localhost', 11211);
$handler = new MemcachedSessionHandler($memcached);
$sessionProvider = new SessionProvider($socketty, $handler, array('name' => 'CUSTOM_NAME'));
declare(ticks = 1);
$sig_handler = function($signal) use($socketty, $logger){
try{
$logger->critical('Server was killed. Stopping new connections and closing existing connections now...');
$socketty->allowNewConnections(false);
$socketty->closeAll();
$logger->critical('All connections now closed. Exiting...');
}finally{
exit;
}
};
pcntl_signal(SIGTERM, $sig_handler);
$app->route('', $sessionProvider);
$app->run();