|
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\Providers; |
|
10
|
|
|
|
|
11
|
|
|
use Waca\Helpers\HttpHelper; |
|
12
|
|
|
use Waca\PdoDatabase; |
|
13
|
|
|
|
|
14
|
|
|
class TorExitProvider |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var PdoDatabase */ |
|
17
|
|
|
private $database; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* TorExitProvider constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param PdoDatabase $database |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(PdoDatabase $database) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->database = $database; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Checks whether an IP address is a Tor exit node for one of the pre-cached IP addresses. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $ip IP Address |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
public function isTorExit($ip) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$statement = $this->database->prepare('SELECT COUNT(1) FROM tornodecache WHERE ipaddr = :ip'); |
|
39
|
|
|
|
|
40
|
|
|
$statement->execute(array(':ip' => $ip)); |
|
41
|
|
|
|
|
42
|
|
|
$count = $statement->fetchColumn(); |
|
43
|
|
|
$statement->closeCursor(); |
|
44
|
|
|
|
|
45
|
|
|
if ($count > 0) { |
|
|
|
|
|
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
else { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function regenerate(PdoDatabase $database, HttpHelper $httpHelper, $destinationIps) |
|
54
|
|
|
{ |
|
55
|
|
|
$query = <<<SQL |
|
56
|
|
|
INSERT INTO tornodecache (ipaddr, exitaddr, exitport) |
|
57
|
|
|
VALUES (:ipaddr, :exitaddr, :exitport) |
|
58
|
|
|
ON DUPLICATE KEY |
|
59
|
|
|
UPDATE touched = CURRENT_TIMESTAMP, updateversion = updateversion + 1 |
|
60
|
|
|
SQL; |
|
61
|
|
|
|
|
62
|
|
|
$statement = $database->prepare($query); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($destinationIps as $ip) { |
|
65
|
|
|
echo 'Fetching data for ' . $ip . PHP_EOL; |
|
66
|
|
|
|
|
67
|
|
|
$statement->bindValue(':exitaddr', $ip); |
|
68
|
|
|
|
|
69
|
|
|
$http = $httpHelper->get( |
|
70
|
|
|
'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
71
|
|
|
array( |
|
72
|
|
|
'ip' => $ip, |
|
73
|
|
|
'port' => 80, |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
$https = $httpHelper->get( |
|
77
|
|
|
'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
78
|
|
|
array( |
|
79
|
|
|
'ip' => $ip, |
|
80
|
|
|
'port' => 443, |
|
81
|
|
|
)); |
|
82
|
|
|
|
|
83
|
|
|
foreach (array(80 => $http, 443 => $https) as $port => $response) { |
|
84
|
|
|
echo ' Running for port ' . $ip . ':' . $port . PHP_EOL; |
|
85
|
|
|
|
|
86
|
|
|
$statement->bindValue(':exitport', $port); |
|
87
|
|
|
|
|
88
|
|
|
$lines = explode("\n", $response); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($lines as $line) { |
|
91
|
|
|
// line contains a comment char, just skip the line. |
|
92
|
|
|
// This is OK as of 2016-04-06 --stw |
|
93
|
|
|
if (strpos($line, '#') !== false) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$statement->bindValue(':ipaddr', $line); |
|
98
|
|
|
$statement->execute(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
echo 'Done for ' . $ip . PHP_EOL; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// kill old cached entries |
|
106
|
|
|
$database->exec('DELETE FROM tornodecache WHERE touched < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.