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 ;
2023-08-14 16:10:20 +02:00
use Amp\ByteStream\ReadableStream ;
2023-08-12 16:34:46 +02:00
use Amp\DeferredFuture ;
2023-08-20 21:39:43 +02:00
use AssertionError ;
2023-08-13 18:31:31 +02:00
use danog\MadelineProto\LocalFile ;
2022-12-30 19:21:36 +01:00
use danog\MadelineProto\Logger ;
use danog\MadelineProto\Magic ;
2020-10-03 15:36:03 +02:00
use danog\MadelineProto\MTProtoTools\Crypt ;
2023-08-20 21:30:36 +02:00
use danog\MadelineProto\Ogg ;
2023-06-25 15:27:46 +02:00
use danog\MadelineProto\PeerNotInDbException ;
2023-08-13 18:31:31 +02:00
use danog\MadelineProto\RemoteUrl ;
2022-12-30 19:21:36 +01:00
use danog\MadelineProto\VoIP ;
2023-08-12 17:29:00 +02:00
use danog\MadelineProto\VoIPController ;
2022-12-30 19:21:36 +01:00
use phpseclib3\Math\BigInteger ;
2023-08-12 16:34:46 +02:00
use Throwable ;
2022-12-30 19:21:36 +01:00
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-08-12 17:29:00 +02:00
/** @var array<int, VoIPController> */
2023-01-20 14:36:21 +01:00
private array $calls = [];
2023-08-12 21:47:09 +02:00
/** @var array<int, VoIPController> */
private array $callsByPeer = [];
2023-08-12 16:34:46 +02:00
private array $pendingCalls = [];
2019-12-28 16:07:09 +01:00
/**
* Request VoIP call .
*
* @ param mixed $user User
*/
2023-08-14 17:16:59 +02:00
public function requestCall ( mixed $user ) : VoIP
2017-04-01 13:20:57 +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
}
2023-08-12 16:34:46 +02:00
$user = $user [ 'bot_api_id' ];
if ( isset ( $this -> pendingCalls [ $user ])) {
return $this -> pendingCalls [ $user ] -> await ();
2017-04-02 16:43:47 +02:00
}
2023-08-12 16:34:46 +02:00
$deferred = new DeferredFuture ;
$this -> pendingCalls [ $user ] = $deferred -> getFuture ();
2023-08-12 17:29:00 +02:00
2017-07-26 18:38:10 +02:00
try {
2023-08-12 16:34:46 +02:00
$this -> logger -> logger ( \sprintf ( 'Calling %s...' , $user ), Logger :: VERBOSE );
$dh_config = ( $this -> getDhConfig ());
$this -> logger -> logger ( 'Generating a...' , Logger :: VERBOSE );
$a = BigInteger :: randomRange ( Magic :: $two , $dh_config [ 'p' ] -> subtract ( Magic :: $two ));
$this -> logger -> logger ( 'Generating g_a...' , Logger :: VERBOSE );
$g_a = $dh_config [ 'g' ] -> powMod ( $a , $dh_config [ 'p' ]);
Crypt :: checkG ( $g_a , $dh_config [ 'p' ]);
$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' => 92 ]])[ 'phone_call' ];
$res [ 'a' ] = $a ;
$res [ 'g_a' ] = \str_pad ( $g_a -> toBytes (), 256 , \chr ( 0 ), STR_PAD_LEFT );
2023-08-12 17:29:00 +02:00
$this -> calls [ $res [ 'id' ]] = $controller = new VoIPController ( $this , $res );
2023-08-12 21:47:09 +02:00
$this -> callsByPeer [ $controller -> public -> otherID ] = $controller ;
2023-08-12 16:34:46 +02:00
unset ( $this -> pendingCalls [ $user ]);
2023-08-12 17:29:00 +02:00
$deferred -> complete ( $controller -> public );
2023-08-12 16:34:46 +02:00
} catch ( Throwable $e ) {
unset ( $this -> pendingCalls [ $user ]);
$deferred -> error ( $e );
}
return $deferred -> getFuture () -> await ();
2017-04-01 13:20:57 +02:00
}
2023-08-12 16:34:46 +02:00
/** @internal */
2023-08-12 17:29:00 +02:00
public function cleanupCall ( int $id ) : void
{
2023-08-12 21:49:56 +02:00
if ( isset ( $this -> calls [ $id ])) {
$call = $this -> calls [ $id ];
2023-08-13 11:40:35 +02:00
unset ( $this -> callsByPeer [ $call -> public -> otherID ], $this -> calls [ $id ]);
2023-08-12 21:49:56 +02:00
}
2017-04-01 13:20:57 +02:00
}
2023-08-12 17:29:00 +02:00
/**
* Get call emojis ( will return null if the call is not inited yet ) .
*
* @ internal
*
* @ return ? list { string , string , string , string }
*/
public function getCallVisualization ( int $id ) : ? array
{
return ( $this -> calls [ $id ] ? ? null ) ? -> getVisualization ();
}
/**
* Accept call .
*/
public function acceptCall ( int $id ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> accept ();
}
/**
* Discard call .
2023-08-12 21:47:09 +02:00
*
* @ param int < 1 , 5 > $rating Call rating in stars
* @ param string $comment Additional comment on call quality .
*/
public function discardCall ( int $id , DiscardReason $reason = DiscardReason :: HANGUP , ? int $rating = null , ? string $comment = null ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> discard ( $reason , $rating , $comment );
}
/**
2023-08-14 18:28:44 +02:00
* Get the phone call with the specified user ID .
2023-08-12 17:29:00 +02:00
*/
2023-08-12 21:47:09 +02:00
public function getCallByPeer ( int $userId ) : ? VoIP
2023-08-12 17:29:00 +02:00
{
2023-08-15 12:41:45 +02:00
return ( $this -> callsByPeer [ $userId ] ? ? null ) ? -> public ;
2023-08-12 17:29:00 +02:00
}
2023-08-20 18:14:26 +02:00
/**
* Get all pending and running calls , indexed by user ID .
*
* @ return array < int , VoIP >
*/
public function getAllCalls () : array
{
return \array_map ( fn ( VoIPController $v ) : VoIP => $v -> public , $this -> callsByPeer );
}
2023-08-14 18:28:44 +02:00
/**
* Get phone call information .
*/
public function getCall ( int $id ) : ? VoIP
{
2023-08-15 12:41:45 +02:00
return ( $this -> calls [ $id ] ? ? null ) ? -> public ;
2023-08-14 18:28:44 +02:00
}
2023-08-12 17:29:00 +02:00
/**
* Play file in call .
*/
2023-08-14 16:10:20 +02:00
public function callPlay ( int $id , LocalFile | RemoteUrl | ReadableStream $file ) : void
2023-08-12 17:29:00 +02:00
{
2023-08-20 21:30:36 +02:00
if ( ! Magic :: canConvertOgg ()) {
2023-08-20 21:39:43 +02:00
if ( $file instanceof LocalFile || $file instanceof RemoteUrl ) {
2023-08-20 21:30:36 +02:00
Ogg :: validateOgg ( $file );
2023-08-20 21:39:43 +02:00
} else {
throw new AssertionError ( " The passed file was not generated by MadelineProto or @libtgvoipbot, please pre-convert it using @libtgvoip bot or install FFI and ffmpeg to perform realtime conversion! " );
2023-08-20 21:30:36 +02:00
}
}
2023-08-12 17:29:00 +02:00
( $this -> calls [ $id ] ? ? null ) ? -> play ( $file );
}
2023-08-20 14:17:21 +02:00
/**
* Play file in call , blocking until the file has finished playing if a stream is provided .
*
* @ internal
*/
public function callPlayBlocking ( int $id , LocalFile | RemoteUrl | ReadableStream $file ) : void
{
if ( ! isset ( $this -> calls [ $id ])) {
return ;
}
2023-08-20 21:30:36 +02:00
$this -> callPlay ( $id , $file );
2023-08-20 14:17:21 +02:00
if ( $file instanceof ReadableStream ) {
$deferred = new DeferredFuture ;
$file -> onClose ( $deferred -> complete ( ... ));
$deferred -> getFuture () -> await ();
}
}
2023-08-14 18:28:44 +02:00
/**
* When called , skips to the next file in the playlist .
*/
public function skipPlay ( int $id ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> skip ();
}
/**
* Stops playing all files in the call , clears the main and the hold playlist .
*/
public function stopPlay ( int $id ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> stop ();
}
2023-08-20 18:31:21 +02:00
/**
* Pauses playback of the current audio file in the call .
*/
public function pausePlay ( int $id ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> pause ();
}
/**
* Resumes playback of the current audio file in the call .
*/
public function resumePlay ( int $id ) : void
{
( $this -> calls [ $id ] ? ? null ) ? -> resume ();
}
2023-08-20 19:07:14 +02:00
/**
* Whether the currently playing audio file is paused .
*/
public function isPlayPaused ( int $id ) : bool
{
return ( $this -> calls [ $id ] ? ? null ) ? -> isPaused () ? ? false ;
}
2023-08-12 17:29:00 +02:00
/**
* Play files on hold in call .
*/
2023-08-14 20:54:16 +02:00
public function callPlayOnHold ( int $id , LocalFile | RemoteUrl | ReadableStream ... $files ) : void
2023-08-12 17:29:00 +02:00
{
2023-08-20 21:30:36 +02:00
if ( ! Magic :: canConvertOgg ()) {
foreach ( $files as $file ) {
2023-08-20 21:39:43 +02:00
if ( $file instanceof LocalFile || $file instanceof RemoteUrl ) {
2023-08-20 21:30:36 +02:00
Ogg :: validateOgg ( $file );
2023-08-20 21:39:43 +02:00
} else {
throw new AssertionError ( " The passed file was not generated by MadelineProto or @libtgvoipbot, please pre-convert it using @libtgvoip bot or install FFI and ffmpeg to perform realtime conversion! " );
2023-08-20 21:30:36 +02:00
}
}
}
2023-08-14 20:54:16 +02:00
( $this -> calls [ $id ] ? ? null ) ? -> playOnHold ( ... $files );
2023-08-12 17:29:00 +02:00
}
2023-08-20 14:17:21 +02:00
/**
* Play files on hold in call .
*
* @ internal
*/
public function callPlayOnHoldBlocking ( int $id , LocalFile | RemoteUrl | ReadableStream ... $files ) : void
{
if ( ! isset ( $this -> calls [ $id ])) {
return ;
}
2023-08-20 21:30:36 +02:00
$this -> callPlayOnHold ( $id , ... $files );
2023-08-20 14:17:21 +02:00
foreach ( $files as $file ) {
if ( $file instanceof ReadableStream ) {
$deferred = new DeferredFuture ;
$file -> onClose ( $deferred -> complete ( ... ));
$deferred -> getFuture () -> await ();
}
}
}
2023-08-19 21:20:51 +02:00
/**
* Get the file that is currently being played .
2023-08-20 14:17:21 +02:00
*
2023-08-19 21:20:51 +02:00
* Will return a string with the object ID of the stream if we ' re currently playing a stream , otherwise returns the related LocalFile or RemoteUrl .
*/
public function callGetCurrent ( int $id ) : RemoteUrl | LocalFile | string | null
{
return ( $this -> calls [ $id ] ? ? null ) ? -> getCurrent ();
}
2023-08-12 17:29:00 +02:00
/**
* Get call state .
*/
public function getCallState ( int $id ) : ? CallState
{
return ( $this -> calls [ $id ] ? ? null ) ? -> getCallState ();
}
2018-02-24 17:54:39 +01:00
}