-
Notifications
You must be signed in to change notification settings - Fork 0
/
plaintext.php
109 lines (101 loc) · 2.54 KB
/
plaintext.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
<?php
/**
* Name of lockfile
* @var string $lockfile
*/
$lockfile = 'iplist.txt.lock';
require_once 'vendor/autoload.php';
if (file_exists(__DIR__.'/'.$lockfile))
{
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);
}
try {
$webClient = new \GuzzleHttp\Client();
try {
if (file_exists(__DIR__.'/iplist.txt.tmp'))
{
unlink(__DIR__.'/iplist.txt.tmp');
}
echo 'Opening file for writing temporary ip list'."\n".__DIR__.'/iplist.txt.tmp'."\n";
$outfile = fopen(__DIR__.'/iplist.txt.tmp','w+');
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 ($outfile, rtrim($blackip,"\r\n")."\n");
}
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
}
try {
echo 'Connecting to https://www.badips.com/ for blacklisted ips'."\n";
unset($infile);
$infile = $webClient->get('http://www.badips.com/get/list/ssh/2')->getBody()->detach();
echo 'Writing ips to file'."\n";
while ($blackip = fgets ($infile))
{
fwrite ($outfile, rtrim($blackip,"\r\n")."\n");
}
}
catch (Exception $e)
{
echo 'Error Caught !'."\n";
echo $e->getMessage()."\n";
}
try {
fclose ($outfile);
echo 'File closed'."\n";
if (file_exists(__DIR__.'/iplist.txt'))
{
echo 'Deleting '.__DIR__.'/iplist.txt'."\n";
unlink(__DIR__.'/iplist.txt');
}
echo 'Renaming '.__DIR__.'/iplist.txt.tmp'.' to'."\n".__DIR__.'/iplist.txt'."\n";
rename(__DIR__.'/iplist.txt.tmp', __DIR__.'/iplist.txt');
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";
}
}
}
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";
}
}