diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index f583b4160..dfd0cc794 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -541,6 +541,7 @@ class Connection extends Session $this->API->logger->logger($e); } } + $this->shared->signalDisconnect($this->id); $this->API->logger->logger("Disconnected from DC {$this->datacenterId}"); } diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 841f4bf02..8aaa692da 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -555,7 +555,7 @@ class DataCenter return true; } catch (\Throwable $e) { - if (\defined('MADELINEPROTO_TEST') && MADELINEPROTO_TEST === 'pony') { + if (\defined(\MADELINEPROTO_TEST::class) && MADELINEPROTO_TEST === 'pony') { throw $e; } $this->API->logger->logger('Connection failed: '.$e->getMessage(), \danog\MadelineProto\Logger::ERROR); diff --git a/src/danog/MadelineProto/DataCenterConnection.php b/src/danog/MadelineProto/DataCenterConnection.php index e2f804c70..9fb09b497 100644 --- a/src/danog/MadelineProto/DataCenterConnection.php +++ b/src/danog/MadelineProto/DataCenterConnection.php @@ -397,14 +397,37 @@ class DataCenterConnection implements JsonSerializable $ctx = $this->ctx->getCtx(); $count += $previousCount = \count($this->connections); for ($x = $previousCount; $x < $count; $x++) { + $connection = new Connection(); + $connection->setExtra($this, $x); + yield $connection->connect($ctx); + + $this->connections[$x] = $connection; $this->availableConnections[$x] = 0; - $this->connections[$x] = new Connection(); - $this->connections[$x]->setExtra($this, $x); - yield $this->connections[$x]->connect($ctx); $ctx = $this->ctx->getCtx(); } } + /** + * Signal that a connection ID disconnected. + * + * @param integer $id Connection ID + * + * @return void + */ + public function signalDisconnect(int $id) + { + $backup = $this->connections[$id]->backupSession(); + $list = ''; + foreach ($backup as $message) { + $list .= $message['_'] ?? '-'; + $list .= ', '; + } + $this->API->logger->logger("Backed up $list from DC {$this->datacenter}.$id"); + $this->backup = \array_merge($this->backup, $backup); + + unset($this->connections[$id], $this->availableConnections[$id]); + } + /** * Close all connections to DC. * @@ -418,18 +441,11 @@ class DataCenterConnection implements JsonSerializable $this->robinLoop = null; } $before = \count($this->backup); - $list = ''; foreach ($this->connections as $connection) { - $backup = $connection->backupSession(); - foreach ($backup as $message) { - $list .= $message['_'] ?? '-'; - $list .= ', '; - } - $this->backup = \array_merge($this->backup, $backup); $connection->disconnect(); } $count = \count($this->backup) - $before; - $this->API->logger->logger("Backed up $count (added {$list}to $before existing messages) from DC {$this->datacenter}"); + $this->API->logger->logger("Backed up $count, added to $before existing messages) from DC {$this->datacenter}"); $this->connections = []; $this->availableConnections = []; diff --git a/src/danog/MadelineProto/Stream/Proxy/SocksProxy.php b/src/danog/MadelineProto/Stream/Proxy/SocksProxy.php index fe50dba62..197416bfb 100644 --- a/src/danog/MadelineProto/Stream/Proxy/SocksProxy.php +++ b/src/danog/MadelineProto/Stream/Proxy/SocksProxy.php @@ -32,6 +32,17 @@ use danog\MadelineProto\Stream\RawProxyStreamInterface; */ class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface { + const REPS = [ + 0 => 'succeeded', + 1 => 'general SOCKS server failure', + 2 => 'connection not allowed by ruleset', + 3 => 'Network unreachable', + 4 => 'Host unreachable', + 5 => 'Connection refused', + 6 => 'TTL expired', + 7 => 'Command not supported', + 8 => 'Address type not supported' + ]; use RawStream; private $extra; @@ -105,6 +116,7 @@ class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterfac $rep = \ord(yield $buffer->bufferRead(1)); if ($rep !== 0) { + $rep = self::REPS[$rep] ?? $rep; throw new \danog\MadelineProto\Exception("Wrong SOCKS5 rep: $rep"); }