Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Docs, refactoring, SSL

  • Loading branch information...
commit 8f923c36e2b150b9e3eb632d6aeab59d82d35aeb 1 parent ed54857
@kakserpom authored
View
19 lib/BoundSocket.php
@@ -189,6 +189,16 @@ protected function initSSLContext() {
$this->errorneous = true;
return false;
}
+ if (!FS::checkFileReadable($this->certfile) || !FS::checkFileReadable($this->pkfile)) {
+ Daemon::log('Couldn\'t read ' . $this->certfile . ' or ' . $this->pkfile . ' file. To generate a key' . PHP_EOL
+ . 'and self-signed certificate, run' . PHP_EOL
+ . ' openssl genrsa -out '.escapeshellarg($this->pkfile).' 2048' . PHP_EOL
+ . ' openssl req -new -key '.escapeshellarg($this->pkfile).' -out cert.req' . PHP_EOL
+ . ' openssl x509 -req -days 365 -in cert.req -signkey '.escapeshellarg($this->pkfile).' -out '.escapeshellarg($this->certfile));
+
+ return;
+ }
+
$this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, [
EventSslContext::OPT_LOCAL_CERT => $this->certfile,
EventSslContext::OPT_LOCAL_PK => $this->pkfile,
@@ -203,7 +213,7 @@ protected function initSSLContext() {
* @param ConnectionPool
* @return void
*/
- public function attachTo($pool) {
+ public function attachTo(ConnectionPool $pool) {
$this->pool = $pool;
$this->pool->attachBound($this);
}
@@ -240,7 +250,9 @@ public function enable() {
-1,
$this->fd
);
- //EventUtil::getSocketName($this->ev, $this->locHost, $this->locPort);
+ if ($this->ev) {
+ $this->ev->getSocketName($this->locHost, $this->locPort);
+ }
} else {
$this->ev->enable();
}
@@ -258,7 +270,7 @@ public function enable() {
}
}
- public function onListenerAcceptEv($listener, $fd, $addrPort, $ctx) {
+ public function onListenerAcceptEv(EventListener $listener, $fd, $addrPort, $ctx) {
$class = $this->pool->connectionClass;
$conn = new $class(null, $this->pool);
$conn->setParentSocket($this);
@@ -303,7 +315,6 @@ public function close() {
}
if ($this->fd !== null) {
if ($this->listenerMode) {
- //$this->fd->free();
$this->fd = null;
} else {
socket_close($this->fd);
View
60 lib/Connection.php
@@ -155,6 +155,17 @@ class Connection extends IOStream {
protected $allowselfsigned = true;
/**
+ * Context cache
+ * @var CappedCacheStorage
+ */
+ protected static $contextCache;
+
+ /**
+ * Context cache size
+ * @var number
+ */
+ protected static $contextCacheSize = 64;
+ /**
* Connected?
* @return boolean
*/
@@ -162,6 +173,9 @@ public function isConnected() {
return $this->connected;
}
+ protected static function initContextCache() {
+
+ }
/**
* Tries to obtain peer name.
* @return boolean Success
@@ -365,16 +379,28 @@ protected function initSSLContext() {
EventSslContext::OPT_VERIFY_PEER => $this->verifypeer,
EventSslContext::OPT_ALLOW_SELF_SIGNED => $this->allowselfsigned,
];
- if ($this->certfile !== null) {
- $params[EventSslContext::OPT_LOCAL_CERT] = $this->certfile;
- }
- if ($this->pkfile !== null) {
- $params[EventSslContext::OPT_LOCAL_PK] = $this->pkfile;
- }
- if ($this->passphrase !== null) {
- $params[EventSslContext::OPT_PASSPHRASE] = $this->passphrase;
- }
- return new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $params);
+ if ($this->certfile !== null) {
+ $params[EventSslContext::OPT_LOCAL_CERT] = $this->certfile;
+ }
+ if ($this->pkfile !== null) {
+ $params[EventSslContext::OPT_LOCAL_PK] = $this->pkfile;
+ }
+ if ($this->passphrase !== null) {
+ $params[EventSslContext::OPT_PASSPHRASE] = $this->passphrase;
+ }
+ $hash = igbinary_serialize($params);
+ if (!self::$contextCache) {
+ self::$contextCache = new CappedCacheStorageHits(self::$contextCacheSize);
+ } elseif ($ctx = self::$contextCache->getValue($hash)) {
+ return $ctx;
+ }
+ $ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, $params);
+ self::$contextCache->put($hash, $ctx);
+ return $ctx;
+ }
+
+ public function getUrl() {
+ return $this->url;
}
/**
@@ -389,13 +415,21 @@ public function connect($url, $cb = null) {
if (!$u) {
return false;
}
- if (!isset($u['port']) && isset($this->pool->config->port->value)) {
- $u['port'] = $this->pool->config->port->value;
+ $this->importParams();
+ if (!isset($u['port'])) {
+ if ($this->ssl) {
+ if (isset($this->pool->config->sslport->value)) {
+ $u['port'] = $this->pool->config->sslport->value;
+ }
+ } else {
+ if (isset($this->pool->config->port->value)) {
+ $u['port'] = $this->pool->config->port->value;
+ }
+ }
}
if (isset($u['user'])) {
$this->user = $u['user'];
}
- $this->importParams();
if ($this->ssl) {
$this->setContext($this->initSSLContext(), EventBufferEvent::SSL_CONNECTING);
View
9 lib/FS.php
@@ -99,6 +99,15 @@ public static function initEvent() {
self::$ev->add();
}
+ /**
+ * Checks if file exists and readable
+ * @param string Path
+ * @return boolean Exists and readable?
+ */
+ public static function checkFileReadable($path) {
+ return is_file($path) && is_readable($path);
+
+ }
/**
* Block until all FS events are completed
View
26 lib/HTTPClient.php
@@ -12,11 +12,25 @@ class HTTPClient extends NetworkClient {
* @return array|false
*/
protected function getConfigDefaults() {
- return array(
- // @todo add description strings
+ return [
+ /**
+ * Default port
+ * @var integer
+ */
'port' => 80,
+
+ /**
+ * Default SSL port
+ * @var integer
+ */
+ 'sslport' => 443,
+
+ /**
+ * Send User-Agent header?
+ * @var boolean
+ */
'expose' => 1,
- );
+ ];
}
public function get($url, $params) {
@@ -28,9 +42,9 @@ public function get($url, $params) {
}
$ssl = $params['scheme'] === 'https';
$this->getConnection(
- 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null),
- function($conn) use ($url, $params) {
- $conn->get($url, $params);
+ 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($ssl ? '#ssl' : ''),
+ function($conn) use ($url, $data, $params) {
+ $conn->get($url, $data, $params);
}
);
}
View
23 lib/HTTPServerConnection.php
@@ -51,17 +51,18 @@ protected function httpReadFirstline() {
if (isset($u['host'])) {
$this->req->attrs->server['HTTP_HOST'] = $u['host'];
}
- $this->req->attrs->server['REQUEST_METHOD'] = $e[0];
- $this->req->attrs->server['REQUEST_TIME'] = time();
- $this->req->attrs->server['REQUEST_TIME_FLOAT'] = microtime(true);
- $this->req->attrs->server['REQUEST_URI'] = $u['path'] . (isset($u['query']) ? '?' . $u['query'] : '');
- $this->req->attrs->server['DOCUMENT_URI'] = $u['path'];
- $this->req->attrs->server['PHP_SELF'] = $u['path'];
- $this->req->attrs->server['QUERY_STRING'] = isset($u['query']) ? $u['query'] : null;
- $this->req->attrs->server['SCRIPT_NAME'] = $this->req->attrs->server['DOCUMENT_URI'] = isset($u['path']) ? $u['path'] : '/';
- $this->req->attrs->server['SERVER_PROTOCOL'] = isset($e[2]) ? $e[2] : 'HTTP/1.1';
- $this->req->attrs->server['REMOTE_ADDR'] = $this->addr;
- $this->req->attrs->server['REMOTE_PORT'] = $this->port;
+ $srv = &$this->req->attrs->server;
+ $srv['REQUEST_METHOD'] = $e[0];
+ $srv['REQUEST_TIME'] = time();
+ $srv['REQUEST_TIME_FLOAT'] = microtime(true);
+ $srv['REQUEST_URI'] = $u['path'] . (isset($u['query']) ? '?' . $u['query'] : '');
+ $srv['DOCUMENT_URI'] = $u['path'];
+ $srv['PHP_SELF'] = $u['path'];
+ $srv['QUERY_STRING'] = isset($u['query']) ? $u['query'] : null;
+ $srv['SCRIPT_NAME'] = $srv['DOCUMENT_URI'] = isset($u['path']) ? $u['path'] : '/';
+ $srv['SERVER_PROTOCOL'] = isset($e[2]) ? $e[2] : 'HTTP/1.1';
+ $srv['REMOTE_ADDR'] = $this->addr;
+ $srv['REMOTE_PORT'] = $this->port;
return true;
}
View
29 lib/IOStream.php
@@ -164,6 +164,12 @@
protected $ctxMode;
/**
+ * SSL?
+ * @var boolean
+ */
+ protected $ssl = false;
+
+ /**
* IOStream constructor
* @param resource File descriptor. Optional.
* @param object Pool. Optional.
@@ -285,15 +291,15 @@ public function setFd($fd, $bev = null) {
if ($this->timeout !== null) {
$this->bev->setTimeouts($this->timeout, $this->timeout);
}
+ if ($this->bevConnect && ($this->fd === null)) {
+ $this->bev->connect($this->addr, false);
+ //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port, EventUtil::AF_UNSPEC);
+ }
if (!$this->bev->enable(Event::READ | Event::WRITE | Event::TIMEOUT | Event::PERSIST)) {
$this->finish();
return;
}
$this->bev->setWatermark(Event::READ, $this->lowMark, $this->highMark);
- if ($this->bevConnect && ($this->fd === null)) {
- $this->bev->connect($this->addr, false);
- //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port, EventUtil::AF_UNSPEC);
- }
init:
if (!$this->inited) {
$this->inited = true;
@@ -661,6 +667,10 @@ public function unsetFd() {
$this->bev = null;
$this->fd = null;
}
+
+ protected function log($m) {
+ Daemon::log(get_class().': '.$m);
+ }
/**
* Called when the connection has got new data
@@ -668,6 +678,9 @@ public function unsetFd() {
* @return void
*/
public function onReadEv($bev) {
+ if (Daemon::$config->logevents->value) {
+ $this->log(' onReadEv called');
+ }
if (!$this->ready) {
$this->wRead = true;
return;
@@ -714,6 +727,9 @@ public function onWriteOnce($cb) {
* @return void
*/
public function onWriteEv($bev) {
+ if (Daemon::$config->logevents->value) {
+ Daemon::log(get_class().' onWriteEv called');
+ }
$this->writing = false;
if ($this->finished) {
$this->close();
@@ -771,6 +787,11 @@ public function onStateEv($bev, $events) {
if ($errno !== 0) {
trigger_error('Socket error #' . $errno . ':' . EventUtil::getLastSocketError(), E_USER_NOTICE);
}
+ if ($this->ssl) {
+ while ($err = $bev->sslError()) {
+ trigger_error('EventBufferEvent SSL error: ' . $err . PHP_EOL, E_USER_NOTICE);
+ }
+ }
}
$this->finished = true;
$this->onFinish();
View
2  lib/NetworkClient.php
@@ -148,7 +148,7 @@ public function getConnection($url = null, $cb = null, $pri = 0) {
*/
public function detach($conn) {
parent::detach($conn);
- $this->touchPending($conn->url);
+ $this->touchPending($conn->getUrl());
}
/**
View
0  lib/WebSocketServerConnection.php 100644 → 100755
File mode changed

0 comments on commit 8f923c3

Please sign in to comment.
Something went wrong with that request. Please try again.