/
TorExitNodes.php
212 lines (184 loc) · 5.96 KB
/
TorExitNodes.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
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
<?php
/**
* Prevents Tor exit nodes from editing a wiki.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Extensions
* @link https://www.mediawiki.org/wiki/Extension:TorBlock Documentation
*
* @author Andrew Garrett <andrew@epstone.net>
* @license GPL-2.0-or-later
*/
namespace MediaWiki\Extension\TorBlock;
use CachedBagOStuff;
use FormatJson;
use MediaWiki\MediaWikiServices;
use RequestContext;
use Wikimedia\IPUtils;
/**
* Collection of functions maintaining the list of Tor exit nodes.
*/
class TorExitNodes {
/**
* Determine if a given IP is a Tor exit node
*
* @param string|null $ip The IP address to check, or null to use the request IP
* @return bool True if an exit node, false otherwise
*/
public static function isExitNode( $ip = null ) {
if ( $ip == null ) {
$ip = RequestContext::getMain()->getRequest()->getIP();
}
return in_array( IPUtils::sanitizeIP( $ip ), self::getExitNodes() );
}
/**
* Get the array of Tor exit nodes using caching
*
* @return string[] List of Tor exit node addresses
*/
public static function getExitNodes() {
static $srvCache;
if ( $srvCache === null ) {
$srvCache = new CachedBagOStuff(
MediaWikiServices::getInstance()->getLocalServerObjectCache()
);
}
return $srvCache->getWithSetCallback(
$srvCache->makeGlobalKey( 'tor-exit-nodes' ),
$srvCache::TTL_HOUR,
function () {
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
return $wanCache->getWithSetCallback(
$wanCache->makeGlobalKey( 'tor-exit-nodes' ),
$wanCache::TTL_DAY,
function () {
return self::fetchExitNodes();
},
[
// Avoid stampedes on TOR list servers due to cache expiration
'lockTSE' => $wanCache::TTL_DAY,
'staleTTL' => $wanCache::TTL_DAY,
// Avoid stampedes on TOR list servers due to cache eviction
'busyValue' => []
]
);
}
);
}
/**
* Load the list of Tor exit nodes from the source and cache it for future use
*
* Do not call this method during HTTP GET/HEAD requests
*
* @return string[] List of Tor exit node addresses
*/
public static function loadExitNodes() {
$nodes = self::fetchExitNodes();
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$wanCache->set( $wanCache->makeGlobalKey( 'tor-exit-nodes' ), $nodes, $wanCache::TTL_DAY );
return $nodes;
}
/**
* Get the list of Tor exit nodes from the configured source
*
* @return string[] List of Tor exit node addresses
*/
private static function fetchExitNodes() {
wfDebugLog( 'torblock', "Loading Tor exit node list cold." );
if ( defined( 'MW_PHPUNIT_TEST' ) ) {
// TEST-NET-1, RFC 5737
return [ '192.0.2.111', '192.0.2.222' ];
}
return self::fetchExitNodesFromOnionooServer() ?: self::fetchExitNodesFromTorProject();
}
/**
* Get the list of Tor exit nodes from the Tor Project's website.
*
* @return string[] List of Tor exit node addresses
*/
private static function fetchExitNodesFromTorProject() {
global $wgTorIPs, $wgTorProjectCA, $wgTorBlockProxy;
$options = [
'caInfo' => is_readable( $wgTorProjectCA ) ? $wgTorProjectCA : null
];
if ( $wgTorBlockProxy ) {
$options['proxy'] = $wgTorBlockProxy;
}
$httpRequestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory();
$nodes = [];
foreach ( $wgTorIPs as $ip ) {
$url = 'https://check.torproject.org/torbulkexitlist?ip=' . $ip;
$data = $httpRequestFactory->get( $url, $options, __METHOD__ );
$lines = explode( "\n", $data );
foreach ( $lines as $line ) {
if ( strpos( $line, '#' ) === false ) {
$nodes[trim( $line )] = true;
}
}
}
return array_keys( $nodes );
}
/**
* Get the list of Tor exit nodes using the Onionoo protocol with the
* server specified in the configuration.
*
* @return string[] List of Tor exit node addresses
*/
private static function fetchExitNodesFromOnionooServer() {
global $wgTorOnionooServer, $wgTorOnionooCA, $wgTorBlockProxy;
$url = wfExpandUrl( "$wgTorOnionooServer/details?type=relay&running=true&flag=Exit",
PROTO_HTTPS );
$options = [
'caInfo' => is_readable( $wgTorOnionooCA ) ? $wgTorOnionooCA : null
];
if ( $wgTorBlockProxy ) {
$options['proxy'] = $wgTorBlockProxy;
}
$raw = MediaWikiServices::getInstance()->getHttpRequestFactory()->get( $url, $options, __METHOD__ );
$data = FormatJson::decode( $raw, true );
if ( !isset( $data['relays'] ) ) {
wfDebugLog( 'torblock', "Got no reply or an invalid reply from Onionoo.\n" );
return [];
}
$nodes = [];
foreach ( $data['relays'] as $relay ) {
$addresses = $relay['or_addresses'];
if ( isset( $relay['exit_addresses'] ) ) {
$addresses = array_merge( $addresses, $relay['exit_addresses'] );
}
foreach ( $addresses as $ip ) {
// Trim the port if it has one.
$portPosition = strrpos( $ip, ':' );
if ( $portPosition !== false ) {
$ip = substr( $ip, 0, $portPosition );
}
// Trim surrounding brackets for IPv6 addresses.
$hasBrackets = $ip[0] == '[';
if ( $hasBrackets ) {
$ip = substr( $ip, 1, -1 );
}
if ( !IPUtils::isValid( $ip ) ) {
wfDebug( 'Invalid IP address in Onionoo response.' );
continue;
}
$nodes[IPUtils::sanitizeIP( $ip )] = true;
}
}
return array_keys( $nodes );
}
}