@@ -20,40 +20,40 @@ |
||
20 | 20 | */ |
21 | 21 | class CachedRDnsLookupProvider implements IRDnsProvider |
22 | 22 | { |
23 | - private $database; |
|
23 | + private $database; |
|
24 | 24 | |
25 | - public function __construct(PdoDatabase $database) |
|
26 | - { |
|
27 | - $this->database = $database; |
|
28 | - } |
|
25 | + public function __construct(PdoDatabase $database) |
|
26 | + { |
|
27 | + $this->database = $database; |
|
28 | + } |
|
29 | 29 | |
30 | - public function getReverseDNS($address) |
|
31 | - { |
|
32 | - $address = trim($address); |
|
30 | + public function getReverseDNS($address) |
|
31 | + { |
|
32 | + $address = trim($address); |
|
33 | 33 | |
34 | - // lets look in our cache database first. |
|
35 | - $rDns = RDnsCache::getByAddress($address, $this->database); |
|
34 | + // lets look in our cache database first. |
|
35 | + $rDns = RDnsCache::getByAddress($address, $this->database); |
|
36 | 36 | |
37 | - if ($rDns instanceof RDnsCache) { |
|
38 | - // touch cache timer |
|
39 | - $rDns->save(); |
|
37 | + if ($rDns instanceof RDnsCache) { |
|
38 | + // touch cache timer |
|
39 | + $rDns->save(); |
|
40 | 40 | |
41 | - return $rDns->getData(); |
|
42 | - } |
|
41 | + return $rDns->getData(); |
|
42 | + } |
|
43 | 43 | |
44 | - // OK, it's not there, let's do an rDNS lookup. |
|
45 | - $result = @ gethostbyaddr($address); |
|
44 | + // OK, it's not there, let's do an rDNS lookup. |
|
45 | + $result = @ gethostbyaddr($address); |
|
46 | 46 | |
47 | - if ($result !== false) { |
|
48 | - $rDns = new RDnsCache(); |
|
49 | - $rDns->setDatabase($this->database); |
|
50 | - $rDns->setAddress($address); |
|
51 | - $rDns->setData($result); |
|
52 | - $rDns->save(); |
|
47 | + if ($result !== false) { |
|
48 | + $rDns = new RDnsCache(); |
|
49 | + $rDns->setDatabase($this->database); |
|
50 | + $rDns->setAddress($address); |
|
51 | + $rDns->setData($result); |
|
52 | + $rDns->save(); |
|
53 | 53 | |
54 | - return $result; |
|
55 | - } |
|
54 | + return $result; |
|
55 | + } |
|
56 | 56 | |
57 | - return null; |
|
58 | - } |
|
57 | + return null; |
|
58 | + } |
|
59 | 59 | } |
@@ -13,96 +13,96 @@ |
||
13 | 13 | |
14 | 14 | class TorExitProvider |
15 | 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 | - 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 |
|
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 | + 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 | 56 | INSERT INTO tornodecache (ipaddr, exitaddr, exitport) |
57 | 57 | VALUES (:ipaddr, :exitaddr, :exitport) |
58 | 58 | ON DUPLICATE KEY |
59 | 59 | UPDATE touched = CURRENT_TIMESTAMP, updateversion = updateversion + 1 |
60 | 60 | SQL; |
61 | 61 | |
62 | - $statement = $database->prepare($query); |
|
62 | + $statement = $database->prepare($query); |
|
63 | 63 | |
64 | - foreach ($destinationIps as $ip) { |
|
65 | - echo 'Fetching data for ' . $ip . PHP_EOL; |
|
64 | + foreach ($destinationIps as $ip) { |
|
65 | + echo 'Fetching data for ' . $ip . PHP_EOL; |
|
66 | 66 | |
67 | - $statement->bindValue(':exitaddr', $ip); |
|
67 | + $statement->bindValue(':exitaddr', $ip); |
|
68 | 68 | |
69 | - $http = $httpHelper->get( |
|
70 | - 'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
71 | - array( |
|
72 | - 'ip' => $ip, |
|
73 | - 'port' => 80, |
|
74 | - )); |
|
69 | + $http = $httpHelper->get( |
|
70 | + 'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
71 | + array( |
|
72 | + 'ip' => $ip, |
|
73 | + 'port' => 80, |
|
74 | + )); |
|
75 | 75 | |
76 | - $https = $httpHelper->get( |
|
77 | - 'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
78 | - array( |
|
79 | - 'ip' => $ip, |
|
80 | - 'port' => 443, |
|
81 | - )); |
|
76 | + $https = $httpHelper->get( |
|
77 | + 'https://check.torproject.org/cgi-bin/TorBulkExitList.py', |
|
78 | + array( |
|
79 | + 'ip' => $ip, |
|
80 | + 'port' => 443, |
|
81 | + )); |
|
82 | 82 | |
83 | - foreach (array(80 => $http, 443 => $https) as $port => $response) { |
|
84 | - echo ' Running for port ' . $ip . ':' . $port . PHP_EOL; |
|
83 | + foreach (array(80 => $http, 443 => $https) as $port => $response) { |
|
84 | + echo ' Running for port ' . $ip . ':' . $port . PHP_EOL; |
|
85 | 85 | |
86 | - $statement->bindValue(':exitport', $port); |
|
86 | + $statement->bindValue(':exitport', $port); |
|
87 | 87 | |
88 | - $lines = explode("\n", $response); |
|
88 | + $lines = explode("\n", $response); |
|
89 | 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 | - } |
|
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 | 96 | |
97 | - $statement->bindValue(':ipaddr', $line); |
|
98 | - $statement->execute(); |
|
99 | - } |
|
100 | - } |
|
97 | + $statement->bindValue(':ipaddr', $line); |
|
98 | + $statement->execute(); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - echo 'Done for ' . $ip . PHP_EOL; |
|
103 | - } |
|
102 | + echo 'Done for ' . $ip . PHP_EOL; |
|
103 | + } |
|
104 | 104 | |
105 | - // kill old cached entries |
|
106 | - $database->exec('DELETE FROM tornodecache WHERE touched < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)'); |
|
107 | - } |
|
105 | + // kill old cached entries |
|
106 | + $database->exec('DELETE FROM tornodecache WHERE touched < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)'); |
|
107 | + } |
|
108 | 108 | } |
109 | 109 | \ No newline at end of file |
@@ -44,8 +44,7 @@ |
||
44 | 44 | |
45 | 45 | if ($count > 0) { |
46 | 46 | return true; |
47 | - } |
|
48 | - else { |
|
47 | + } else { |
|
49 | 48 | return false; |
50 | 49 | } |
51 | 50 | } |
@@ -61,8 +61,7 @@ |
||
61 | 61 | $cacheEntry->save(); |
62 | 62 | |
63 | 63 | $cacheResult = $cacheEntry; |
64 | - } |
|
65 | - else { |
|
64 | + } else { |
|
66 | 65 | $data = $cacheResult->getData(); |
67 | 66 | } |
68 | 67 |
@@ -23,77 +23,77 @@ |
||
23 | 23 | */ |
24 | 24 | class CachedApiAntispoofProvider implements IAntiSpoofProvider |
25 | 25 | { |
26 | - /** |
|
27 | - * @var PdoDatabase |
|
28 | - */ |
|
29 | - private $database; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var HttpHelper |
|
33 | - */ |
|
34 | - private $httpHelper; |
|
35 | - |
|
36 | - public function __construct(PdoDatabase $database, HttpHelper $httpHelper) |
|
37 | - { |
|
38 | - $this->database = $database; |
|
39 | - $this->httpHelper = $httpHelper; |
|
40 | - } |
|
41 | - |
|
42 | - public function getSpoofs($username) |
|
43 | - { |
|
44 | - // FIXME: domains! |
|
45 | - /** @var Domain $domain */ |
|
46 | - $domain = Domain::getById(1, $this->database); |
|
47 | - |
|
48 | - /** @var AntiSpoofCache $cacheResult */ |
|
49 | - $cacheResult = AntiSpoofCache::getByUsername($username, $this->database); |
|
50 | - if ($cacheResult == false) { |
|
51 | - // get the data from the API |
|
52 | - $data = $this->httpHelper->get($domain->getWikiApiPath(), array( |
|
53 | - 'action' => 'antispoof', |
|
54 | - 'format' => 'php', |
|
55 | - 'username' => $username, |
|
56 | - )); |
|
57 | - |
|
58 | - $cacheEntry = new AntiSpoofCache(); |
|
59 | - $cacheEntry->setDatabase($this->database); |
|
60 | - $cacheEntry->setUsername($username); |
|
61 | - $cacheEntry->setData($data); |
|
62 | - $cacheEntry->save(); |
|
63 | - |
|
64 | - $cacheResult = $cacheEntry; |
|
65 | - } |
|
66 | - else { |
|
67 | - $data = $cacheResult->getData(); |
|
68 | - } |
|
69 | - |
|
70 | - $result = unserialize($data); |
|
71 | - |
|
72 | - if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) { |
|
73 | - $cacheResult->delete(); |
|
74 | - |
|
75 | - if (isset($result['error']['info'])) { |
|
76 | - throw new Exception("Unrecognised API response to query: " . $result['error']['info']); |
|
77 | - } |
|
78 | - |
|
79 | - throw new Exception("Unrecognised API response to query."); |
|
80 | - } |
|
81 | - |
|
82 | - if ($result['antispoof']['result'] == "pass") { |
|
83 | - // All good here! |
|
84 | - return array(); |
|
85 | - } |
|
86 | - |
|
87 | - if ($result['antispoof']['result'] == "conflict") { |
|
88 | - // we've got conflicts, let's do something with them. |
|
89 | - return $result['antispoof']['users']; |
|
90 | - } |
|
91 | - |
|
92 | - if ($result['antispoof']['result'] == "error") { |
|
93 | - // we've got conflicts, let's do something with them. |
|
94 | - throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']); |
|
95 | - } |
|
96 | - |
|
97 | - throw new Exception("Unrecognised API response to query."); |
|
98 | - } |
|
26 | + /** |
|
27 | + * @var PdoDatabase |
|
28 | + */ |
|
29 | + private $database; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var HttpHelper |
|
33 | + */ |
|
34 | + private $httpHelper; |
|
35 | + |
|
36 | + public function __construct(PdoDatabase $database, HttpHelper $httpHelper) |
|
37 | + { |
|
38 | + $this->database = $database; |
|
39 | + $this->httpHelper = $httpHelper; |
|
40 | + } |
|
41 | + |
|
42 | + public function getSpoofs($username) |
|
43 | + { |
|
44 | + // FIXME: domains! |
|
45 | + /** @var Domain $domain */ |
|
46 | + $domain = Domain::getById(1, $this->database); |
|
47 | + |
|
48 | + /** @var AntiSpoofCache $cacheResult */ |
|
49 | + $cacheResult = AntiSpoofCache::getByUsername($username, $this->database); |
|
50 | + if ($cacheResult == false) { |
|
51 | + // get the data from the API |
|
52 | + $data = $this->httpHelper->get($domain->getWikiApiPath(), array( |
|
53 | + 'action' => 'antispoof', |
|
54 | + 'format' => 'php', |
|
55 | + 'username' => $username, |
|
56 | + )); |
|
57 | + |
|
58 | + $cacheEntry = new AntiSpoofCache(); |
|
59 | + $cacheEntry->setDatabase($this->database); |
|
60 | + $cacheEntry->setUsername($username); |
|
61 | + $cacheEntry->setData($data); |
|
62 | + $cacheEntry->save(); |
|
63 | + |
|
64 | + $cacheResult = $cacheEntry; |
|
65 | + } |
|
66 | + else { |
|
67 | + $data = $cacheResult->getData(); |
|
68 | + } |
|
69 | + |
|
70 | + $result = unserialize($data); |
|
71 | + |
|
72 | + if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) { |
|
73 | + $cacheResult->delete(); |
|
74 | + |
|
75 | + if (isset($result['error']['info'])) { |
|
76 | + throw new Exception("Unrecognised API response to query: " . $result['error']['info']); |
|
77 | + } |
|
78 | + |
|
79 | + throw new Exception("Unrecognised API response to query."); |
|
80 | + } |
|
81 | + |
|
82 | + if ($result['antispoof']['result'] == "pass") { |
|
83 | + // All good here! |
|
84 | + return array(); |
|
85 | + } |
|
86 | + |
|
87 | + if ($result['antispoof']['result'] == "conflict") { |
|
88 | + // we've got conflicts, let's do something with them. |
|
89 | + return $result['antispoof']['users']; |
|
90 | + } |
|
91 | + |
|
92 | + if ($result['antispoof']['result'] == "error") { |
|
93 | + // we've got conflicts, let's do something with them. |
|
94 | + throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']); |
|
95 | + } |
|
96 | + |
|
97 | + throw new Exception("Unrecognised API response to query."); |
|
98 | + } |
|
99 | 99 | } |
@@ -13,11 +13,11 @@ |
||
13 | 13 | |
14 | 14 | class ClearExpiredIdentificationData extends ConsoleTaskBase |
15 | 15 | { |
16 | - /** |
|
17 | - * @return void |
|
18 | - */ |
|
19 | - public function execute() |
|
20 | - { |
|
21 | - IdentificationVerifier::clearExpiredCacheEntries($this->getSiteConfiguration(), $this->getDatabase()); |
|
22 | - } |
|
16 | + /** |
|
17 | + * @return void |
|
18 | + */ |
|
19 | + public function execute() |
|
20 | + { |
|
21 | + IdentificationVerifier::clearExpiredCacheEntries($this->getSiteConfiguration(), $this->getDatabase()); |
|
22 | + } |
|
23 | 23 | } |
@@ -101,8 +101,7 @@ |
||
101 | 101 | $hashPos = strpos($rawline, '#'); |
102 | 102 | if ($hashPos !== false) { |
103 | 103 | $line = substr($rawline, 0, $hashPos); |
104 | - } |
|
105 | - else { |
|
104 | + } else { |
|
106 | 105 | $line = $rawline; |
107 | 106 | } |
108 | 107 |
@@ -16,152 +16,152 @@ |
||
16 | 16 | |
17 | 17 | class RecreateTrustedIpTableTask extends ConsoleTaskBase |
18 | 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 (strlen($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 | - } |
|
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 (strlen($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 | 167 | } |
168 | 168 | \ No newline at end of file |
@@ -13,14 +13,14 @@ |
||
13 | 13 | |
14 | 14 | class UpdateTorExitTask extends ConsoleTaskBase |
15 | 15 | { |
16 | - /** |
|
17 | - * @return void |
|
18 | - */ |
|
19 | - public function execute() |
|
20 | - { |
|
21 | - TorExitProvider::regenerate( |
|
22 | - $this->getDatabase(), |
|
23 | - $this->getHttpHelper(), |
|
24 | - $this->getSiteConfiguration()->getTorExitPaths()); |
|
25 | - } |
|
16 | + /** |
|
17 | + * @return void |
|
18 | + */ |
|
19 | + public function execute() |
|
20 | + { |
|
21 | + TorExitProvider::regenerate( |
|
22 | + $this->getDatabase(), |
|
23 | + $this->getHttpHelper(), |
|
24 | + $this->getSiteConfiguration()->getTorExitPaths()); |
|
25 | + } |
|
26 | 26 | } |
27 | 27 | \ No newline at end of file |
@@ -142,8 +142,7 @@ |
||
142 | 142 | |
143 | 143 | if ($siteConfiguration->getLocationProviderApiKey() === null) { |
144 | 144 | $page->setLocationProvider(new FakeLocationProvider()); |
145 | - } |
|
146 | - else { |
|
145 | + } else { |
|
147 | 146 | $page->setLocationProvider( |
148 | 147 | new IpLocationProvider( |
149 | 148 | $database, |
@@ -24,162 +24,162 @@ |
||
24 | 24 | |
25 | 25 | abstract class ApplicationBase |
26 | 26 | { |
27 | - private $configuration; |
|
28 | - |
|
29 | - public function __construct(SiteConfiguration $configuration) |
|
30 | - { |
|
31 | - $this->configuration = $configuration; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Application entry point. |
|
36 | - * |
|
37 | - * Sets up the environment and runs the application, performing any global cleanup operations when done. |
|
38 | - */ |
|
39 | - public function run() |
|
40 | - { |
|
41 | - try { |
|
42 | - if ($this->setupEnvironment()) { |
|
43 | - $this->main(); |
|
44 | - } |
|
45 | - } |
|
46 | - catch (Exception $ex) { |
|
47 | - print $ex->getMessage(); |
|
48 | - } |
|
49 | - finally { |
|
50 | - $this->cleanupEnvironment(); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Environment setup |
|
56 | - * |
|
57 | - * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false |
|
58 | - * and shut down prematurely. |
|
59 | - * |
|
60 | - * @return bool |
|
61 | - * @throws EnvironmentException |
|
62 | - */ |
|
63 | - protected function setupEnvironment() |
|
64 | - { |
|
65 | - foreach ([ |
|
66 | - 'mbstring', // unicode and stuff |
|
67 | - 'pdo', |
|
68 | - 'pdo_mysql', // new database module |
|
69 | - 'session', |
|
70 | - 'date', |
|
71 | - 'pcre', // core stuff |
|
72 | - 'curl', // mediawiki api access etc |
|
73 | - 'openssl', // token generation |
|
74 | - ] as $x) { |
|
75 | - if (!extension_loaded($x)) { |
|
76 | - throw new EnvironmentException("PHP extension `$x` is required."); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - ini_set('user_agent', $this->getConfiguration()->getUserAgent()); |
|
81 | - |
|
82 | - $this->setupDatabase(); |
|
83 | - |
|
84 | - return true; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Checks the database is connectable and at the correct version |
|
89 | - * |
|
90 | - * @throws EnvironmentException |
|
91 | - * @throws Exception |
|
92 | - */ |
|
93 | - private function setupDatabase(): void |
|
94 | - { |
|
95 | - // check the schema version |
|
96 | - $database = PdoDatabase::getDatabaseConnection($this->getConfiguration()); |
|
97 | - |
|
98 | - $actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn(); |
|
99 | - if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) { |
|
100 | - throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.'); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return SiteConfiguration |
|
106 | - */ |
|
107 | - public function getConfiguration() |
|
108 | - { |
|
109 | - return $this->configuration; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Main application logic |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - abstract protected function main(); |
|
117 | - |
|
118 | - /** |
|
119 | - * Any cleanup tasks should go here |
|
120 | - * |
|
121 | - * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
122 | - * This should *only* be for cleaning up, no logic should go here. |
|
123 | - * |
|
124 | - * @return void |
|
125 | - */ |
|
126 | - abstract protected function cleanupEnvironment(); |
|
127 | - |
|
128 | - /** |
|
129 | - * @param ITask $page |
|
130 | - * @param SiteConfiguration $siteConfiguration |
|
131 | - * @param PdoDatabase $database |
|
132 | - * |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - protected function setupHelpers( |
|
136 | - ITask $page, |
|
137 | - SiteConfiguration $siteConfiguration, |
|
138 | - PdoDatabase $database |
|
139 | - ) { |
|
140 | - $page->setSiteConfiguration($siteConfiguration); |
|
141 | - |
|
142 | - // setup the global database object |
|
143 | - $page->setDatabase($database); |
|
144 | - |
|
145 | - // set up helpers and inject them into the page. |
|
146 | - $httpHelper = new HttpHelper($siteConfiguration); |
|
147 | - |
|
148 | - $page->setEmailHelper( |
|
149 | - new EmailHelper($siteConfiguration->getEmailSender(), $siteConfiguration->getIrcNotificationsInstance()) |
|
150 | - ); |
|
151 | - |
|
152 | - $page->setHttpHelper($httpHelper); |
|
153 | - |
|
154 | - if ($siteConfiguration->getLocationProviderApiKey() === null) { |
|
155 | - $page->setLocationProvider(new FakeLocationProvider()); |
|
156 | - } |
|
157 | - else { |
|
158 | - $page->setLocationProvider( |
|
159 | - new IpLocationProvider( |
|
160 | - $database, |
|
161 | - $siteConfiguration->getLocationProviderApiKey(), |
|
162 | - $httpHelper |
|
163 | - )); |
|
164 | - } |
|
165 | - |
|
166 | - $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database)); |
|
167 | - |
|
168 | - $page->setRdnsProvider(new CachedRDnsLookupProvider($database)); |
|
169 | - |
|
170 | - $page->setAntiSpoofProvider(new CachedApiAntispoofProvider($database, $httpHelper)); |
|
171 | - |
|
172 | - $page->setOAuthProtocolHelper(new OAuthProtocolHelper( |
|
173 | - $siteConfiguration->getOAuthConsumerToken(), |
|
174 | - $siteConfiguration->getOAuthConsumerSecret(), |
|
175 | - $database, |
|
176 | - $siteConfiguration->getUserAgent() |
|
177 | - )); |
|
178 | - |
|
179 | - $page->setNotificationHelper(new IrcNotificationHelper( |
|
180 | - $siteConfiguration, |
|
181 | - $database)); |
|
182 | - |
|
183 | - $page->setTorExitProvider(new TorExitProvider($database)); |
|
184 | - } |
|
27 | + private $configuration; |
|
28 | + |
|
29 | + public function __construct(SiteConfiguration $configuration) |
|
30 | + { |
|
31 | + $this->configuration = $configuration; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Application entry point. |
|
36 | + * |
|
37 | + * Sets up the environment and runs the application, performing any global cleanup operations when done. |
|
38 | + */ |
|
39 | + public function run() |
|
40 | + { |
|
41 | + try { |
|
42 | + if ($this->setupEnvironment()) { |
|
43 | + $this->main(); |
|
44 | + } |
|
45 | + } |
|
46 | + catch (Exception $ex) { |
|
47 | + print $ex->getMessage(); |
|
48 | + } |
|
49 | + finally { |
|
50 | + $this->cleanupEnvironment(); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Environment setup |
|
56 | + * |
|
57 | + * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false |
|
58 | + * and shut down prematurely. |
|
59 | + * |
|
60 | + * @return bool |
|
61 | + * @throws EnvironmentException |
|
62 | + */ |
|
63 | + protected function setupEnvironment() |
|
64 | + { |
|
65 | + foreach ([ |
|
66 | + 'mbstring', // unicode and stuff |
|
67 | + 'pdo', |
|
68 | + 'pdo_mysql', // new database module |
|
69 | + 'session', |
|
70 | + 'date', |
|
71 | + 'pcre', // core stuff |
|
72 | + 'curl', // mediawiki api access etc |
|
73 | + 'openssl', // token generation |
|
74 | + ] as $x) { |
|
75 | + if (!extension_loaded($x)) { |
|
76 | + throw new EnvironmentException("PHP extension `$x` is required."); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + ini_set('user_agent', $this->getConfiguration()->getUserAgent()); |
|
81 | + |
|
82 | + $this->setupDatabase(); |
|
83 | + |
|
84 | + return true; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Checks the database is connectable and at the correct version |
|
89 | + * |
|
90 | + * @throws EnvironmentException |
|
91 | + * @throws Exception |
|
92 | + */ |
|
93 | + private function setupDatabase(): void |
|
94 | + { |
|
95 | + // check the schema version |
|
96 | + $database = PdoDatabase::getDatabaseConnection($this->getConfiguration()); |
|
97 | + |
|
98 | + $actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn(); |
|
99 | + if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) { |
|
100 | + throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.'); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return SiteConfiguration |
|
106 | + */ |
|
107 | + public function getConfiguration() |
|
108 | + { |
|
109 | + return $this->configuration; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Main application logic |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + abstract protected function main(); |
|
117 | + |
|
118 | + /** |
|
119 | + * Any cleanup tasks should go here |
|
120 | + * |
|
121 | + * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
122 | + * This should *only* be for cleaning up, no logic should go here. |
|
123 | + * |
|
124 | + * @return void |
|
125 | + */ |
|
126 | + abstract protected function cleanupEnvironment(); |
|
127 | + |
|
128 | + /** |
|
129 | + * @param ITask $page |
|
130 | + * @param SiteConfiguration $siteConfiguration |
|
131 | + * @param PdoDatabase $database |
|
132 | + * |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + protected function setupHelpers( |
|
136 | + ITask $page, |
|
137 | + SiteConfiguration $siteConfiguration, |
|
138 | + PdoDatabase $database |
|
139 | + ) { |
|
140 | + $page->setSiteConfiguration($siteConfiguration); |
|
141 | + |
|
142 | + // setup the global database object |
|
143 | + $page->setDatabase($database); |
|
144 | + |
|
145 | + // set up helpers and inject them into the page. |
|
146 | + $httpHelper = new HttpHelper($siteConfiguration); |
|
147 | + |
|
148 | + $page->setEmailHelper( |
|
149 | + new EmailHelper($siteConfiguration->getEmailSender(), $siteConfiguration->getIrcNotificationsInstance()) |
|
150 | + ); |
|
151 | + |
|
152 | + $page->setHttpHelper($httpHelper); |
|
153 | + |
|
154 | + if ($siteConfiguration->getLocationProviderApiKey() === null) { |
|
155 | + $page->setLocationProvider(new FakeLocationProvider()); |
|
156 | + } |
|
157 | + else { |
|
158 | + $page->setLocationProvider( |
|
159 | + new IpLocationProvider( |
|
160 | + $database, |
|
161 | + $siteConfiguration->getLocationProviderApiKey(), |
|
162 | + $httpHelper |
|
163 | + )); |
|
164 | + } |
|
165 | + |
|
166 | + $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database)); |
|
167 | + |
|
168 | + $page->setRdnsProvider(new CachedRDnsLookupProvider($database)); |
|
169 | + |
|
170 | + $page->setAntiSpoofProvider(new CachedApiAntispoofProvider($database, $httpHelper)); |
|
171 | + |
|
172 | + $page->setOAuthProtocolHelper(new OAuthProtocolHelper( |
|
173 | + $siteConfiguration->getOAuthConsumerToken(), |
|
174 | + $siteConfiguration->getOAuthConsumerSecret(), |
|
175 | + $database, |
|
176 | + $siteConfiguration->getUserAgent() |
|
177 | + )); |
|
178 | + |
|
179 | + $page->setNotificationHelper(new IrcNotificationHelper( |
|
180 | + $siteConfiguration, |
|
181 | + $database)); |
|
182 | + |
|
183 | + $page->setTorExitProvider(new TorExitProvider($database)); |
|
184 | + } |
|
185 | 185 | } |
@@ -10,21 +10,21 @@ |
||
10 | 10 | |
11 | 11 | abstract class PublicInterfacePageBase extends PageBase |
12 | 12 | { |
13 | - /** |
|
14 | - * PublicInterfaceInternalPageBase constructor. |
|
15 | - */ |
|
16 | - public function __construct() |
|
17 | - { |
|
18 | - $this->template = 'publicbase.tpl'; |
|
19 | - } |
|
13 | + /** |
|
14 | + * PublicInterfaceInternalPageBase constructor. |
|
15 | + */ |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + $this->template = 'publicbase.tpl'; |
|
19 | + } |
|
20 | 20 | |
21 | - final public function execute() |
|
22 | - { |
|
23 | - parent::execute(); |
|
24 | - } |
|
21 | + final public function execute() |
|
22 | + { |
|
23 | + parent::execute(); |
|
24 | + } |
|
25 | 25 | |
26 | - final public function finalisePage() |
|
27 | - { |
|
28 | - parent::finalisePage(); |
|
29 | - } |
|
26 | + final public function finalisePage() |
|
27 | + { |
|
28 | + parent::finalisePage(); |
|
29 | + } |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -12,21 +12,21 @@ |
||
12 | 12 | |
13 | 13 | interface IRoutedTask extends ITask |
14 | 14 | { |
15 | - /** |
|
16 | - * Sets the route the request will take. Only should be called from the request router. |
|
17 | - * |
|
18 | - * @param $routeName string |
|
19 | - * |
|
20 | - * @return void |
|
21 | - * |
|
22 | - * @throws Exception |
|
23 | - * @category Security-Critical |
|
24 | - */ |
|
25 | - public function setRoute($routeName); |
|
15 | + /** |
|
16 | + * Sets the route the request will take. Only should be called from the request router. |
|
17 | + * |
|
18 | + * @param $routeName string |
|
19 | + * |
|
20 | + * @return void |
|
21 | + * |
|
22 | + * @throws Exception |
|
23 | + * @category Security-Critical |
|
24 | + */ |
|
25 | + public function setRoute($routeName); |
|
26 | 26 | |
27 | - /** |
|
28 | - * Gets the name of the route that has been passed from the request router. |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function getRouteName(); |
|
27 | + /** |
|
28 | + * Gets the name of the route that has been passed from the request router. |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function getRouteName(); |
|
32 | 32 | } |
33 | 33 | \ No newline at end of file |