-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxyChecker.php
427 lines (386 loc) · 13.7 KB
/
proxyChecker.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/*
----------------------------------------------------------------------------
LICENSE
----------------------------------------------------------------------------
This file is part of Proxy Checker.
Proxy Checker is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Proxy Checker 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Proxy Checker. If not, see <https://www.gnu.org/licenses/>.
----------------------------------------------------------------------------
Copyright (c) 2024 Dimas lanjaka
----------------------------------------------------------------------------
This project is licensed under the GNU General Public License v3.0
For full license details, please visit: https://www.gnu.org/licenses/gpl-3.0.html
If you have any inquiries regarding the license or permissions, please contact:
Name: Dimas Lanjaka
Website: https://www.webmanajemen.com
Email: [email protected]
*/
require_once __DIR__ . "/proxyCheckerParallel-func.php";
use PhpProxyHunter\ProxyDB;
use PhpProxyHunter\Proxy;
use PhpProxyHunter\Scheduler;
global $isCli;
if (!$isCli) {
if (function_exists('header')) {
// Allow from any origin
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: text/plain; charset=utf-8');
header('X-Powered-By: L3n4r0x');
}
// only allow post
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
exit('direct method disallowed');
}
}
// limit execution time seconds unit
$maxExecutionTime = 120;
$startTime = microtime(true);
if (function_exists('set_time_limit')) {
// Set the PHP maximum execution time to 5 minutes (300 seconds)
set_time_limit(300);
}
// set output buffering to zero
// avoid error while running on CLI
if (!$isCli) {
ini_set('output_buffering', 0);
if (ob_get_level() == 0) {
ob_start();
}
}
$config = getConfig(getUserId());
$endpoint = trim($config['endpoint']);
$headers = array_filter($config['headers']);
$checksFor = $config['type'];
if (!$isCli) {
if (isset($_SERVER['HTTP_HOST'])) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$origin = $protocol . $_SERVER['HTTP_HOST'];
}
if (isset($origin)) {
echo "working proxies $origin/working.txt\n";
echo "dead proxies $origin/dead.txt\n";
}
}
/// FUNCTIONS (DO NOT EDIT)
$db = new ProxyDB(__DIR__ . '/src/database.sqlite');
$lockFilePath = __DIR__ . "/proxyChecker.lock";
$statusFile = __DIR__ . "/status.txt";
$max_checks = 50;
/**
* Array of strings to remove.
*
* @var string[]
*/
$str_to_remove = [];
$isAdmin = false;
if ($isCli) {
$short_opts = "p:m::";
$long_opts = [
"proxy:",
"max::",
"userId::",
"lockFile::",
"runner::",
"admin::"
];
$options = getopt($short_opts, $long_opts);
if (!empty($options['max'])) {
$max = intval($options['max']);
if ($max > 0) {
$max_checks = $max;
}
}
if (!empty($options['admin']) && $options['admin'] !== 'false') {
$isAdmin = true;
// set time limit 30 minutes for admin
$maxExecutionTime = 30 * 60;
// disable execution limit
set_time_limit(0);
}
}
if (file_exists($lockFilePath) && !is_debug() && !$isAdmin) {
echo date(DATE_RFC3339) . ' another process still running' . PHP_EOL;
exit();
} else {
file_put_contents($lockFilePath, $config['user_id'] . '=' . json_encode($config));
file_put_contents($statusFile, 'running');
}
Scheduler::register(function () use ($lockFilePath, $statusFile, $db) {
// clean proxies.txt
// clean_proxies_file(__DIR__ . '/proxies.txt');
$data = parse_working_proxies($db);
$countsString = implode("\n", array_map(function ($key, $value) {
return "$key proxies $value";
}, array_keys($data['counter']), $data['counter']));
echo $countsString . PHP_EOL;
echo "releasing lock" . PHP_EOL;
// clean lock files
if (file_exists($lockFilePath)) {
unlink($lockFilePath);
}
echo "update status to IDLE" . PHP_EOL;
file_put_contents($statusFile, 'idle');
}, 'z_onExit' . basename(__FILE__));
// print cURL informations
echo "User " . $config['user_id'] . ' at ' . date("Y-m-d H:i:s") . PHP_EOL;
echo "GET $endpoint " . strtoupper($checksFor) . PHP_EOL;
echo implode("\n", $headers) . PHP_EOL;
// Specify the file path
$untestedFilePath = __DIR__ . "/proxies.txt";
$deadPath = __DIR__ . "/dead.txt";
$workingPath = __DIR__ . "/working.txt";
setMultiPermissions([$untestedFilePath, $workingPath, $deadPath], true);
// move backup added proxies
$countLinesUntestedProxies = countNonEmptyLines($untestedFilePath);
if ($countLinesUntestedProxies < 100 && file_exists($untestedFilePath) && filesize($untestedFilePath) === 0) {
$assets = getFilesByExtension(__DIR__ . '/assets/proxies', 'txt');
foreach ($assets as $asset) {
if (strpos($asset, 'added-') !== false) {
echo $asset . ' - ' . (file_exists($asset) ? 'true' : 'false') . PHP_EOL;
if (file_exists($asset)) {
$move = moveContent($asset, $untestedFilePath);
if ($move === '') {
unlink($asset);
exit('copied assets' . PHP_EOL);
} else {
echo $move . PHP_EOL;
}
}
}
}
}
$untested = [];
try {
$db_untested = $db->getUntestedProxies($max_checks);
$db_working = $db->getWorkingProxies($max_checks);
// include dead proxies when current date (minute unit) can be divided by 3
// or if untested proxies less than $max_checks items
$include_dead_proxies = date('i') % 3 == 0 || count($db_untested) < $max_checks || empty($db_untested);
$db_dead = $include_dead_proxies ? $db->getDeadProxies($max_checks) : [];
$db_data = array_merge($db_untested, $db_working, $db_dead);
$db_data_map = array_map(function ($item) {
$wrap = new Proxy($item['proxy']);
foreach ($item as $key => $value) {
if (property_exists($wrap, $key)) {
$wrap->$key = $value;
}
}
if (!empty($item['username']) && !empty($item['password'])) {
$wrap->username = $item['username'];
$wrap->password = $item['password'];
}
return $wrap;
}, $db_data);
$db_data_map = filter_proxies($db_data_map, $include_dead_proxies);
$untested = array_merge($untested, $db_data_map);
echo "[DB] queue: " . count($db_data_map) . " proxies" . PHP_EOL;
} catch (\Throwable $th) {
echo "failed add untested proxies from database " . $th->getMessage() . PHP_EOL;
}
// get proxy from proxies.txt
if ($countLinesUntestedProxies > 0) {
$file_untested_str = read_first_lines($untestedFilePath, 150);
if (empty($file_untested_str)) {
$file_untested_str = [];
}
$file_untested = extractProxies(implode("\n", $file_untested_str));
$file_untested = filter_proxies($file_untested, date('i') % 3 == 0);
echo "[FILE] queue: " . count($file_untested) . " proxies" . PHP_EOL;
$untested = array_merge($untested, $file_untested);
shuffle($untested);
}
if (empty($untested)) {
// re-check dead proxies when both data (db & file) is empty
$db_dead = $db->getDeadProxies(10000);
$db_data_map = array_map(function ($item) {
// check if dead proxy checked less than 1 hour ago
if (!empty($item['last_check'])) {
if (!isDateRFC3339OlderThanHours($item['last_check'], 24)) {
// drop dead proxy when last checked less than 24 hour ago
return null;
}
}
// process transform to Proxy class
$wrap = new Proxy($item['proxy']);
foreach ($item as $key => $value) {
if (property_exists($wrap, $key)) {
$wrap->$key = $value;
}
}
if (!empty($item['username']) && !empty($item['password'])) {
$wrap->username = $item['username'];
$wrap->password = $item['password'];
}
return $wrap;
}, $db_dead);
// assign untested
$untested = array_filter(array_merge($untested, $db_data_map));
}
echo PHP_EOL;
execute_array_proxies();
function execute_array_proxies()
{
global $untested, $max_checks;
// filter proxies and skip dead proxies when current minute can be divided by 3
$proxies = filter_proxies($untested, date('i') % 3 == 0);
// skip empty array
if (empty($proxies)) {
return;
}
// shuffle array
shuffle($proxies);
iterateArray($proxies, $max_checks, 'execute_single_proxy');
}
/**
* filter empty proxy, last checked only yesterday or empty
* @param Proxy[] $proxies
* @param bool $skip_dead_proxies
* @return Proxy[]
*/
function filter_proxies(array $proxies, bool $skip_dead_proxies = false)
{
global $db, $str_to_remove;
if (empty($proxies)) {
return [];
}
// unique proxies
$proxies = uniqueClassObjectsByProperty($proxies, 'proxy');
// skip already checked proxy
$proxies = array_filter($proxies, function (Proxy $item) use ($db, $skip_dead_proxies) {
$sel = $db->select($item->proxy);
if (empty($sel)) {
echo "add $item->proxy" . PHP_EOL;
// add proxy
$db->add($item->proxy, true);
// re-select proxy
$sel = $db->select($item->proxy);
}
if (!empty($sel) && empty($sel[0]['status'])) {
echo "[SQLite] untested $item->proxy" . PHP_EOL;
$db->updateStatus($item->proxy, 'untested');
}
$str_to_remove[] = $item->proxy;
schedule_remover();
if (empty($item->last_check) || empty($item->status)) {
return true;
}
if ($skip_dead_proxies) {
if ($item->status == 'dead' || $item->status == 'port-closed') {
return false;
}
}
return isDateRFC3339OlderThanHours($item->last_check, 24);
});
return $proxies;
}
function execute_single_proxy(Proxy $item)
{
global $db, $headers, $endpoint, $startTime, $maxExecutionTime, $str_to_remove;
// Check if execution time has exceeded the maximum allowed time
$elapsedTime = microtime(true) - $startTime;
if ($elapsedTime > $maxExecutionTime) {
// Execution time exceeded
echo "Execution time exceeded maximum allowed time of {$maxExecutionTime} seconds." . PHP_EOL;
exit(0);
return;
}
$raw_proxy = '';
$proxyValid = isValidProxy($item->proxy);
if ($proxyValid) {
$raw_proxy = $item->proxy;
if (!is_null($item->username) && !is_null($item->password)) {
$raw_proxy .= '@' . $item->username . ':' . $item->password;
}
if (!isPortOpen($item->proxy)) {
$db->updateStatus($item->proxy, 'port-closed');
echo $item->proxy . ' port closed' . PHP_EOL;
} else {
$proxy_types = [];
$anonymities = [];
$latencies = [];
$errors = [];
$checks = [
'http' => checkProxy($item->proxy, 'http', $endpoint, $headers, $item->username, $item->password),
'socks5' => checkProxy($item->proxy, 'socks5', $endpoint, $headers, $item->username, $item->password),
'socks4' => checkProxy($item->proxy, 'socks4', $endpoint, $headers, $item->username, $item->password)
];
foreach ($checks as $type => $check) {
if ($check['result']) {
$proxy_types[] = $type;
} else {
// echo "$type://{$item->proxy} error: {$check['error']}" . PHP_EOL;
$errors[] = "$type {$check['error']}";
}
if (!empty($check['anonymity'])) {
$anonymities[] = $check['anonymity'];
}
$latencies[] = !empty($check['latency']) ? $check['latency'] : -1;
}
if (!empty($proxy_types)) {
$merged_proxy_types = implode('-', $proxy_types);
echo $item->proxy . ' working ' . strtoupper($merged_proxy_types) . ' latency ' . max($latencies) . ' ms' . PHP_EOL;
$db->updateData($item->proxy, [
'type' => $merged_proxy_types,
'status' => 'active',
'latency' => max($latencies),
'username' => $item->username,
'password' => $item->password,
'https' => strpos($endpoint, 'https') !== false ? 'true' : 'false',
'anonymity' => implode('-', array_unique($anonymities))
]);
if (empty($item->webgl_renderer) || empty($item->browser_vendor) || empty($item->webgl_vendor)) {
$webgl = random_webgl_data();
$db->updateData($item->proxy, [
'webgl_renderer' => $webgl->webgl_renderer,
'webgl_vendor' => $webgl->webgl_vendor,
'browser_vendor' => $webgl->browser_vendor
]);
}
if (empty($item->timezone) || empty($item->country) || empty($item->lang)) {
foreach ($proxy_types as $type) {
get_geo_ip($item->proxy, $type, $db);
}
}
if (empty($item->useragent) && strlen(trim($item->useragent)) <= 5) {
$item->useragent = randomWindowsUa();
$db->updateData($item->proxy, ['useragent' => $item->useragent]);
}
} else {
$error = join(" ", $errors);
if (strpos($error, 'anonymity') !== false) {
$db->updateStatus($item->proxy, 'untested');
echo $item->proxy . ' failed obtain anoymity' . PHP_EOL;
} else {
$db->updateStatus($item->proxy, 'dead');
echo $item->proxy . ' dead' . PHP_EOL;
}
}
}
} else {
$raw_proxy = $item->proxy;
try {
$db->remove($item->proxy);
echo $item->proxy . ' invalid proxy -> deleted' . PHP_EOL;
} catch (Exception $e) {
$errorMessage = $e->getMessage();
// Handle or display the error message
echo "fail delete " . $item->proxy . ' : ' . $errorMessage . PHP_EOL;
}
}
// add to remover scheduler
if (!empty($raw_proxy)) {
$str_to_remove[] = $raw_proxy;
}
schedule_remover();
}