-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.proxy.php
142 lines (130 loc) · 4.77 KB
/
class.proxy.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
<?php
class proxy {
public function __construct() {
}
public function publishProxy($https = false) {
if ($https) {
$url = 'http://www.tkdaili.com/api/getiplist.aspx?vkey=2C777C9751352F3D8C99355ED68252A2&num=100&country=CN&high=1&https=1&style=2';
$exchangeName = 'e_proxy_https';
$queueName = 'q_proxy_https';
$routerName = 'proxy_https';
}
else {
$url = 'http://www.tkdaili.com/api/getiplist.aspx?vkey=2C777C9751352F3D8C99355ED68252A2&num=100&country=CN&high=1&style=2';
$exchangeName = 'e_proxy';
$queueName = 'q_proxy';
$routerName = 'proxy';
}
$userAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$info = curl_exec($ch);
if(curl_errno($ch))
{
echo curl_error($ch);
}
curl_close($ch);
$content = trim($info);
$arr = explode("\n", $content);
$params = array('host' =>'10.168.45.191',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/kwd');
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName($exchangeName);
foreach ($arr as $proxy) {
$exchange->publish(trim($proxy), $routerName);
}
$conn->disconnect();
}
public function getProxy($https = false) {
if ($https) {
$queueName = 'q_proxy_https';
}
else {
$queueName = 'q_proxy';
}
$params = array('host' =>'10.168.45.191',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/kwd');
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$message = $queue->get(AMQP_AUTOACK);
$proxy = $message->getBody();
while (!$this->_testProxy($proxy, $https)) {
$message = $queue->get(AMQP_AUTOACK);
$proxy = $message->getBody();
}
$conn->disconnect();
return $proxy;
}
public function _testProxy($proxy, $https = false) {
if ($https) {
$url = 'https://login.taobao.com/member/login.jhtml';
$timeout = 5;
}
else {
$url = 'http://www.taobao.com?spm=1.7274553.1997517345.1.7V4oN5';
$timeout = 3;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if ($https) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$info = curl_exec($ch);
if(curl_errno($ch))
{
curl_close($ch);
return false;
}
else {
curl_close($ch);
return true;
}
}
public function getTotalNum() {
$params = array('host' =>'localhost',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/kwd');
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$queue = new AMQPQueue($channel);
$queue->setName('q_proxy');
$queue->setFlags(AMQP_PASSIVE);
$messageCount = $queue->declare();
return $messageCount;
}
public function test() {
$params = array('host' =>'localhost',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/kwd');
$conn = new AMQPConnection($params);
$conn->connect();
$channel = new AMQPChannel($conn);
$exchange = new AMQPExchange($channel);
$exchange->setName('e_proxy');
$exchange->publish($message, 'proxy');
}
}