BMWX6
AG епты
PHP:
class Server
{
private $base = NULL;
private $event = NULL;
private $id = 0;
private $ip = '127.0.0.1';
private $port = '1511';
private $connections = array();
private $buffers = array();
private $pem = './server.pem';
public function __construct()
{
$this->base = event_base_new();
$this->event = event_new();
$errno = 0;
$errstr = '';
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'local_cert', $this->pem);
stream_context_set_option($context, 'ssl', 'ciphers', 'ALL');
stream_context_set_option($context, 'ssl', 'verify_peer', false);
$socket = stream_socket_server('ssl://'.$this->ip.':'.$this->port, $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
stream_set_blocking($socket, 0);
event_set($this->event, $socket, EV_READ | EV_PERSIST, array($this, 'accept'), $this->base);
event_base_set($this->event, $this->base);
event_add($this->event);
}
public function accept($socket, $flag, $base)
{
$this->id ++;
$connection = stream_socket_accept($socket);
stream_set_blocking($connection, 0);
$buffer = event_buffer_new($connection, array($this, 'onRead'), NULL, array($this, 'onError'), $this->id);
event_buffer_base_set($buffer, $this->base);
event_buffer_timeout_set($buffer, 30, 30);
event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
event_buffer_priority_set($buffer, 10);
event_buffer_enable($buffer, EV_READ | EV_PERSIST);
$this->connections[$this->id] = $connection;
$this->buffers[$this->id] = $buffer;
}
private function onError($buffer, $error, $id)
{
event_buffer_disable($this->buffers[$id], EV_READ | EV_WRITE);
event_buffer_free($this->buffers[$id]);
fclose($this->connections[$id]);
unset($this->buffers[$id], $this->connections[$id]);
}
private function onRead($buffer, $id)
{
while($read = event_buffer_read($buffer, 256))
{
echo $read;
}
}
public function execute()
{
event_base_loop($this->base);
}
}
$server = new Server();
$server->execute()
PHP:
v@ubuntu:/var/www$ php index.php |hexdump -C
00000000 80 7d 01 03 01 00 54 00 00 00 20 00 00 39 00 00 |.}....T... ..9..|
00000010 38 00 00 35 00 00 16 00 00 13 00 00 0a 07 00 c0 |8..5............|
00000020 00 00 33 00 00 32 00 00 2f 00 00 07 05 00 80 03 |..3..2../.......|
00000030 00 80 00 00 05 00 00 04 01 00 80 00 00 15 00 00 |................|
00000040 12 00 00 09 06 00 40 00 00 14 00 00 11 00 00 08 |......@.........|
00000050 00 00 06 04 00 80 00 00 03 02 00 80 00 00 ff 46 |...............F|
00000060 cd 29 c7 99 a4 1b 82 34 c6 76 ec 50 97 61 b3 37 |.).....4.v.P.a.7|
PHP:
v@ubuntu:/var/www$ socat OPENSSL-LISTEN:1511,cert=server.pem,verify=0 - |hexdump -C
00000000 a5 01 03 44 e6 bd 8d bc c5 c1 70 32 b3 81 03 a0 |...D......p2....|
00000010 61 44 40 6a 5e 32 84 ad d1 94 55 c7 fb 1e d4 d3 |aD@j^2....U.....|
00000020 43 7d 5a b8 16 da a6 44 9f 7a 3a f9 a4 23 3d ef |C}Z....D.z:..#=.|
2012/01/19 19:26:25 socat[6762] E SSL_read(): Connection reset by peer
00000030 ba 73 c4 03 a3 2f 1a 08 |.s.../..|
00000038