1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 06:34:41 +01:00

Allow binding to only ipv6 or only ipv4 addresses

This commit is contained in:
Daniil Gentili 2023-09-05 16:12:02 +02:00
parent b82ddab4c0
commit 0fbf22f956
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 15 additions and 3 deletions

View File

@ -580,6 +580,7 @@ final class Connection
$this->API->logger->logger("Reconnecting DC {$this->datacenterId}"); $this->API->logger->logger("Reconnecting DC {$this->datacenterId}");
$this->disconnect(true); $this->disconnect(true);
$this->shared->connect($this->id); $this->shared->connect($this->id);
$this->connect();
} }
/** /**
* Get name. * Get name.

View File

@ -24,6 +24,8 @@ use Amp\Dns\DnsResolver;
use Amp\Http\Client\HttpClient; use Amp\Http\Client\HttpClient;
use Amp\Http\Client\Request; use Amp\Http\Client\Request;
use Amp\Socket\ConnectContext; use Amp\Socket\ConnectContext;
use Amp\Socket\InternetAddress;
use Amp\Socket\InternetAddressVersion;
use Amp\Sync\LocalKeyedMutex; use Amp\Sync\LocalKeyedMutex;
use AssertionError; use AssertionError;
use danog\MadelineProto\Stream\Common\BufferedRawStream; use danog\MadelineProto\Stream\Common\BufferedRawStream;
@ -131,10 +133,9 @@ final class DataCenter
* Generate contexts. * Generate contexts.
* *
* @param integer $dc_number DC ID to generate contexts for * @param integer $dc_number DC ID to generate contexts for
* @param ConnectContext $context Connection context
* @return non-empty-list<ConnectionContext> * @return non-empty-list<ConnectionContext>
*/ */
private function generateContexts(int $dc_number, ?ConnectContext $context = null): array private function generateContexts(int $dc_number): array
{ {
$test = $this->getSettings()->getTestMode() ? 'test' : 'main'; $test = $this->getSettings()->getTestMode() ? 'test' : 'main';
$ipv6 = $this->getSettings()->getIpv6() ? 'ipv6' : 'ipv4'; $ipv6 = $this->getSettings()->getIpv6() ? 'ipv6' : 'ipv4';
@ -231,7 +232,14 @@ final class DataCenter
} }
$combos = \array_unique($combos, SORT_REGULAR); $combos = \array_unique($combos, SORT_REGULAR);
$context ??= (new ConnectContext())->withConnectTimeout($this->getSettings()->getTimeout())->withBindTo($this->getSettings()->getBindTo()); $bind = $this->getSettings()->getBindTo();
$onlyIPv6 = null;
if ($bind !== null) {
$onlyIPv6 = InternetAddress::fromString($bind)->getVersion() === InternetAddressVersion::IPv6
? 'ipv6'
: 'ipv4';
}
$context = (new ConnectContext())->withConnectTimeout($this->getSettings()->getTimeout())->withBindTo($bind);
foreach ($combos as $combo) { foreach ($combos as $combo) {
foreach ([true, false] as $useDoH) { foreach ([true, false] as $useDoH) {
$ipv6Combos = [ $ipv6Combos = [
@ -239,6 +247,9 @@ final class DataCenter
$this->getSettings()->getIpv6() ? 'ipv4' : 'ipv6' $this->getSettings()->getIpv6() ? 'ipv4' : 'ipv6'
]; ];
foreach ($ipv6Combos as $ipv6) { foreach ($ipv6Combos as $ipv6) {
if ($onlyIPv6 !== null && $onlyIPv6 !== $ipv6) {
continue;
}
if (!isset($this->API->dcList[$test][$ipv6][$dc_number]['ip_address'])) { if (!isset($this->API->dcList[$test][$ipv6][$dc_number]['ip_address'])) {
continue; continue;
} }