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