-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxychecker.php
158 lines (126 loc) · 4.42 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
<?php
namespace ProxyChecker;
class ProxyChecker
{
private $proxyCheckUrl;
private $config = array(
'timeout' => 10,
'check' => array('get', 'post', 'cookie', 'referer', 'user_agent'),
);
public function __construct($proxyCheckUrl, array $config = array())
{
$this->proxyCheckUrl = $proxyCheckUrl;
$this->setConfig($config);
}
public function setConfig(array $config)
{
$this->config = array_merge($this->config, $config);
}
public function checkProxies(array $proxies)
{
$results = array();
foreach ($proxies as $proxy) {
try {
$results[$proxy] = $this->checkProxy($proxy);
} catch (\Exception $e) {
$results[$proxy]['error'] = $e->getMessage();
}
}
return $results;
}
public function checkProxy($proxy)
{
list($content, $info) = $this->getProxyContent($proxy);
return $this->checkProxyContent($content, $info);
}
private function getProxyContent($proxy)
{
@list($proxyIp, $proxyPassword, $proxyType) = explode(',', $proxy);
$ch = \curl_init();
$url = $this->proxyCheckUrl;
// check query
if (in_array('get', $this->config['check'])) {
$url .= '?q=query';
}
$options = array(
CURLOPT_URL => $url,
CURLOPT_PROXY => $proxyIp,
CURLOPT_HEADER => true,
CURLOPT_TIMEOUT => $this->config['timeout'],
CURLOPT_CONNECTTIMEOUT => $this->config['timeout'],
CURLOPT_RETURNTRANSFER => true
);
if (!empty($proxyPassword)) {
$options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
$options[CURLOPT_PROXYUSERPWD] = $proxyPassword;
}
// check post
if (in_array('post', $this->config['check'])) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = array(
'r' => 'request'
);
}
// check cookie
if (in_array('cookie', $this->config['check'])) {
$options[CURLOPT_COOKIE] = 'c=cookie';
}
// check refderer
if (in_array('referer', $this->config['check'])) {
$options[CURLOPT_REFERER] = 'http://www.google.com';
}
// check user agent
if (in_array('user_agent', $this->config['check'])) {
$options[CURLOPT_USERAGENT] = 'Mozila/4.0';
}
if (!empty($proxyType)) {
if ('http' == $proxyType) {
$options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
} else if ('socks4' == $proxyType) {
$options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS4;
} else if ('socks5' == $proxyType) {
$options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
}
}
\curl_setopt_array($ch, $options);
$content = \curl_exec($ch);
$info = \curl_getinfo($ch);
return array($content, $info);
}
private function checkProxyContent($content, $info)
{
if (!$content) {
throw new \Exception('Empty content');
}
if (!strpos($content, 'check this string in proxy response content')) {
throw new \Exception('Wrong content');
}
if (200 !== $info['http_code']) {
throw new \Exception('Code invalid: ' . $info['http_code']);
}
$allowed = array();
$disallowed = array();
foreach ($this->config['check'] as $value) {
if (strpos($content, "allow_$value")) {
$allowed[] = $value;
} else {
$disallowed[] = $value;
}
}
// proxy level
$proxyLevel = '';
if (strpos($content, 'proxylevel_elite')) {
$proxyLevel = 'elite';
} elseif (strpos($content, 'proxylevel_anonymous')) {
$proxyLevel = 'anonymous';
} elseif (strpos($content, 'proxylevel_transparent')) {
$proxyLevel = 'transparent';
}
return array(
'allowed' => $allowed,
'disallowed' => $disallowed,
'proxy_level' => $proxyLevel,
'info' => $info
);
}
}