-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare.php
More file actions
389 lines (326 loc) · 12.8 KB
/
Copy pathprepare.php
File metadata and controls
389 lines (326 loc) · 12.8 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
set_time_limit(60);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: Content-Type");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Type: application/json; charset=utf-8');
interface OSDetectorInterface {
public function detect(): string;
}
interface ExtensionCheckerInterface {
public function check(): array;
}
interface ToolCheckerInterface {
public function check(): array;
}
interface FileOperatorInterface {
public function checkAllFiles(): array;
public function updatePacFile(): bool;
public function readConfig(): ?array;
}
class OSDetector implements OSDetectorInterface {
public function detect(): string {
if (defined('PHP_OS_FAMILY')) {
$os = PHP_OS_FAMILY;
} else {
$os = PHP_OS;
if (stripos($os, 'WIN') === 0) {
$os = 'Windows';
} elseif (stripos($os, 'Linux') === 0) {
$os = 'Linux';
} else {
$os = 'Unsupported';
}
}
return strtolower($os);
}
}
class PHPExtensionChecker implements ExtensionCheckerInterface {
private $os;
private $disabledFunctions;
public function __construct(string $os) {
$this->os = $os;
$this->disabledFunctions = explode(',', ini_get('disable_functions') ?: '');
}
public function check(): array {
$checks = [
'shell_exec' => !in_array('shell_exec', $this->disabledFunctions),
'file_get_contents' => !in_array('file_get_contents', $this->disabledFunctions),
'php_curl' => extension_loaded('curl')
];
if ($this->os === 'windows') {
$checks['popen'] = $this->checkPopen();
$checks['com_dotnet'] = $this->checkComDotnet();
$checks['wmi_connect'] = $this->checkWMI();
}
return $checks;
}
private function checkComDotnet(): bool {
return extension_loaded('com_dotnet');
}
private function checkWMI(): bool {
if (!class_exists('COM')) {
return false;
}
try {
new COM('WbemScripting.SWbemLocator');
return true;
} catch (Exception $e) {
return false;
}
}
private function checkPopen(): bool {
return !in_array('popen', $this->disabledFunctions);
}
}
class SystemToolChecker implements ToolCheckerInterface {
private $os;
private $execEnabled;
private $fileGetContentsEnabled;
public function __construct(string $os, bool $execEnabled, bool $fileGetContentsEnabled) {
$this->os = $os;
$this->execEnabled = $execEnabled;
$this->fileGetContentsEnabled = $fileGetContentsEnabled;
}
public function check(): array {
return $this->os === 'windows'
? $this->checkWindowsTools()
: $this->checkLinuxTools();
}
private function checkWindowsTools(): array {
return ['powershell' => $this->testCommand('where powershell')];
}
private function checkLinuxTools(): array {
$tools = ['lsof', 'nohup', 'kill'];
$result = [];
foreach ($tools as $tool) {
$result[$tool] = $this->testCommand("which $tool");
}
$procMounted = is_dir('/proc');
return array_merge($result, [
'/proc_mounted' => $procMounted,
'/proc_exe_readable' => $procMounted && $this->checkProcFile('/proc/self/exe'),
'/proc_cmdline_readable' => $procMounted && $this->checkProcFile('/proc/self/cmdline')
]);
}
private function testCommand(string $cmd): bool {
if (!$this->execEnabled) return false;
$redirect = $this->os === 'windows' ? '2>NUL' : '2>/dev/null';
exec("$cmd $redirect", $_, $code);
return $code === 0;
}
private function checkProcFile(string $path): bool {
return $this->fileGetContentsEnabled && @file_get_contents($path, false, null, 0, 1) !== false;
}
}
class FileOperator implements FileOperatorInterface {
private $os;
private $rootDir;
private $configPath = 'config.json';
private $pacPath = 'local.pac';
public function __construct(string $os) {
$this->os = $os;
$this->rootDir = __DIR__;
}
public function checkAllFiles(): array {
return [
'ciadpi_file' => $this->checkCiadpiFile(),
'config_file' => $this->checkFile($this->configPath, ['writable', 'valid']),
'curl_certificate_file' => $this->checkFile('curl_cert/cacert.pem'),
'pac_file' => $this->checkFile($this->pacPath, ['writable', 'updated']),
($this->os === 'windows' ? 'windows_file' : 'linux_file') => $this->checkProcessFile(),
'main_server_hosts' => $this->checkMainHostsFiles()
];
}
public function updatePacFile(): bool {
$config = $this->readConfig();
if (!$config || !$this->validateConfig($config)) return false;
$pacContent = file_get_contents($this->pacPath);
if ($pacContent === false) return false;
$servers = [];
for ($i = 1; $i <= 8; $i++) {
$portKey = "main_$i";
$port = $config['ciadpi_main_servers_tcp_ports'][$portKey] ?? null;
if (is_numeric($port)) {
$domains = $this->readHostsFile("byedpi/main_server_{$i}_hosts.txt");
$servers[] = [
'ip' => $config['local_ip'],
'port' => $port,
'domains' => $domains
];
}
}
$serversJson = json_encode($servers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
if ($serversJson === false) return false;
$newContent = preg_replace(
'/const servers = \[.*?\];/s',
"const servers = $serversJson;",
$pacContent
);
if ($newContent === null) return false;
return file_put_contents($this->pacPath, $newContent) !== false;
}
public function readConfig(): ?array {
$content = @file_get_contents($this->configPath);
if ($content === false) return null;
$config = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) return null;
return $config;
}
private function checkCiadpiFile(): array {
$exe = $this->os === 'windows' ? 'ciadpi.exe' : 'ciadpi';
$path = "byedpi/$exe";
return $this->checkFile($path, ['executable']);
}
private function checkProcessFile(): array {
$filename = $this->os === 'windows' ? 'windows.php' : 'linux.php';
return $this->checkFile($filename);
}
private function checkMainHostsFiles(): array {
$result = [];
for ($i = 1; $i <= 8; $i++) {
$path = "byedpi/main_server_{$i}_hosts.txt";
$result["main_server_{$i}_hosts_file"] = array_merge(
$this->checkFile($path, ['writable', 'hosts']),
['hosts' => $this->readHostsFile($path)]
);
}
return $result;
}
private function checkFile(string $path, array $flags = []): array {
$fullPath = $this->rootDir . '/' . $path;
$exists = file_exists($fullPath);
$result = [
'filename' => basename($path),
'filepath' => realpath($fullPath) ?: $fullPath,
'exists' => $exists,
'is_file' => $exists && is_file($fullPath),
'readable' => $exists && is_readable($fullPath)
];
foreach ($flags as $flag) {
switch ($flag) {
case 'executable':
$result['executable'] = $exists && is_executable($fullPath);
break;
case 'writable':
$result['writable'] = $exists && is_writable($fullPath);
break;
case 'valid':
$config = $this->readConfig();
$result['valid'] = $config !== null && $this->validateConfig($config);
break;
case 'hosts':
$result['hosts'] = $this->readHostsFile($path);
break;
}
}
return $result;
}
private function readHostsFile(string $path): array {
$content = @file_get_contents($this->rootDir . '/' . $path);
if ($content === false) return [];
return array_filter(array_map('trim', explode("\n", $content)));
}
private function validateConfig(?array $config): bool {
if (!$config || !isset($config['local_ip']) || !filter_var($config['local_ip'], FILTER_VALIDATE_IP)) {
return false;
}
if (!isset($config['ciadpi_main_servers_tcp_ports']) || !is_array($config['ciadpi_main_servers_tcp_ports'])) {
return false;
}
foreach ($config['ciadpi_main_servers_tcp_ports'] as $port) {
if (!is_numeric($port) || $port < 1 || $port > 65535) {
return false;
}
}
return true;
}
}
function getCurrentGgcDomain() {
$sources = [
"https://redirector.gvt1.com/report_mapping?di=no",
"http://redirector.c.googlevideo.com/report_mapping?di=no"
];
$decoder = [
'u' => '0', 'z' => '1', 'p' => '2', 'k' => '3', 'f' => '4', 'a' => '5',
'5' => '6', '0' => '7', 'v' => '8', 'q' => '9', 'l' => 'a', 'g' => 'b',
'b' => 'c', '6' => 'd', '1' => 'e', 'w' => 'f', 'r' => 'g', 'm' => 'h',
'h' => 'i', 'c' => 'j', '7' => 'k', '2' => 'l', 'x' => 'm', 's' => 'n',
'n' => 'o', 'i' => 'p', 'd' => 'q', '8' => 'r', '3' => 's', 'y' => 't',
't' => 'u', 'o' => 'v', 'j' => 'w', 'e' => 'x', '9' => 'y', '4' => 'z',
'-' => '-'
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 4,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
]);
foreach ($sources as $url) {
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
if (!$response) continue;
if (preg_match('/=>\s*([a-z0-9\-]+)\b/', $response, $match)) {
$prefix = trim($match[1], '.: ');
$converted = '';
foreach (str_split($prefix) as $char) {
$converted .= $decoder[$char] ?? $char;
}
curl_close($ch);
return "https://rr1---sn-{$converted}.googlevideo.com/generate_204";
}
}
curl_close($ch);
return null;
}
$osDetector = new OSDetector();
$os = $osDetector->detect();
$response = [
'os' => ['detected_os' => $os],
'php' => [],
'tools_and_functions' => [],
'files' => [],
'config' => []
];
if ($os === 'unsupported') {
echo json_encode($response, JSON_PRETTY_PRINT);
exit;
}
$phpChecker = new PHPExtensionChecker($os);
$phpCheck = $phpChecker->check();
$response['php'] = $phpCheck;
$toolChecker = new SystemToolChecker(
$os,
$phpCheck['shell_exec'],
$phpCheck['file_get_contents']
);
$response['tools_and_functions'] = $toolChecker->check();
$fileOperator = new FileOperator($os);
$fileResults = $fileOperator->checkAllFiles();
$response['files'] = $fileResults;
$config = $fileOperator->readConfig();
if ($config !== null) {
$response['config'] = [
'local_ip' => $config['local_ip'] ?? null,
'ciadpi_main_servers_tcp_ports' => $config['ciadpi_main_servers_tcp_ports'] ?? [],
'ciadpi_main_servers_latest_used_strategies' => $config['ciadpi_main_servers_latest_used_strategies'] ?? [],
'ciadpi_test_servers_tcp_ports' => $config['ciadpi_test_servers_tcp_ports'] ?? [],
'select_links' => $config['select_links'] ?? []
];
}
$response['pac_update'] = ['success' => false];
if (($fileResults['pac_file']['writable'] ?? false)) {
$response['pac_update']['success'] = $fileOperator->updatePacFile();
}
$ggcDomain = null;
if (($phpCheck['php_curl'] ?? false)) {
$ggcDomain = getCurrentGgcDomain();
}
$response['your_google_global_cache'] = $ggcDomain ?? "Google Global Cache не удалось определить.";
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
?>