From 1b84c2e683e44527186b4e4c662004fb6e969dea Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 11 May 2019 15:58:54 +0200 Subject: [PATCH] Async factorization --- docs | 2 +- .../MTProtoTools/AuthKeyHandler.php | 48 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/docs b/docs index 0fcff8e5..e8e22ca1 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 0fcff8e5a31af300511949c4dbafc7be6c0d4dd7 +Subproject commit e8e22ca165ac1bc8a7435d5d55b3e94b1883d9d8 diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 32dc11d0..34e477b3 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -131,11 +131,8 @@ trait AuthKeyHandler if (!$pq->equals($p->multiply($q))) { $this->logger->logger('Automatic factorization failed, trying wolfram module', \danog\MadelineProto\Logger::ERROR); - if (!extension_loaded('curl')) { - throw new Exception(['extension', 'curl']); - } - $p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::wolfram_single($pq->__toString())); + $p = new \phpseclib\Math\BigInteger(yield $this->wolfram_single_async($pq->__toString())); if (!$p->equals(\danog\MadelineProto\Magic::$zero)) { $q = $pq->divide($p)[0]; if ($p->compare($q) > 0) { @@ -523,6 +520,49 @@ trait AuthKeyHandler throw new \danog\MadelineProto\SecurityException('An error occurred while binding temporary and permanent authorization keys.'); } + public function wolfram_single_async($what) + { + $code = yield (yield $this->datacenter->getHTTPClient()->request('http://www.wolframalpha.com/api/v1/code'))->getBody(); + $query = 'Do prime factorization of '.$what; + $params = [ + 'async' => true, + 'banners' => 'raw', + 'debuggingdata' => false, + 'format' => 'moutput', + 'formattimeout' => 8, + 'input' => $query, + 'output' => 'JSON', + 'proxycode' => json_decode($code, true)['code'], + ]; + $url = 'https://www.wolframalpha.com/input/json.jsp?'.http_build_query($params); + + $request = (new Request($url))->withHeader('referer', 'https://www.wolframalpha.com/input/?i='.urlencode($query)); + + $res = json_decode(yield (yield $this->datacenter->getHTTPClient()->request($request))->getBody(), true); + if (!isset($res['queryresult']['pods'])) { + return false; + } + $fres = false; + foreach ($res['queryresult']['pods'] as $cur) { + if ($cur['id'] === 'Divisors') { + $fres = explode(', ', preg_replace(["/{\d+, /", "/, \d+}$/"], '', $cur['subpods'][0]['moutput'])); + break; + } + } + if (is_array($fres)) { + $fres = $fres[0]; + + $newval = intval($fres); + if (is_int($newval)) { + $fres = $newval; + } + + return $fres; + } + + return false; + } + // Creates authorization keys public function init_authorization() {