|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BPT\receiver\multi; |
|
4
|
|
|
|
|
5
|
|
|
use BPT\BPT; |
|
6
|
|
|
use BPT\constants\loggerTypes; |
|
7
|
|
|
use BPT\lock; |
|
8
|
|
|
use BPT\logger; |
|
9
|
|
|
use BPT\receiver\webhook; |
|
10
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* curl class , for multiprocessing with curl tricks |
|
14
|
|
|
*/ |
|
15
|
|
|
class curl extends webhook { |
|
16
|
|
|
public static function init (): string|null { |
|
17
|
|
|
if (!self::checkIP()) { |
|
18
|
|
|
logger::write('not authorized access denied. IP : '. $_SERVER['REMOTE_ADDR'] ?? 'unknown',loggerTypes::WARNING); |
|
19
|
|
|
BPT::exit(); |
|
20
|
|
|
} |
|
21
|
|
|
return self::getUpdate(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private static function checkIP(): bool { |
|
25
|
|
|
return $_SERVER['REMOTE_ADDR'] === $_SERVER['SERVER_ADDR']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private static function getUpdate (): string { |
|
29
|
|
|
$input = json_decode(file_get_contents('php://input'), true); |
|
30
|
|
|
webhook::telegramVerify($input['ip']); |
|
31
|
|
|
return $input['update']; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @internal Only for BPT self usage , Don't use it in your source! |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function install() { |
|
38
|
|
|
$urls = self::setURLS(); |
|
39
|
|
|
$file = $urls['file']; |
|
40
|
|
|
self::create($file, self::getTimeout($file)); |
|
41
|
|
|
self::setWebhook($urls['url']); |
|
42
|
|
|
lock::set('BPT-MULTI-CURL'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function getTimeout($url): float|int { |
|
46
|
|
|
$times = []; |
|
47
|
|
|
for ($i = 0; $i < 10; $i ++) { |
|
48
|
|
|
$ch = curl_init($url); |
|
49
|
|
|
curl_setopt_array($ch, [CURLOPT_POSTFIELDS => json_encode([]), CURLOPT_TIMEOUT_MS => 100, CURLOPT_NOBODY => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_CONNECTTIMEOUT_MS => 100, CURLOPT_HTTPHEADER => ['accept: application/json', 'content-type: application/json']]); |
|
50
|
|
|
$start = microtime(true); |
|
51
|
|
|
curl_exec($ch); |
|
52
|
|
|
$times[] = ((microtime(true) - $start) * 1000); |
|
53
|
|
|
} |
|
54
|
|
|
$timeout = round(array_sum($times) / count($times)); |
|
55
|
|
|
return $timeout > 50 ? $timeout + 10 : 50; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private static function create($file,$timeout) { |
|
59
|
|
|
file_put_contents('receiver.php', '<?php http_response_code(200);ignore_user_abort();$ch = curl_init(\'' . $file . '\');curl_setopt_array($ch, [CURLOPT_POSTFIELDS => json_encode([\'update\'=>file_get_contents(\'php://input\'),\'ip\'=>$_SERVER[\'REMOTE_ADDR\']]), CURLOPT_TIMEOUT_MS => ' . $timeout . ', CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_CONNECTTIMEOUT_MS => ' . $timeout . ', CURLOPT_HTTPHEADER => [\'accept: application/json\', \'content-type: application/json\']]);curl_exec($ch);curl_close($ch);?>'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
#[ArrayShape(['url' => 'array|string|string[]', 'file' => 'array|string|string[]'])] |
|
63
|
|
|
private static function setURLS(): array { |
|
64
|
|
|
$base_url = self::setURL(); |
|
65
|
|
|
$file = basename($_SERVER['REQUEST_URI']); |
|
66
|
|
|
return [ |
|
67
|
|
|
'url'=>str_replace($file, 'receiver.php', $base_url), |
|
68
|
|
|
'file'=>str_replace($file, basename($_SERVER['SCRIPT_NAME']), $base_url) |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
} |