1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ _ ___ ____ ____ ____ |
5
|
|
|
* / \ | |__ _ _ ___ ___|_ _| _ \| _ \| __ ) |
6
|
|
|
* / _ \ | '_ \| | | / __|/ _ \| || |_) | | | | _ \ |
7
|
|
|
* / ___ \| |_) | |_| \__ \ __/| || __/| |_| | |_) | |
8
|
|
|
* /_/ \_\_.__/ \__,_|___/\___|___|_| |____/|____/ |
9
|
|
|
* |
10
|
|
|
* This file is part of Kristuff\AbsuseIPDB. |
11
|
|
|
* |
12
|
|
|
* (c) Kristuff <[email protected]> |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* @version 0.9.10 |
18
|
|
|
* @copyright 2020-2021 Kristuff |
19
|
|
|
*/ |
20
|
|
|
namespace Kristuff\AbuseIPDB; |
21
|
|
|
|
22
|
|
|
use Kristuff\AbuseIPDB\QuietApiHandler; |
23
|
|
|
use Kristuff\Mishell\Program; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AbstractClient |
27
|
|
|
* |
28
|
|
|
* Abstract base class for main program |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractClient extends ShellUtils |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
const SHORT_ARGUMENTS = "o:S:GLBK:C:d:R:c:m:l:E:V:hvs:"; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const LONG_ARGUMENTS = ['output:','save-key:', 'config', 'list', 'blacklist', 'check:', 'check-block:', 'days:', 'report:', 'categories:', 'message:', 'limit:', 'clear:','bulk-report:', 'help', 'verbose', 'score:','version']; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
const VERSION = 'v0.9.10'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var QuietApiHandler |
50
|
|
|
*/ |
51
|
|
|
protected static $api = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected static $keyPath = __DIR__ .'/../config/key.json'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected static $basicCommands = [ |
62
|
|
|
['h', 'help', 'printHelp'], |
63
|
|
|
['version', 'version', 'printVersion'], // no short arg |
64
|
|
|
['S', 'save-key', 'registerApiKey'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
protected static $mainCommands = [ |
71
|
|
|
['G', 'config', 'printConfig'], |
72
|
|
|
['L', 'list', 'printCategories'], |
73
|
|
|
['C', 'check', 'checkIP'], |
74
|
|
|
['K', 'check-block', 'checkBlock'], |
75
|
|
|
['R', 'report', 'reportIP'], |
76
|
|
|
['V', 'bulk-report', 'bulkReport'], |
77
|
|
|
['B', 'blacklist', 'getBlacklist'], |
78
|
|
|
['E', 'clear', 'clearIP'], |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Parse command |
83
|
|
|
* |
84
|
|
|
* @access public |
85
|
|
|
* @static |
86
|
|
|
* @param array $arguments |
87
|
|
|
* @param string $keyPath The configuration file path |
88
|
|
|
* |
89
|
|
|
* @return bool true is the command has been found, otherwise false |
90
|
|
|
*/ |
91
|
|
|
protected static function parseCommand(array $arguments, string $keyPath) |
92
|
|
|
{ |
93
|
|
|
foreach( self::$basicCommands as $cmd){ |
94
|
|
|
if (self::inArguments($arguments, $cmd[0], $cmd[1])){ |
95
|
|
|
call_user_func(__NAMESPACE__ .'\AbuseIPDBClient::' . $cmd[2], $cmd[2]=== 'registerApiKey' ? $arguments : null); |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
foreach( self::$mainCommands as $cmd){ |
100
|
|
|
if (self::inArguments($arguments, $cmd[0], $cmd[1])){ |
101
|
|
|
self::createHandler($keyPath); |
102
|
|
|
self::setOutputFormat($arguments); |
103
|
|
|
call_user_func(__NAMESPACE__ .'\AbuseIPDBClient::' . $cmd[2], $arguments); |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get and register output format |
112
|
|
|
* |
113
|
|
|
* @access protected |
114
|
|
|
* @static |
115
|
|
|
* @param array $arguments The list of arguments |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
protected static function setOutputFormat(array $arguments) |
121
|
|
|
{ |
122
|
|
|
$given = self::getArgumentValue($arguments, 'o', 'output') ?? 'default'; |
123
|
|
|
$output = empty($given) ? 'default' : $given; |
124
|
|
|
self::validate(in_array($output, ['default', 'json', 'plaintext']),'Invalid output argument given.'); |
125
|
|
|
self::$outputFormat = $output ; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Check for install then create and register ApiHandler |
130
|
|
|
* |
131
|
|
|
* @access public |
132
|
|
|
* @static |
133
|
|
|
* @param string $configPath The configuration file path |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
* @throws \InvalidArgumentException If the given file does not exist |
137
|
|
|
* @throws \Kristuff\AbuseIPDB\InvalidPermissionException If the given file is not readable |
138
|
|
|
*/ |
139
|
|
|
protected static function createHandler(string $keyPath) |
140
|
|
|
{ |
141
|
|
|
self::$keyPath = $keyPath; |
142
|
|
|
self::validate( self::checkForInstall(), 'Key file missing.'); |
143
|
|
|
try { |
144
|
|
|
self::$api = self::fromConfigFile(self::$keyPath); |
145
|
|
|
} catch (\Exception $e) { |
146
|
|
|
self::error($e->getMessage()); |
147
|
|
|
self::printFooter(); |
148
|
|
|
Program::exit(1); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check for install |
154
|
|
|
* |
155
|
|
|
* @access protected |
156
|
|
|
* @static |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
protected static function checkForInstall() |
161
|
|
|
{ |
162
|
|
|
return file_exists(self::$keyPath); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get a new instance of ApiHandler with config stored in a Json file |
167
|
|
|
* |
168
|
|
|
* @access public |
169
|
|
|
* @static |
170
|
|
|
* @param string $configPath The configuration file path |
171
|
|
|
* |
172
|
|
|
* @return \Kristuff\AbuseIPDB\ApiHandler |
173
|
|
|
* @throws \InvalidArgumentException If the given file does not exist |
174
|
|
|
* @throws \Kristuff\AbuseIPDB\InvalidPermissionException If the given file is not readable |
175
|
|
|
*/ |
176
|
|
|
public static function fromConfigFile(string $configPath) |
177
|
|
|
{ |
178
|
|
|
// check file exists |
179
|
|
|
if (!file_exists($configPath) || !is_file($configPath)){ |
180
|
|
|
throw new \InvalidArgumentException('The file [' . $configPath . '] does not exist.'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// check file is readable |
184
|
|
|
if (!is_readable($configPath)){ |
185
|
|
|
throw new InvalidPermissionException('The file [' . $configPath . '] is not readable.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$keyConfig = self::loadJsonFile($configPath); |
189
|
|
|
$selfIps = []; |
190
|
|
|
|
191
|
|
|
// Look for other optional config files in the same directory |
192
|
|
|
$selfIpsConfigPath = pathinfo($configPath, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . 'self_ips.json'; |
|
|
|
|
193
|
|
|
if (file_exists($selfIpsConfigPath)){ |
194
|
|
|
$selfIps = self::loadJsonFile($selfIpsConfigPath)->self_ips; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$app = new QuietApiHandler($keyConfig->api_key, $selfIps); |
198
|
|
|
|
199
|
|
|
return $app; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |