2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-12-13 13:29:53 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-13 14:40:48 +01:00
|
|
|
* Proxying AMPHP connector.
|
2019-12-13 13:29:53 +01:00
|
|
|
*
|
|
|
|
* This file is part of MadelineProto.
|
|
|
|
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
2019-12-13 13:29:53 +01:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\MadelineProto;
|
2019-12-13 15:37:57 +01:00
|
|
|
|
2023-01-04 16:04:05 +01:00
|
|
|
use Amp\Cancellation;
|
2019-12-13 14:40:48 +01:00
|
|
|
use Amp\Socket\ConnectContext;
|
2023-01-22 15:39:10 +01:00
|
|
|
use Amp\Socket\Socket;
|
2023-01-04 16:04:05 +01:00
|
|
|
use Amp\Socket\SocketAddress;
|
2023-01-03 21:51:49 +01:00
|
|
|
use Amp\Socket\SocketConnector;
|
2022-12-30 19:21:36 +01:00
|
|
|
use Throwable;
|
2019-12-13 13:29:53 +01:00
|
|
|
|
2023-01-15 12:05:38 +01:00
|
|
|
final class ContextConnector implements SocketConnector
|
2019-12-13 13:29:53 +01:00
|
|
|
{
|
2023-01-11 18:47:27 +01:00
|
|
|
public function __construct(private DoHWrapper $doHWrapper, private LoggerGetter $loggerGetter, private bool $fromDns = false)
|
2019-12-13 13:29:53 +01:00
|
|
|
{
|
|
|
|
}
|
2023-01-22 15:39:10 +01:00
|
|
|
public function connect(SocketAddress|string $uri, ?ConnectContext $context = null, ?Cancellation $cancellation = null): Socket
|
2019-12-13 13:29:53 +01:00
|
|
|
{
|
2023-01-03 22:07:58 +01:00
|
|
|
$ctx = $context ?? new ConnectContext();
|
2023-01-11 18:47:27 +01:00
|
|
|
$ctxs = $this->doHWrapper->generateContexts($uri, $ctx);
|
2023-01-03 22:07:58 +01:00
|
|
|
if (empty($ctxs)) {
|
|
|
|
throw new Exception("No contexts for raw connection to URI {$uri}");
|
|
|
|
}
|
2023-01-04 16:04:05 +01:00
|
|
|
$logger = $this->loggerGetter->getLogger();
|
2023-01-03 22:07:58 +01:00
|
|
|
foreach ($ctxs as $ctx) {
|
|
|
|
try {
|
|
|
|
$ctx->setIsDns($this->fromDns);
|
2023-01-11 21:06:10 +01:00
|
|
|
if ($cancellation) {
|
|
|
|
$ctx->setCancellation($cancellation);
|
|
|
|
}
|
2023-01-03 22:07:58 +01:00
|
|
|
$result = ($ctx->getStream());
|
2023-01-04 16:04:05 +01:00
|
|
|
$logger->logger('OK!', Logger::WARNING);
|
2023-01-03 22:07:58 +01:00
|
|
|
return $result->getSocket();
|
|
|
|
} catch (Throwable $e) {
|
2023-01-04 15:13:55 +01:00
|
|
|
if (\defined('MADELINEPROTO_TEST') && \constant('MADELINEPROTO_TEST') === 'pony') {
|
2023-01-03 22:07:58 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
2023-01-04 16:04:05 +01:00
|
|
|
$logger->logger('Connection failed: '.$e, Logger::ERROR);
|
2019-12-13 13:29:53 +01:00
|
|
|
}
|
2023-01-03 22:07:58 +01:00
|
|
|
}
|
|
|
|
throw new Exception("Could not connect to URI {$uri}");
|
2019-12-13 13:29:53 +01:00
|
|
|
}
|
2019-12-13 13:34:14 +01:00
|
|
|
}
|