2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-02-24 17:54:13 +01:00
|
|
|
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
/**
|
|
|
|
* AuthKeyHandler module.
|
|
|
|
*
|
|
|
|
* 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>
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
2019-10-31 15:07:35 +01:00
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
*/
|
2018-02-24 17:54:39 +01:00
|
|
|
|
2017-04-01 13:20:57 +02:00
|
|
|
namespace danog\MadelineProto\VoIP;
|
|
|
|
|
2022-12-30 19:21:36 +01:00
|
|
|
use danog\MadelineProto\Exception;
|
|
|
|
use danog\MadelineProto\Lang;
|
|
|
|
use danog\MadelineProto\Logger;
|
2020-07-28 20:39:32 +02:00
|
|
|
use danog\MadelineProto\Loop\Update\UpdateLoop;
|
2022-12-30 19:21:36 +01:00
|
|
|
use danog\MadelineProto\Magic;
|
2020-10-03 15:36:03 +02:00
|
|
|
use danog\MadelineProto\MTProtoTools\Crypt;
|
2023-06-25 15:27:46 +02:00
|
|
|
use danog\MadelineProto\PeerNotInDbException;
|
2022-12-30 19:21:36 +01:00
|
|
|
use danog\MadelineProto\RPCErrorException;
|
|
|
|
use danog\MadelineProto\SecurityException;
|
|
|
|
use danog\MadelineProto\VoIP;
|
|
|
|
use phpseclib3\Math\BigInteger;
|
|
|
|
|
|
|
|
use const STR_PAD_LEFT;
|
2020-03-01 16:31:56 +01:00
|
|
|
|
2017-04-01 13:20:57 +02:00
|
|
|
/**
|
|
|
|
* Manages the creation of the authorization key.
|
|
|
|
*
|
|
|
|
* https://core.telegram.org/mtproto/auth_key
|
|
|
|
* https://core.telegram.org/mtproto/samples-auth_key
|
2023-02-16 18:38:47 +01:00
|
|
|
*
|
|
|
|
* @internal
|
2017-04-01 13:20:57 +02:00
|
|
|
*/
|
|
|
|
trait AuthKeyHandler
|
|
|
|
{
|
2023-01-20 14:36:21 +01:00
|
|
|
private array $calls = [];
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
2020-03-01 16:31:56 +01:00
|
|
|
* Accept call from VoIP instance.
|
2019-12-28 16:07:09 +01:00
|
|
|
*
|
2022-12-30 19:21:36 +01:00
|
|
|
* @param VoIP $instance Call instance
|
2020-03-01 16:31:56 +01:00
|
|
|
* @param array $user User
|
2020-03-03 00:08:39 +01:00
|
|
|
* @internal
|
2019-12-28 16:07:09 +01:00
|
|
|
*/
|
2023-01-13 14:36:10 +01:00
|
|
|
public function acceptCallFrom(VoIP $instance, array $user): ?VoIP
|
2019-05-15 22:08:49 +02:00
|
|
|
{
|
2023-01-13 14:36:10 +01:00
|
|
|
if (!$this->acceptCall($user)) {
|
2023-01-04 12:12:44 +01:00
|
|
|
$instance->discard();
|
2023-01-13 14:36:10 +01:00
|
|
|
return null;
|
2020-03-01 16:31:56 +01:00
|
|
|
}
|
2023-01-04 12:12:44 +01:00
|
|
|
return $instance;
|
2019-05-15 22:08:49 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
2022-12-30 19:21:36 +01:00
|
|
|
* @param VoIP $instance Call instance
|
2020-03-01 16:31:56 +01:00
|
|
|
* @param array $call Call info
|
|
|
|
* @param array $reason Discard reason
|
|
|
|
* @param array $rating Rating
|
|
|
|
* @param boolean $need_debug Needs debug?
|
2020-03-03 00:08:39 +01:00
|
|
|
* @internal
|
2019-12-28 16:07:09 +01:00
|
|
|
*/
|
2023-01-13 14:36:10 +01:00
|
|
|
public function discardCallFrom(VoIP $instance, array $call, array $reason, array $rating = [], bool $need_debug = true): VoIP
|
2019-05-15 22:08:49 +02:00
|
|
|
{
|
2023-01-13 14:36:10 +01:00
|
|
|
$this->discardCall($call, $reason, $rating, $need_debug);
|
2023-01-04 12:12:44 +01:00
|
|
|
return $instance;
|
2019-05-15 22:08:49 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Request VoIP call.
|
|
|
|
*
|
|
|
|
* @param mixed $user User
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
public function requestCall(mixed $user)
|
2017-04-01 13:20:57 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2022-12-30 22:31:20 +01:00
|
|
|
$user = ($this->getInfo($user));
|
2017-07-20 16:20:19 +02:00
|
|
|
if (!isset($user['InputUser']) || $user['InputUser']['_'] === 'inputUserSelf') {
|
2023-06-25 15:27:46 +02:00
|
|
|
throw new PeerNotInDbException();
|
2017-05-02 12:13:27 +02:00
|
|
|
}
|
2017-05-02 12:13:01 +02:00
|
|
|
$user = $user['InputUser'];
|
2023-01-04 15:13:55 +01:00
|
|
|
$this->logger->logger(\sprintf('Calling %s...', $user['user_id']), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$dh_config = ($this->getDhConfig());
|
2023-06-24 18:00:19 +02:00
|
|
|
$this->logger->logger('Generating a...', Logger::VERBOSE);
|
2022-12-30 19:21:36 +01:00
|
|
|
$a = BigInteger::randomRange(Magic::$two, $dh_config['p']->subtract(Magic::$two));
|
2023-06-24 18:00:19 +02:00
|
|
|
$this->logger->logger('Generating g_a...', Logger::VERBOSE);
|
2017-04-01 13:20:57 +02:00
|
|
|
$g_a = $dh_config['g']->powMod($a, $dh_config['p']);
|
2020-10-03 15:36:03 +02:00
|
|
|
Crypt::checkG($g_a, $dh_config['p']);
|
2022-12-30 19:21:36 +01:00
|
|
|
$controller = new VoIP(true, $user['user_id'], $this, VoIP::CALL_STATE_REQUESTED);
|
|
|
|
$controller->storage = ['a' => $a, 'g_a' => \str_pad($g_a->toBytes(), 256, \chr(0), STR_PAD_LEFT)];
|
2022-12-30 22:31:20 +01:00
|
|
|
$res = $this->methodCallAsyncRead('phone.requestCall', ['user_id' => $user, 'g_a_hash' => \hash('sha256', $g_a->toBytes(), true), 'protocol' => ['_' => 'phoneCallProtocol', 'udp_p2p' => true, 'udp_reflector' => true, 'min_layer' => 65, 'max_layer' => VoIP::getConnectionMaxLayer()]]);
|
2019-03-29 20:25:42 +01:00
|
|
|
$controller->setCall($res['phone_call']);
|
|
|
|
$this->calls[$res['phone_call']['id']] = $controller;
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->updaters[UpdateLoop::GENERIC]->resume();
|
2017-07-19 12:41:18 +02:00
|
|
|
return $controller;
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Accept call.
|
|
|
|
*
|
|
|
|
* @param array $call Call
|
|
|
|
*/
|
2023-01-13 14:36:10 +01:00
|
|
|
public function acceptCall(array $call): bool
|
2017-04-01 13:20:57 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw new Exception();
|
2017-04-02 16:43:47 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
if ($this->callStatus($call['id']) !== VoIP::CALL_STATE_ACCEPTED) {
|
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_error_1'], $call['id']));
|
2017-07-31 16:42:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['accepting_call'], $this->calls[$call['id']]->getOtherID()), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$dh_config = ($this->getDhConfig());
|
2023-06-24 18:00:19 +02:00
|
|
|
$this->logger->logger('Generating b...', Logger::VERBOSE);
|
2022-12-30 19:21:36 +01:00
|
|
|
$b = BigInteger::randomRange(Magic::$two, $dh_config['p']->subtract(Magic::$two));
|
2017-04-02 16:42:17 +02:00
|
|
|
$g_b = $dh_config['g']->powMod($b, $dh_config['p']);
|
2020-10-03 15:36:03 +02:00
|
|
|
Crypt::checkG($g_b, $dh_config['p']);
|
2017-07-26 18:38:10 +02:00
|
|
|
try {
|
2022-12-30 22:31:20 +01:00
|
|
|
$res = $this->methodCallAsyncRead('phone.acceptCall', ['peer' => ['id' => $call['id'], 'access_hash' => $call['access_hash'], '_' => 'inputPhoneCall'], 'g_b' => $g_b->toBytes(), 'protocol' => ['_' => 'phoneCallProtocol', 'udp_reflector' => true, 'udp_p2p' => true, 'min_layer' => 65, 'max_layer' => VoIP::getConnectionMaxLayer()]]);
|
2022-12-30 19:21:36 +01:00
|
|
|
} catch (RPCErrorException $e) {
|
2017-07-28 22:38:13 +02:00
|
|
|
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_already_accepted'], $call['id']));
|
2017-07-26 18:38:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-31 16:42:24 +02:00
|
|
|
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(Lang::$current_lang['call_already_declined']);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->discardCall($call['id'], ['_' => 'phoneCallDiscardReasonHangup']);
|
2017-07-31 16:42:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-26 18:38:10 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
2017-07-20 16:20:19 +02:00
|
|
|
$this->calls[$res['phone_call']['id']]->storage['b'] = $b;
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->updaters[UpdateLoop::GENERIC]->resume();
|
2017-07-31 16:42:24 +02:00
|
|
|
return true;
|
2017-04-02 16:42:17 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Confirm call.
|
|
|
|
*
|
|
|
|
* @param array $params Params
|
|
|
|
*/
|
2022-12-30 22:31:20 +01:00
|
|
|
public function confirmCall(array $params)
|
2017-04-02 16:42:17 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
if ($this->callStatus($params['id']) !== VoIP::CALL_STATE_REQUESTED) {
|
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_error_2'], $params['id']));
|
2017-04-02 16:42:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_confirming'], $this->calls[$params['id']]->getOtherID()), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$dh_config = ($this->getDhConfig());
|
2022-12-30 19:21:36 +01:00
|
|
|
$params['g_b'] = new BigInteger((string) $params['g_b'], 256);
|
2020-10-03 15:36:03 +02:00
|
|
|
Crypt::checkG($params['g_b'], $dh_config['p']);
|
2022-12-30 19:21:36 +01:00
|
|
|
$key = \str_pad($params['g_b']->powMod($this->calls[$params['id']]->storage['a'], $dh_config['p'])->toBytes(), 256, \chr(0), STR_PAD_LEFT);
|
2019-07-21 16:50:48 +02:00
|
|
|
try {
|
2022-12-30 22:31:20 +01:00
|
|
|
$res = ($this->methodCallAsyncRead('phone.confirmCall', ['key_fingerprint' => \substr(\sha1($key, true), -8), 'peer' => ['id' => $params['id'], 'access_hash' => $params['access_hash'], '_' => 'inputPhoneCall'], 'g_a' => $this->calls[$params['id']]->storage['g_a'], 'protocol' => ['_' => 'phoneCallProtocol', 'udp_reflector' => true, 'min_layer' => 65, 'max_layer' => VoIP::getConnectionMaxLayer()]]))['phone_call'];
|
2022-12-30 19:21:36 +01:00
|
|
|
} catch (RPCErrorException $e) {
|
2019-07-21 16:50:48 +02:00
|
|
|
if ($e->rpc === 'CALL_ALREADY_ACCEPTED') {
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_already_accepted'], $params['id']));
|
2019-07-21 16:50:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($e->rpc === 'CALL_ALREADY_DECLINED') {
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(Lang::$current_lang['call_already_declined']);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->discardCall($params['id'], ['_' => 'phoneCallDiscardReasonHangup']);
|
2019-07-21 16:50:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-03-28 17:10:35 +01:00
|
|
|
$this->calls[$params['id']]->setCall($res);
|
2017-07-20 16:20:19 +02:00
|
|
|
$visualization = [];
|
2022-12-30 19:21:36 +01:00
|
|
|
$length = new BigInteger(\count(Magic::$emojis));
|
|
|
|
foreach (\str_split(\hash('sha256', $key.\str_pad($this->calls[$params['id']]->storage['g_a'], 256, \chr(0), STR_PAD_LEFT), true), 8) as $number) {
|
2019-09-02 17:08:36 +02:00
|
|
|
$number[0] = \chr(\ord($number[0]) & 0x7f);
|
2022-12-30 19:21:36 +01:00
|
|
|
$visualization[] = Magic::$emojis[(int) (new BigInteger($number, 256))->divide($length)[1]->toString()];
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2017-07-20 16:20:19 +02:00
|
|
|
$this->calls[$params['id']]->setVisualization($visualization);
|
2019-09-02 17:08:36 +02:00
|
|
|
$this->calls[$params['id']]->configuration['endpoints'] = \array_merge($res['connections'], $this->calls[$params['id']]->configuration['endpoints']);
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->calls[$params['id']]->configuration = \array_merge(['recv_timeout' => $this->config['call_receive_timeout_ms'] / 1000, 'init_timeout' => $this->config['call_connect_timeout_ms'] / 1000, 'data_saving' => VoIP::DATA_SAVING_NEVER, 'enable_NS' => true, 'enable_AEC' => true, 'enable_AGC' => true, 'auth_key' => $key, 'auth_key_id' => \substr(\sha1($key, true), -8), 'call_id' => \substr(\hash('sha256', $key, true), -16), 'network_type' => VoIP::NET_TYPE_ETHERNET], $this->calls[$params['id']]->configuration);
|
2017-07-19 12:41:18 +02:00
|
|
|
$this->calls[$params['id']]->parseConfig();
|
2023-01-04 15:13:55 +01:00
|
|
|
return $this->calls[$params['id']]->startTheMagic();
|
2017-04-02 16:42:17 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Complete call handshake.
|
|
|
|
*
|
|
|
|
* @param array $params Params
|
|
|
|
*/
|
2022-12-30 22:31:20 +01:00
|
|
|
public function completeCall(array $params)
|
2017-04-02 16:43:47 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
if ($this->callStatus($params['id']) !== VoIP::CALL_STATE_ACCEPTED || !isset($this->calls[$params['id']]->storage['b'])) {
|
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_error_3'], $params['id']));
|
2017-04-01 13:20:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_completing'], $this->calls[$params['id']]->getOtherID()), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$dh_config = ($this->getDhConfig());
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\hash('sha256', $params['g_a_or_b'], true) != $this->calls[$params['id']]->storage['g_a_hash']) {
|
2023-06-24 18:00:19 +02:00
|
|
|
throw new SecurityException('Invalid g_a!');
|
2017-04-02 16:42:17 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
$params['g_a_or_b'] = new BigInteger((string) $params['g_a_or_b'], 256);
|
2020-10-03 15:36:03 +02:00
|
|
|
Crypt::checkG($params['g_a_or_b'], $dh_config['p']);
|
2022-12-30 19:21:36 +01:00
|
|
|
$key = \str_pad($params['g_a_or_b']->powMod($this->calls[$params['id']]->storage['b'], $dh_config['p'])->toBytes(), 256, \chr(0), STR_PAD_LEFT);
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\substr(\sha1($key, true), -8) != $params['key_fingerprint']) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw new SecurityException(Lang::$current_lang['fingerprint_invalid']);
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2017-07-20 16:20:19 +02:00
|
|
|
$visualization = [];
|
2022-12-30 19:21:36 +01:00
|
|
|
$length = new BigInteger(\count(Magic::$emojis));
|
|
|
|
foreach (\str_split(\hash('sha256', $key.\str_pad($params['g_a_or_b']->toBytes(), 256, \chr(0), STR_PAD_LEFT), true), 8) as $number) {
|
2019-09-02 17:08:36 +02:00
|
|
|
$number[0] = \chr(\ord($number[0]) & 0x7f);
|
2022-12-30 19:21:36 +01:00
|
|
|
$visualization[] = Magic::$emojis[(int) (new BigInteger($number, 256))->divide($length)[1]->toString()];
|
2017-07-20 16:20:19 +02:00
|
|
|
}
|
|
|
|
$this->calls[$params['id']]->setVisualization($visualization);
|
2019-09-02 17:08:36 +02:00
|
|
|
$this->calls[$params['id']]->configuration['endpoints'] = \array_merge($params['connections'], $this->calls[$params['id']]->configuration['endpoints']);
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->calls[$params['id']]->configuration = \array_merge(['recv_timeout' => $this->config['call_receive_timeout_ms'] / 1000, 'init_timeout' => $this->config['call_connect_timeout_ms'] / 1000, 'data_saving' => VoIP::DATA_SAVING_NEVER, 'enable_NS' => true, 'enable_AEC' => true, 'enable_AGC' => true, 'auth_key' => $key, 'auth_key_id' => \substr(\sha1($key, true), -8), 'call_id' => \substr(\hash('sha256', $key, true), -16), 'network_type' => VoIP::NET_TYPE_ETHERNET], $this->calls[$params['id']]->configuration);
|
2017-07-20 16:20:19 +02:00
|
|
|
$this->calls[$params['id']]->parseConfig();
|
2017-07-31 20:57:13 +02:00
|
|
|
return $this->calls[$params['id']]->startTheMagic();
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Get call status.
|
|
|
|
*
|
2019-12-28 17:11:08 +01:00
|
|
|
* @param int $id Call ID
|
2019-12-28 16:07:09 +01:00
|
|
|
*/
|
2022-12-30 19:21:36 +01:00
|
|
|
public function callStatus(int $id): int
|
2017-04-01 13:20:57 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2017-04-01 13:20:57 +02:00
|
|
|
if (isset($this->calls[$id])) {
|
2017-07-19 12:41:18 +02:00
|
|
|
return $this->calls[$id]->getCallState();
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
return VoIP::CALL_STATE_NONE;
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Get call info.
|
|
|
|
*
|
2019-12-28 17:11:08 +01:00
|
|
|
* @param int $call Call ID
|
2019-12-28 16:07:09 +01:00
|
|
|
*/
|
2022-12-30 19:21:36 +01:00
|
|
|
public function getCall(int $call): array
|
2017-04-01 13:20:57 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-19 12:41:18 +02:00
|
|
|
}
|
2017-07-06 18:36:02 +02:00
|
|
|
return $this->calls[$call];
|
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Discard call.
|
|
|
|
*
|
|
|
|
* @param array $call Call
|
|
|
|
* @param array $rating Rating
|
|
|
|
* @param boolean $need_debug Need debug?
|
|
|
|
*/
|
2023-01-21 21:21:35 +01:00
|
|
|
public function discardCall(array $call, array $reason, array $rating = [], bool $need_debug = true): ?VoIP
|
2017-07-06 18:36:02 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (!\class_exists('\\danog\\MadelineProto\\VoIP')) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw Exception::extension('libtgvoip');
|
2017-07-20 16:20:19 +02:00
|
|
|
}
|
2017-07-22 01:02:48 +02:00
|
|
|
if (!isset($this->calls[$call['id']])) {
|
2023-01-21 21:21:35 +01:00
|
|
|
return null;
|
2017-07-22 01:02:48 +02:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
$this->logger->logger(\sprintf(Lang::$current_lang['call_discarding'], $call['id']), Logger::VERBOSE);
|
2017-07-20 16:20:19 +02:00
|
|
|
try {
|
2022-12-30 22:31:20 +01:00
|
|
|
$res = $this->methodCallAsyncRead('phone.discardCall', ['peer' => $call, 'duration' => \time() - $this->calls[$call['id']]->whenCreated(), 'connection_id' => $this->calls[$call['id']]->getPreferredRelayID(), 'reason' => $reason]);
|
2022-12-30 19:21:36 +01:00
|
|
|
} catch (RPCErrorException $e) {
|
2023-06-28 15:50:38 +02:00
|
|
|
if (!\in_array($e->rpc, ['CALL_ALREADY_DECLINED', 'CALL_ALREADY_ACCEPTED'], true)) {
|
2017-07-20 16:20:19 +02:00
|
|
|
throw $e;
|
2017-07-06 18:36:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-20 16:20:19 +02:00
|
|
|
if (!empty($rating)) {
|
2023-06-24 18:00:19 +02:00
|
|
|
$this->logger->logger(\sprintf('Setting rating for call %s...', $call['id']), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->methodCallAsyncRead('phone.setCallRating', ['peer' => $call, 'rating' => $rating['rating'], 'comment' => $rating['comment']]);
|
2017-07-20 16:20:19 +02:00
|
|
|
}
|
2019-05-10 13:34:44 +02:00
|
|
|
if ($need_debug && isset($this->calls[$call['id']])) {
|
2023-06-24 18:00:19 +02:00
|
|
|
$this->logger->logger(\sprintf('Saving debug data for call %s...', $call['id']), Logger::VERBOSE);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->methodCallAsyncRead('phone.saveCallDebug', ['peer' => $call, 'debug' => $this->calls[$call['id']]->getDebugLog()]);
|
2017-07-20 16:20:19 +02:00
|
|
|
}
|
2023-05-11 12:51:35 +02:00
|
|
|
$c = $this->calls[$call['id']];
|
2017-07-21 12:23:09 +02:00
|
|
|
unset($this->calls[$call['id']]);
|
2023-05-11 12:51:35 +02:00
|
|
|
return $c;
|
2019-07-18 23:48:35 +02:00
|
|
|
}
|
2019-12-28 16:07:09 +01:00
|
|
|
/**
|
|
|
|
* Check state of calls.
|
|
|
|
*
|
2020-10-06 20:37:11 +02:00
|
|
|
* @internal
|
2019-12-28 16:07:09 +01:00
|
|
|
*/
|
|
|
|
public function checkCalls(): void
|
2019-07-18 23:48:35 +02:00
|
|
|
{
|
2022-12-08 20:16:40 +01:00
|
|
|
\array_walk($this->calls, function ($controller, $id): void {
|
2022-12-30 19:21:36 +01:00
|
|
|
if ($controller->getCallState() === VoIP::CALL_STATE_ENDED) {
|
2023-01-04 15:13:55 +01:00
|
|
|
$this->logger('Discarding ended call...');
|
2017-07-20 16:21:07 +02:00
|
|
|
$controller->discard();
|
2021-04-18 14:34:32 +02:00
|
|
|
unset($this->calls[$id]);
|
2017-07-20 16:21:07 +02:00
|
|
|
}
|
|
|
|
});
|
2017-04-01 13:20:57 +02:00
|
|
|
}
|
2018-02-24 17:54:39 +01:00
|
|
|
}
|