|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\ConsoleTasks; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use PDOException; |
|
13
|
|
|
use PDOStatement; |
|
14
|
|
|
use Waca\RegexConstants; |
|
15
|
|
|
use Waca\Tasks\ConsoleTaskBase; |
|
16
|
|
|
|
|
17
|
|
|
class RecreateTrustedIpTableTask extends ConsoleTaskBase |
|
18
|
|
|
{ |
|
19
|
|
|
public function execute() |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
echo "Fetching file...\n"; |
|
23
|
|
|
|
|
24
|
|
|
$htmlfile = file($this->getSiteConfiguration()->getXffTrustedHostsFile(), |
|
25
|
|
|
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
26
|
|
|
|
|
27
|
|
|
$ip = array(); |
|
28
|
|
|
$iprange = array(); |
|
29
|
|
|
$dnsdomain = array(); |
|
30
|
|
|
|
|
31
|
|
|
echo "Sorting file...\n"; |
|
32
|
|
|
$this->readFile($htmlfile, $iprange, $ip, $dnsdomain); |
|
33
|
|
|
|
|
34
|
|
|
echo "Exploding CIDRs...\n"; |
|
35
|
|
|
$this->explodeCidrs($iprange, $ip); |
|
36
|
|
|
|
|
37
|
|
|
echo "Resolving DNS...\n"; |
|
38
|
|
|
$this->resolveDns($dnsdomain, $ip); |
|
39
|
|
|
|
|
40
|
|
|
echo "Uniq-ing array...\n"; |
|
41
|
|
|
|
|
42
|
|
|
$ip = array_unique($ip); |
|
43
|
|
|
|
|
44
|
|
|
$database = $this->getDatabase(); |
|
45
|
|
|
|
|
46
|
|
|
$database->exec('DELETE FROM xfftrustcache;'); |
|
47
|
|
|
|
|
48
|
|
|
$insert = $database->prepare('INSERT INTO xfftrustcache (ip) VALUES (:ip);'); |
|
49
|
|
|
|
|
50
|
|
|
$this->doInserts($ip, $insert); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string[] $dnsDomains the DNS domains to resolve |
|
55
|
|
|
* @param string[] $ipAddresses existing array of IPs to add to |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function resolveDns($dnsDomains, &$ipAddresses) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($dnsDomains as $domain) { |
|
60
|
|
|
$ipList = gethostbynamel($domain); |
|
61
|
|
|
|
|
62
|
|
|
if ($ipList === false) { |
|
63
|
|
|
echo "Invalid DNS name $domain\n"; |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($ipList as $ipAddress) { |
|
68
|
|
|
$ipAddresses[] = $ipAddress; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// don't DoS |
|
72
|
|
|
usleep(10000); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $iprange |
|
78
|
|
|
* @param $ip |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function explodeCidrs($iprange, &$ip) |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($iprange as $r) { |
|
83
|
|
|
$ips = $this->getXffTrustProvider()->explodeCidr($r); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($ips as $i) { |
|
86
|
|
|
$ip[] = $i; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $htmlfile |
|
93
|
|
|
* @param $iprange |
|
94
|
|
|
* @param $ip |
|
95
|
|
|
* @param $dnsdomain |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function readFile($htmlfile, &$iprange, &$ip, &$dnsdomain) |
|
98
|
|
|
{ |
|
99
|
|
|
foreach ($htmlfile as $line_num => $rawline) { |
|
100
|
|
|
// remove the comments |
|
101
|
|
|
$hashPos = strpos($rawline, '#'); |
|
102
|
|
|
if ($hashPos !== false) { |
|
103
|
|
|
$line = substr($rawline, 0, $hashPos); |
|
104
|
|
|
} |
|
105
|
|
|
else { |
|
106
|
|
|
$line = $rawline; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$line = trim($line); |
|
110
|
|
|
|
|
111
|
|
|
// this was a comment or empty line... |
|
112
|
|
|
if ($line == "") { |
|
113
|
|
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// match a regex of an CIDR range: |
|
117
|
|
|
$ipcidr = '@' . RegexConstants::IPV4 . RegexConstants::IPV4_CIDR . '@'; |
|
118
|
|
|
if (preg_match($ipcidr, $line) === 1) { |
|
119
|
|
|
$iprange[] = $line; |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$ipnoncidr = '@' . RegexConstants::IPV4 . '@'; |
|
124
|
|
|
if (preg_match($ipnoncidr, $line) === 1) { |
|
125
|
|
|
$ip[] = $line; |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// it's probably a DNS name. |
|
130
|
|
|
$dnsdomain[] = $line; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $ip |
|
136
|
|
|
* @param PDOStatement $insert |
|
137
|
|
|
* |
|
138
|
|
|
* @throws Exception |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function doInserts($ip, PDOStatement $insert) |
|
141
|
|
|
{ |
|
142
|
|
|
$successful = true; |
|
143
|
|
|
|
|
144
|
|
|
foreach ($ip as $i) { |
|
145
|
|
|
if (count($i) > 15) { |
|
146
|
|
|
echo "Rejected $i\n"; |
|
147
|
|
|
$successful = false; |
|
148
|
|
|
|
|
149
|
|
|
continue; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
try { |
|
153
|
|
|
$insert->execute(array(':ip' => $i)); |
|
154
|
|
|
} |
|
155
|
|
|
catch (PDOException $ex) { |
|
156
|
|
|
echo "Exception on $i :\n"; |
|
157
|
|
|
echo $ex->getMessage(); |
|
158
|
|
|
$successful = false; |
|
159
|
|
|
break; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (!$successful) { |
|
164
|
|
|
throw new Exception('Encountered errors during transaction processing'); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |