Bugfixes for my.telegram.org wrapper

This commit is contained in:
Daniil Gentili 2019-06-22 20:34:15 +02:00
parent 74ab5d2ff5
commit caae5a8fd9
3 changed files with 34 additions and 14 deletions

View File

@ -89,12 +89,12 @@ foreach (['methods', 'constructors'] as $type) {
$url = getUrl($name, $type); $url = getUrl($name, $type);
foreach ($final_new_args as $name => $ttype) { foreach ($final_new_args as $name => $ttype) {
if (!isset($final_old_args[$name])) { if (!isset($final_old_args[$name]) && $name !== 'flags') {
$res .= "Added $name param to $url\n"; $res .= "Added $name param to $url\n";
} }
} }
foreach ($final_old_args as $name => $ttype) { foreach ($final_old_args as $name => $ttype) {
if (!isset($final_new_args[$name])) { if (!isset($final_new_args[$name]) && $name !== 'flags') {
$res .= "Removed $name param from $url\n"; $res .= "Removed $name param from $url\n";
} }
} }

View File

@ -55,6 +55,7 @@ use danog\MadelineProto\Stream\Transport\WssStream;
use danog\MadelineProto\Stream\Transport\WsStream; use danog\MadelineProto\Stream\Transport\WsStream;
use function Amp\call; use function Amp\call;
use function Amp\Socket\Internal\parseUri; use function Amp\Socket\Internal\parseUri;
use Amp\Artax\Cookie\CookieJar;
/** /**
* Manages datacenters. * Manages datacenters.
@ -71,13 +72,14 @@ class DataCenter
private $HTTPClient; private $HTTPClient;
private $DoHClient; private $DoHClient;
private $NonProxiedDoHClient; private $NonProxiedDoHClient;
private $CookieJar;
public function __sleep() public function __sleep()
{ {
return ['sockets', 'curdc', 'dclist', 'settings']; return ['sockets', 'curdc', 'dclist', 'settings'];
} }
public function __magic_construct($API, $dclist, $settings) public function __magic_construct($API, $dclist, $settings, CookieJar $jar = null)
{ {
$this->API = $API; $this->API = $API;
$this->dclist = $dclist; $this->dclist = $dclist;
@ -92,10 +94,11 @@ class DataCenter
unset($this->sockets[$key]); unset($this->sockets[$key]);
} }
} }
$this->HTTPClient = new DefaultClient(new ArrayCookieJar(), new HttpSocketPool(new ProxySocketPool([$this, 'rawConnectAsync']))); $this->CookieJar = $jar ?? new ArrayCookieJar;
$this->HTTPClient = new DefaultClient($this->CookieJar, new HttpSocketPool(new ProxySocketPool([$this, 'rawConnectAsync'])));
$DoHHTTPClient = new DefaultClient( $DoHHTTPClient = new DefaultClient(
new ArrayCookieJar(), $this->CookieJar,
new HttpSocketPool( new HttpSocketPool(
new ProxySocketPool( new ProxySocketPool(
function (string $uri, CancellationToken $token = null, ClientConnectContext $ctx = null) { function (string $uri, CancellationToken $token = null, ClientConnectContext $ctx = null) {
@ -679,6 +682,16 @@ class DataCenter
{ {
return $this->HTTPClient; return $this->HTTPClient;
} }
/**
* Get Artax async HTTP client.
*
* @return \Amp\Artax\CookieJar
*/
public function getCookieJar(): CookieJar
{
return $this->CookieJar;
}
/** /**
* Get DNS over HTTPS async DNS client. * Get DNS over HTTPS async DNS client.
* *

View File

@ -19,6 +19,7 @@
namespace danog\MadelineProto; namespace danog\MadelineProto;
use Amp\Artax\Request; use Amp\Artax\Request;
use Amp\Artax\Cookie\ArrayCookieJar;
/** /**
* Wrapper for my.telegram.org. * Wrapper for my.telegram.org.
@ -29,16 +30,16 @@ class MyTelegramOrgWrapper
private $logged = false; private $logged = false;
private $hash = ''; private $hash = '';
private $token;
private $number; private $number;
private $creation_hash; private $creation_hash;
private $settings = []; private $settings = [];
private $async = true; private $async = true;
private $jar;
const MY_TELEGRAM_URL = 'https://my.telegram.org'; const MY_TELEGRAM_URL = 'https://my.telegram.org';
public function __sleep() public function __sleep()
{ {
return ['logged', 'hash', 'token', 'number', 'creation_hash', 'settings', 'async']; return ['logged', 'hash', 'number', 'creation_hash', 'settings', 'async', 'jar'];
} }
public function __construct($settings = []) public function __construct($settings = [])
@ -52,6 +53,9 @@ class MyTelegramOrgWrapper
if ($this->settings === null) { if ($this->settings === null) {
$this->settings = []; $this->settings = [];
} }
if (!$this->jar) {
$this->jar = new ArrayCookieJar;
}
$this->settings = MTProto::getSettings($this->settings); $this->settings = MTProto::getSettings($this->settings);
$this->datacenter = new DataCenter( $this->datacenter = new DataCenter(
new class($this->settings) new class($this->settings)
@ -62,7 +66,8 @@ class MyTelegramOrgWrapper
} }
}, },
[], [],
$this->settings['connection_settings'] $this->settings['connection_settings'],
$this->jar
); );
} }
@ -95,6 +100,7 @@ class MyTelegramOrgWrapper
$response = yield $this->datacenter->getHTTPClient()->request($request); $response = yield $this->datacenter->getHTTPClient()->request($request);
$result = yield $response->getBody(); $result = yield $response->getBody();
switch ($result) { switch ($result) {
case 'true': case 'true':
//Logger::log(['Login OK'], Logger::VERBOSE); //Logger::log(['Login OK'], Logger::VERBOSE);
@ -103,8 +109,6 @@ class MyTelegramOrgWrapper
throw new Exception($result); throw new Exception($result);
} }
$this->token = explode(';', explode('stel_token=', $response->getHeader('Set-Cookie'))[1])[0];
return $this->logged = true; return $this->logged = true;
} }
@ -126,13 +130,16 @@ class MyTelegramOrgWrapper
$title = explode('</title>', explode('<title>', $result)[1])[0]; $title = explode('</title>', explode('<title>', $result)[1])[0];
switch ($title) { switch ($title) {
case 'App configuration':return true; case 'App configuration':
return true;
case 'Create new application': case 'Create new application':
$this->creation_hash = explode('"/>', explode('<input type="hidden" name="hash" value="', $result)[1])[0]; $this->creation_hash = explode('"/>', explode('<input type="hidden" name="hash" value="', $result)[1])[0];
return false; return false;
} }
$this->logged = false;
throw new Exception($title); throw new Exception($title);
} }
@ -177,7 +184,7 @@ class MyTelegramOrgWrapper
$result = yield $response->getBody(); $result = yield $response->getBody();
if ($result) { if ($result) {
throw new Exception($result); throw new Exception(html_entity_decode($result));
} }
$request = new Request(self::MY_TELEGRAM_URL.'/apps'); $request = new Request(self::MY_TELEGRAM_URL.'/apps');