Fix connection to proxies

This commit is contained in:
Daniil Gentili 2019-10-28 17:08:04 +01:00
parent d7306ab713
commit e9e66f3835
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 41 additions and 12 deletions

View File

@ -541,6 +541,7 @@ class Connection extends Session
$this->API->logger->logger($e); $this->API->logger->logger($e);
} }
} }
$this->shared->signalDisconnect($this->id);
$this->API->logger->logger("Disconnected from DC {$this->datacenterId}"); $this->API->logger->logger("Disconnected from DC {$this->datacenterId}");
} }

View File

@ -555,7 +555,7 @@ class DataCenter
return true; return true;
} catch (\Throwable $e) { } catch (\Throwable $e) {
if (\defined('MADELINEPROTO_TEST') && MADELINEPROTO_TEST === 'pony') { if (\defined(\MADELINEPROTO_TEST::class) && MADELINEPROTO_TEST === 'pony') {
throw $e; throw $e;
} }
$this->API->logger->logger('Connection failed: '.$e->getMessage(), \danog\MadelineProto\Logger::ERROR); $this->API->logger->logger('Connection failed: '.$e->getMessage(), \danog\MadelineProto\Logger::ERROR);

View File

@ -397,14 +397,37 @@ class DataCenterConnection implements JsonSerializable
$ctx = $this->ctx->getCtx(); $ctx = $this->ctx->getCtx();
$count += $previousCount = \count($this->connections); $count += $previousCount = \count($this->connections);
for ($x = $previousCount; $x < $count; $x++) { 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->availableConnections[$x] = 0;
$this->connections[$x] = new Connection();
$this->connections[$x]->setExtra($this, $x);
yield $this->connections[$x]->connect($ctx);
$ctx = $this->ctx->getCtx(); $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. * Close all connections to DC.
* *
@ -418,18 +441,11 @@ class DataCenterConnection implements JsonSerializable
$this->robinLoop = null; $this->robinLoop = null;
} }
$before = \count($this->backup); $before = \count($this->backup);
$list = '';
foreach ($this->connections as $connection) { foreach ($this->connections as $connection) {
$backup = $connection->backupSession();
foreach ($backup as $message) {
$list .= $message['_'] ?? '-';
$list .= ', ';
}
$this->backup = \array_merge($this->backup, $backup);
$connection->disconnect(); $connection->disconnect();
} }
$count = \count($this->backup) - $before; $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->connections = [];
$this->availableConnections = []; $this->availableConnections = [];

View File

@ -32,6 +32,17 @@ use danog\MadelineProto\Stream\RawProxyStreamInterface;
*/ */
class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface 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; use RawStream;
private $extra; private $extra;
@ -105,6 +116,7 @@ class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterfac
$rep = \ord(yield $buffer->bufferRead(1)); $rep = \ord(yield $buffer->bufferRead(1));
if ($rep !== 0) { if ($rep !== 0) {
$rep = self::REPS[$rep] ?? $rep;
throw new \danog\MadelineProto\Exception("Wrong SOCKS5 rep: $rep"); throw new \danog\MadelineProto\Exception("Wrong SOCKS5 rep: $rep");
} }