-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxyCheckerReal.php.txt
234 lines (206 loc) · 6.77 KB
/
proxyCheckerReal.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
<?php
require_once __DIR__ . '/func-proxy.php';
global $isCli;
use PhpProxyHunter\Proxy;
use PhpProxyHunter\ProxyDB;
use PhpProxyHunter\Scheduler;
use PhpProxyHunter\Server;
Scheduler::$debug = false;
// re-check working proxies
// real check whether actual title same
$output_log = __DIR__ . '/proxyChecker.txt';
$max = 100;
$db = new ProxyDB(__DIR__ . '/src/database.sqlite');
$str = '';
$str = '8.220.136.174:9080';
// environment checks
if (!$isCli) {
// set output buffering to zero
ini_set('output_buffering', 0);
if (ob_get_level() == 0) {
ob_start();
}
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: text/plain; charset=UTF-8');
// setup lock file
$id = Server::getRequestIP();
if (empty($id)) {
$id = Server::useragent();
}
// lock file same as scanPorts.php
$webLockFile = tmp() . '/runners/real-web-' . sanitizeFilename($id) . '.lock';
if (file_exists($webLockFile) && !$isAdmin) {
exit(date(DATE_RFC3339) . ' another process still running (web lock file is locked) ' . basename(__FILE__, '.php') . PHP_EOL);
} else {
write_file($webLockFile, date(DATE_RFC3339));
// truncate output log file
truncateFile($output_log);
}
// delete web lock file after webserver closed
Scheduler::register(function () use ($webLockFile) {
delete_path($webLockFile);
}, 'webserver-close-' . md5(__FILE__));
// parse post data
if (isset($_REQUEST['proxy'])) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// post data with body key/name proxy
$parse = parsePostData();
if ($parse) {
if (isset($parse['proxy'])) {
$str = rawurldecode($parse['proxy']);
} else {
$str = rawurldecode(json_encode($parse));
}
}
} else {
// proxyCheckerReal.php.php?proxy=ANY_STRING_CONTAINS_PROXY
$str = rawurldecode($_REQUEST['proxy']);
}
}
// web server run parallel in background
// avoid bad response or hangs whole web server
$file = __FILE__;
$output_file = __DIR__ . '/proxyChecker.txt';
$cmd = "php " . escapeshellarg($file);
$runner = tmp() . "/runners/" . basename($webLockFile . '.lock') . ($isWin ? '.bat' : "");
$uid = getUserId();
$cmd .= " --userId=" . escapeshellarg($uid);
$cmd .= " --lockFile=" . escapeshellarg(unixPath($webLockFile));
$cmd .= " --runner=" . escapeshellarg(unixPath($runner));
$cmd .= " --max=" . escapeshellarg("30");
$cmd .= " --admin=" . escapeshellarg($isAdmin ? 'true' : 'false');
echo $cmd . "\n\n";
// Generate the command to run in the background
$cmd = sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, escapeshellarg($output_file), escapeshellarg($webLockFile));
// Write the command to the runner script
write_file($runner, $cmd);
// Execute the runner script in the background
runBashOrBatch($runner);
// Exit the PHP script
exit;
} else {
$short_opts = "p:m::";
$long_opts = [
"max::",
"userId::",
"lockFile::",
"runner::",
"admin::",
"proxy::"
];
$options = getopt($short_opts, $long_opts);
if (!empty($options['proxy'])) {
$str = rawurldecode($options['proxy']);
}
$isAdmin = !empty($options['admin']) && $options['admin'] !== 'false';
if (!$isAdmin) {
// only apply lock file for non-admin command
if (!empty($options['lockFile'])) {
if (file_exists($options['lockFile'])) {
exit(date(DATE_RFC3339) . ' another process still running (--lockFile is locked) ' . basename(__FILE__, '.php') . PHP_EOL);
}
write_file($options['lockFile'], '');
Scheduler::register(function () use ($options) {
delete_path($options['lockFile']);
}, 'release-cli-lock');
}
}
if (!empty($options['runner'])) {
// remove web server runner after finish
Scheduler::register(function () use ($options) {
delete_path($options['runner']);
}, 'release-runner-script');
}
if (!empty($options['max'])) {
$max_test = intval($options['max']);
if ($max_test > 0) {
$max = $max_test;
}
}
}
// process
if (!empty($str)) {
$proxies = extractProxies($str, $db, true);
$proxies = array_map(function (Proxy $item) {
return [
'proxy' => $item->proxy,
'username' => $item->username,
'password' => $item->password,
'last_check' => $item->last_check
];
}, $proxies);
} else {
$proxies = array_merge(
$db->getWorkingProxies($max),
$db->getUntestedProxies($max),
$db->getDeadProxies($max)
);
}
shuffle($proxies);
foreach ($proxies as $proxy) {
realCheck($proxy['proxy']);
}
function getPageTitle($html)
{
preg_match('/<title>(.*?)<\/title>/', $html, $matches);
return isset($matches[1]) ? $matches[1] : null;
}
function realCheck($proxy)
{
global $db;
echo str_repeat('=', 27);
echo "\nCHECKING $proxy\n";
echo str_repeat('=', 27);
echo PHP_EOL;
/**
* @var string[]
*/
$protocols = [];
$headers = [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language: en-US,en;q=0.9',
];
$checks = [
'socks4' => checkProxy($proxy, 'socks4', 'https://bing.com', $headers),
'http' => checkProxy($proxy, 'http', 'https://bing.com', $headers),
'socks5' => checkProxy($proxy, 'socks5', 'https://bing.com', $headers),
];
foreach ($checks as $proxyType => $cek) {
// $ch = $cek['curl'];
$log = "" . strtoupper($cek['type']) . "://" . $cek['proxy'] . "\n";
$log .= "RESULT: " . ($cek['result'] ? 'true' : 'false') . "\n";
if (!$cek['result']) {
$log .= 'ERROR: ' . trim("" . $cek['error']) . "\n";
}
$log .= trim($cek['response-headers']) . "\n";
$body = trim($cek['body']);
if (!empty($body)) {
$dom = \simplehtmldom\helper::str_get_html($body);
$title = trim("" . $dom->title());
$log .= "TITLE: $title\n";
if (strpos(strtolower($title), 'bing') !== false) {
$protocols[] = strtolower($cek['type']);
}
}
echo $log;
}
if (!empty($protocols)) {
$db->updateData($proxy, ['status' => 'active', 'type' => strtolower(implode('-', $protocols))]);
} else {
$db->updateData($proxy, ['status' => 'dead'], false);
}
}
function write_working()
{
global $db;
echo "[CHECKER-PARALLEL] writing working proxies" . PHP_EOL;
$data = parse_working_proxies($db);
file_put_contents(__DIR__ . '/working.txt', $data['txt']);
file_put_contents(__DIR__ . '/working.json', json_encode($data['array']));
file_put_contents(__DIR__ . '/status.json', json_encode($data['counter']));
return $data;
}
Scheduler::register('write_working', 'z_writing_working_proxies');