-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
MyTelegramOrgWrapper.php
298 lines (293 loc) · 11.2 KB
/
MyTelegramOrgWrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
declare(strict_types=1);
/**
* MyTelegramOrgWrapper 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 <[email protected]>
* @copyright 2016-2023 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use Amp\Http\Client\Cookie\LocalCookieJar;
use Amp\Http\Client\Request;
/**
* Wrapper for my.telegram.org.
*/
final class MyTelegramOrgWrapper
{
/**
* Whether we're logged in.
*/
private bool $logged = false;
/**
* Login hash.
*/
private string $hash = '';
/**
* Phone number.
*/
private string $number = '';
/**
* Creation hash.
*/
private string $creation_hash = '';
/**
* Settings.
*
*/
private Settings $settings;
/**
* Datacenter instance.
*/
private DoHWrapper $datacenter;
/**
* Cooke jar.
*
*/
private ?LocalCookieJar $jar = null;
/**
* Endpoint.
*/
private const MY_TELEGRAM_URL = 'https://my.telegram.org';
/**
* Sleep function.
*/
public function __sleep(): array
{
return ['logged', 'hash', 'number', 'creation_hash', 'settings', 'async', 'jar'];
}
/**
* Constructor.
*/
public function __construct(SettingsAbstract $settings)
{
if (!$settings instanceof Settings) {
$settings = new Settings;
$settings->merge($this->settings);
}
$this->settings = $settings;
$this->__wakeup();
}
/**
* Wakeup function.
*/
public function __wakeup(): void
{
if (!$this->jar || !$this->jar instanceof LocalCookieJar) {
$this->jar = new LocalCookieJar();
}
$this->datacenter = new DoHWrapper(
new class($this->settings) implements
SettingsGetter,
LoggerGetter {
public function __construct(
private readonly Settings $settings
) {
}
public function getSettings(): Settings
{
return $this->settings;
}
public function getLogger(): Logger
{
return $this->getLogger();
}
},
$this->jar
);
}
/**
* Login.
*
* @param string $number Phone number
*/
public function login(string $number): void
{
$this->number = $number;
$request = new Request(self::MY_TELEGRAM_URL.'/auth/send_password', 'POST');
$request->setBody(http_build_query(['phone' => $number]));
$request->setHeaders($this->getHeaders('origin'));
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
$resulta = json_decode($result, true);
if (!isset($resulta['random_hash'])) {
throw new Exception($result);
}
$this->hash = $resulta['random_hash'];
}
/**
* Complete login.
*
* @param string $password Password
*/
public function completeLogin(string $password)
{
if ($this->logged) {
throw new Exception('Already logged in!');
}
$request = new Request(self::MY_TELEGRAM_URL.'/auth/login', 'POST');
$request->setBody(http_build_query(['phone' => $this->number, 'random_hash' => $this->hash, 'password' => $password]));
$request->setHeaders($this->getHeaders('origin'));
$request->setHeader('user-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
switch ($result) {
case 'true':
//Logger::log(['Login OK'], Logger::VERBOSE);
break;
default:
throw new Exception($result);
}
return $this->logged = true;
}
/**
* Whether we are logged in.
*/
public function loggedIn(): bool
{
return $this->logged;
}
/**
* Check if an app was already created.
*/
public function hasApp()
{
if (!$this->logged) {
throw new Exception('Not logged in!');
}
$request = new Request(self::MY_TELEGRAM_URL.'/apps');
$request->setHeaders($this->getHeaders('refer'));
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
$title = explode('</title>', explode('<title>', $result)[1])[0];
switch ($title) {
case 'App configuration':
return true;
case 'Create new application':
$this->creation_hash = explode('"/>', explode('<input type="hidden" name="hash" value="', $result)[1])[0];
return false;
}
$this->logged = false;
throw new Exception($title);
}
/**
* Get the currently created app.
*/
public function getApp()
{
if (!$this->logged) {
throw new Exception('Not logged in!');
}
$request = new Request(self::MY_TELEGRAM_URL.'/apps');
$request->setHeaders($this->getHeaders('refer'));
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
$cose = explode('<label for="app_id" class="col-md-4 text-right control-label">App api_id:</label>
<div class="col-md-7">
<span class="form-control input-xlarge uneditable-input" onclick="this.select();"><strong>', $result);
$asd = explode('</strong></span>', $cose[1]);
$api_id = $asd[0];
$cose = explode('<label for="app_hash" class="col-md-4 text-right control-label">App api_hash:</label>
<div class="col-md-7">
<span class="form-control input-xlarge uneditable-input" onclick="this.select();">', $result);
$asd = explode('</span>', $cose[1]);
$api_hash = $asd[0];
return ['api_id' => (int) $api_id, 'api_hash' => $api_hash];
}
/**
* Create an app.
*
* @param array $settings App parameters
*/
public function createApp(array $settings)
{
if (!$this->logged) {
throw new Exception('Not logged in!');
}
if ($this->hasApp()) {
throw new Exception('The app was already created!');
}
$request = new Request(self::MY_TELEGRAM_URL.'/apps/create', 'POST');
$request->setHeaders($this->getHeaders('app'));
$request->setBody(http_build_query(['hash' => $this->creation_hash, 'app_title' => $settings['app_title'], 'app_shortname' => $settings['app_shortname'], 'app_url' => $settings['app_url'], 'app_platform' => $settings['app_platform'], 'app_desc' => $settings['app_desc']]));
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
if ($result) {
throw new Exception(html_entity_decode($result));
}
$request = new Request(self::MY_TELEGRAM_URL.'/apps');
$request->setHeaders($this->getHeaders('refer'));
$response = $this->datacenter->HTTPClient->request($request);
$result = $response->getBody()->buffer();
$title = explode('</title>', explode('<title>', $result)[1])[0];
if ($title === 'Create new application') {
$this->creation_hash = explode('"/>', explode('<input type="hidden" name="hash" value="', $result)[1])[0];
throw new Exception('App creation failed');
}
$cose = explode('<label for="app_id" class="col-md-4 text-right control-label">App api_id:</label>
<div class="col-md-7">
<span class="form-control input-xlarge uneditable-input" onclick="this.select();"><strong>', $result);
$asd = explode('</strong></span>', $cose['1']);
$api_id = $asd['0'];
$cose = explode('<label for="app_hash" class="col-md-4 text-right control-label">App api_hash:</label>
<div class="col-md-7">
<span class="form-control input-xlarge uneditable-input" onclick="this.select();">', $result);
$asd = explode('</span>', $cose['1']);
$api_hash = $asd['0'];
return ['api_id' => (int) $api_id, 'api_hash' => $api_hash];
}
/**
* Function for generating curl request headers.
*
* @param string $httpType Origin
*/
private function getHeaders(string $httpType): array
{
// Common header flags.
$headers = [];
$headers[] = 'Dnt: 1';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
// Add additional headers based on the type of request.
switch ($httpType) {
case 'origin':
$headers[] = 'Origin: '.self::MY_TELEGRAM_URL;
//$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
$headers[] = 'Accept: application/json, text/javascript, */*; q=0.01';
$headers[] = 'Referer: '.self::MY_TELEGRAM_URL.'/auth';
$headers[] = 'X-Requested-With: XMLHttpRequest';
break;
case 'refer':
//$headers[] = 'Accept-Encoding: gzip, deflate, sdch, br';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Referer: '.self::MY_TELEGRAM_URL;
$headers[] = 'Cache-Control: max-age=0';
break;
case 'app':
$headers[] = 'Origin: '.self::MY_TELEGRAM_URL;
//$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
$headers[] = 'Accept: */*';
$headers[] = 'Referer: '.self::MY_TELEGRAM_URL.'/apps';
$headers[] = 'X-Requested-With: XMLHttpRequest';
break;
}
$final_headers = [];
foreach ($headers as $header) {
[$key, $value] = explode(':', $header, 2);
$final_headers[trim($key)] = trim($value);
}
return $final_headers;
}
}