-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplainhash.php
131 lines (122 loc) · 3.01 KB
/
plainhash.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
<?php
/**
* Lookup key we'll be using to map in RewriteMap condition for apache rules
* @var string $bankey
*/
$bankey = 'BIP';
/**
* Name of lockfile
* @var string $lockfile
*/
$lockfile = 'iplist.hash.lock';
require_once 'vendor/autoload.php';
if (file_exists(__DIR__.'/'.$lockfile) || file_exists(__DIR__.'/iplist.hash.lck'))
{
echo 'Lockfile exist, please remove and make sure no other instance are running before trying again'."\n";
echo 'Lockfile name '.__DIR__.'/'.$lockfile."\n";
exit(254);
}
try {
$lockhandle = fopen(__DIR__.'/'.$lockfile, 'w+');
}
catch (Exception $e)
{
echo 'Can\'t create lockfile '.__DIR__.'/'.$lockfile."\n";
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
exit(255);
}
$webClient = new \GuzzleHttp\Client();
try {
if (file_exists(__DIR__.'/iplist.hash.tmp'))
{
unlink(__DIR__.'/iplist.hash.tmp');
}
echo 'Opening hash file for writing temporary ip list'."\n".__DIR__.'/iplist.hash.tmp'."\n";
$db = fopen(__DIR__.'/iplist.hash.tmp','w+');
if ($db !== FALSE)
{
try {
echo 'Connecting to https://www.blocklist.de/ for blacklisted ips'."\n";
$infile = $webClient->get('https://www.blocklist.de/downloads/export-ips_all.txt')->getBody()->detach();
echo 'Writing ips to file'."\n";
while ($blackip = fgets($infile))
{
fwrite($db, rtrim($blackip,"\r\n")."\t".$bankey."\n");
}
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
}
try {
unset($infile);
echo 'Connecting to https://www.badips.com/ for blacklisted ips'."\n";
$infile = $webClient->get('http://www.badips.com/get/list/ssh/2')->getBody()->detach();
echo 'Writing ips to file'."\n";
while ($blackip = fgets ($infile))
{
fwrite($db, rtrim($blackip,"\r\n")."\t".$bankey."\n");
}
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
}
try
{
fclose($db);
if (file_exists(__DIR__.'/iplist.hash'))
{
echo 'Deleting '.__DIR__.'/iplist.hash'."\n";
unlink(__DIR__.'/iplist.hash');
}
echo 'Renaming '.__DIR__.'/iplist.hash.tmp'.' to'."\n".__DIR__.'/iplist.hash'."\n";
rename(__DIR__.'/iplist.hash.tmp', __DIR__.'/iplist.hash');
fclose($lockhandle);
if (file_exists(__DIR__.'/'.$lockfile))
{
unlink(__DIR__.'/'.$lockfile);
echo 'Removing lockfile'."\n";
}
exit(0);
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
fclose($lockhandle);
if (file_exists(__DIR__.'/'.$lockfile))
{
unlink(__DIR__.'/'.$lockfile);
echo 'Removing lockfile'."\n";
}
exit(255);
}
}
else {
echo 'Can\'t open hash file'."\n";
fclose($db);
fclose($lockhandle);
if (file_exists(__DIR__.'/'.$lockfile))
{
unlink(__DIR__.'/'.$lockfile);
echo 'Removing lockfile'."\n";
}
exit(255);
}
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
fclose($lockhandle);
if (file_exists(__DIR__.'/'.$lockfile))
{
unlink(__DIR__.'/'.$lockfile);
echo 'Removing lockfile'."\n";
}
exit(255);
}