-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxyChecker.php.txt
357 lines (314 loc) · 11.5 KB
/
proxyChecker.php.txt
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
<?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: dimaslanjaka@gmail.com
*/
require_once __DIR__ . "/func-proxy.php";
use PhpProxyHunter\ProxyDB;
$db = new ProxyDB();
$isCli = (php_sapi_name() === 'cli' || defined('STDIN') || (empty($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT']) && count($_SERVER['argv']) > 0));
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);
// ignore limitation if exists
if (function_exists('set_time_limit')) {
if (gethostname() == "DESKTOP-JVTSJ6I") {
call_user_func('set_time_limit', 0);
} else {
call_user_func('set_time_limit', $maxExecutionTime);
}
}
// 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'];
echo $config['user_id'] . ' ' . date("Y-m-d H:i:s") . PHP_EOL;
echo "GET $endpoint " . strtoupper($checksFor) . PHP_EOL;
echo implode("\n", $headers) . PHP_EOL;
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";
}
}
echo "\n";
/// FUNCTIONS (DO NOT EDIT)
$lockFilePath = __DIR__ . "/proxyChecker.lock";
$statusFile = __DIR__ . "/status.txt";
if (file_exists($lockFilePath)) {
echo "another process still running\n";
exit();
} else {
file_put_contents($lockFilePath, $config['user_id'] . '=' . json_encode($config));
file_put_contents($statusFile, 'running');
}
function exitProcess()
{
global $lockFilePath, $statusFile;
if (file_exists($lockFilePath)) unlink($lockFilePath);
file_put_contents($statusFile, 'idle');
}
register_shutdown_function('exitProcess');
// Specify the file path
$filePath = __DIR__ . "/proxies.txt";
$socksPath = __DIR__ . "/socks.txt";
$socksWorkingPath = __DIR__ . '/socks-working.txt';
$workingPath = __DIR__ . "/working.txt";
$deadPath = __DIR__ . "/dead.txt";
$workingProxies = [];
$socksWorkingProxies = [];
setFilePermissions([$filePath, $workingPath, $deadPath]);
/**
* run proxies check shuffled
*/
function shuffleChecks()
{
global $filePath, $workingPath, $workingProxies, $deadPath, $isCli, $db;
// Read lines of the file into an array
$untested = extractProxies(file_get_contents($filePath));
$working = extractProxies(file_get_contents($workingPath));
$lines = array_merge($untested, $working);
$lines = array_map(function ($line) {
return trim($line->proxy);
}, $working);
$lines = array_unique($lines);
shuffle($lines);
// rewrite untested proxies
file_put_contents($filePath, join("\n", $lines));
if (empty($lines) || count($lines) < 30) {
if (file_exists($deadPath)) {
echo "proxies low, respawning dead proxies\n\n";
// respawn 100 dead proxies
moveLinesToFile($deadPath, $filePath, 100);
// repeat
return shuffleChecks();
} else {
echo "no proxies to respawn";
exit();
}
}
// Shuffle the array
shuffle($lines);
// Iterate through the shuffled lines
foreach (array_unique($lines) as $line) {
if (!$isCli && ob_get_level() > 0) {
// LIVE output buffering on web server
flush();
ob_flush();
}
$from_db = $db->select($line);
if (!empty($from_db)) {
$dbproxy = $from_db[0];
if (!is_null($dbproxy) && isset($dbproxy['last_check']) && !is_null($dbproxy['last_check']) && !isDateRFC3339OlderThanHours($dbproxy['last_check'], 5)) {
// skip proxy already checked less than 5 hours ago
echo "$line already checked" . PHP_EOL;
continue;
}
}
if (checkProxyLine($line) == "break") break;
// move to dead.txt checked proxy
removeStringAndMoveToFile($filePath, $deadPath, trim($line));
}
// rewrite all working proxies
if (count($workingProxies) > 1) {
file_put_contents($workingPath, join("\n", $workingProxies));
}
}
/**
* check single proxy and append into global working proxies
* @return string break, success, failed
* * break: the execution time excedeed
* * success: proxy working
* * failed: proxy not working
*/
function checkProxyLine($line)
{
global $startTime, $maxExecutionTime, $workingProxies, $checksFor, $socksWorkingProxies, $db, $filePath, $deadPath, $endpoint, $headers;
// Check if the elapsed time exceeds the limit
if ((microtime(true) - $startTime) > $maxExecutionTime) {
echo "maximum execution time excedeed ($maxExecutionTime)\n";
// Execution time exceeded, break out of the loop
return "break";
}
$proxy = trim($line);
list($ip, $port) = explode(':', $proxy);
$geoUrl = "http://ip-get-geolocation.com/api/json/$ip";
if (!isPortOpen($proxy)) {
echo "$proxy port closed\n";
$db->updateStatus($proxy, 'port-closed');
return "failed";
}
$successType = [];
if (strpos($checksFor, 'http') !== false) {
$check = checkProxy($proxy, 'http', $endpoint, $headers);
if ($check['result'] !== false) {
echo "$proxy working type HTTP";
$latency = $check['latency'];
if ($check['private']) {
$db->update($proxy, 'http', null, null, null, 'private', $latency);
} else {
$db->update($proxy, 'http', null, null, null, 'active', $latency);
}
echo " latency $latency ms\n";
$item = "$proxy|$latency|HTTP";
// fetch ip info
$geoIp = json_decode(curlGetWithProxy($geoUrl, $proxy, 'http'), true);
// Check if JSON decoding was successful
if ($geoIp !== null && json_last_error() === JSON_ERROR_NONE) {
if (trim($geoIp['status']) != 'fail') {
$item .= "|" . implode("|", [$geoIp['region'], $geoIp['city'], $geoIp['country'], $geoIp['timezone']]);
$db->update($proxy, null, $geoIp['region'], $geoIp['city'], $geoIp['country'], null, null, $geoIp['timezone']);
} else {
$cachefile = curlGetCache($geoUrl);
if (file_exists($cachefile)) unlink($cachefile);
}
}
if (!in_array($item, $workingProxies)) {
// If the item doesn't exist, push it into the array
$workingProxies[] = $item;
}
// return "success";
$successType[] = 'http';
}
}
if (strpos($checksFor, 'socks5') !== false) {
$check = checkProxy($proxy, 'socks5', $endpoint, $headers);
if ($check['result'] !== false) {
echo "$proxy working type SOCKS5\n";
$latency = $check['latency'];
if ($check['private']) {
$db->update($proxy, 'socks5', null, null, null, 'private', $latency);
} else {
$db->update($proxy, 'socks5', null, null, null, 'active', $latency);
}
$item = "$proxy|$latency|SOCKS5";
// fetch ip info
$geoIp = json_decode(curlGetWithProxy($geoUrl, $proxy, 'socks5'), true);
// Check if JSON decoding was successful
if ($geoIp !== null && json_last_error() === JSON_ERROR_NONE) {
if (trim($geoIp['status']) != 'fail') {
$item .= "|" . implode("|", [$geoIp['region'], $geoIp['city'], $geoIp['country'], $geoIp['timezone']]);
$db->update($proxy, null, $geoIp['region'], $geoIp['city'], $geoIp['country'], null, null, $geoIp['timezone']);
} else {
$cachefile = curlGetCache($geoUrl);
if (file_exists($cachefile)) unlink($cachefile);
}
}
if (!in_array($item, $socksWorkingProxies)) {
// If the item doesn't exist, push it into the array
$socksWorkingProxies[] = $item;
}
// return "success";
$successType[] = 'socks5';
}
}
if (strpos($checksFor, 'socks4') !== false) {
$check = checkProxy($proxy, 'socks4', $endpoint, $headers);
if ($check['result'] !== false) {
echo "$proxy working type SOCKS4\n";
$latency = $check['latency'];
if ($check['private']) {
$db->update($proxy, 'socks4', null, null, null, 'private', $latency);
} else {
$db->update($proxy, 'socks4', null, null, null, 'active', $latency);
}
$item = "$proxy|$latency|SOCKS4";
// fetch ip info
$geoIp = json_decode(curlGetWithProxy($geoUrl, $proxy, 'socks4'), true);
// Check if JSON decoding was successful
if ($geoIp !== null && json_last_error() === JSON_ERROR_NONE) {
if (trim($geoIp['status']) != 'fail') {
$item .= "|" . implode("|", [$geoIp['region'], $geoIp['city'], $geoIp['country'], $geoIp['timezone']]);
$db->update($proxy, null, $geoIp['region'], $geoIp['city'], $geoIp['country'], null, null, $geoIp['timezone']);
} else {
$cachefile = curlGetCache($geoUrl);
if (file_exists($cachefile)) unlink($cachefile);
}
}
if (!in_array($item, $socksWorkingProxies)) {
// If the item doesn't exist, push it into the array
$socksWorkingProxies[] = $item;
}
// return "success";
$successType[] = 'socks4';
}
}
if (!empty($successType)) {
$db->update($proxy, implode('-', $successType));
return "success";
}
echo "$proxy not working\n";
// remove dead proxy from check list
$db->update($proxy, null, null, null, null, 'dead');
// removeStringAndMoveToFile($filePath, $deadPath, trim($proxy));
return "failed";
}
/// FUNCTIONS ENDS
// main script
function main()
{
global $filePath, $deadPath;
// move backup added proxies
$backup = __DIR__ . '/proxies-backup.txt';
if (file_exists($backup)) {
if (moveContent($backup, $filePath)) {
unlink($backup);
}
}
// filter only IP:PORT each lines
rewriteIpPortFile($filePath);
rewriteIpPortFile($deadPath);
// remove duplicate proxies
removeDuplicateLines($filePath);
removeDuplicateLines($deadPath);
shuffleChecks();
// sequentalChecks();
}
main();