-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblacklist-cli.js
More file actions
executable file
·279 lines (255 loc) · 8.11 KB
/
Copy pathblacklist-cli.js
File metadata and controls
executable file
·279 lines (255 loc) · 8.11 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node
'use strict';
const net = require('net');
const path = require('path');
const sessions = require('./sessions');
const IPC_SOCKET_PATH = path.join(__dirname, 'ohproxy.sock');
const args = process.argv.slice(2);
const command = args[0];
function usage() {
console.log(`
IP Blacklist CLI
Usage:
node blacklist-cli.js <command> [options]
Commands:
list [--all] [--json] List active blacklist entries
pending [--json] List IPs inside the login grace window
check <ip> Show blacklist and pending state for an IP
add <ip> [--reason text] [--expires time|never]
Add or update a manual blacklist entry
remove <ip> Remove a blacklist entry and clear pending state
clear expired|auto|manual|all Clear blacklist entries by scope
Time format:
Nsecs, Nmins, Nhours, Ndays, or never
Examples:
node blacklist-cli.js list
node blacklist-cli.js pending
node blacklist-cli.js add 203.0.113.10 --reason "manual block" --expires 7days
node blacklist-cli.js remove 203.0.113.10
node blacklist-cli.js clear expired
`);
}
function fail(message) {
console.error(`Error: ${message}`);
process.exit(1);
}
function validateIp(ip) {
const value = String(ip || '').trim();
if (!value || !net.isIP(value)) fail('A valid IPv4 or IPv6 address is required');
return value;
}
function formatDate(timestamp) {
if (!timestamp) return 'never';
return new Date(timestamp * 1000).toISOString().replace('T', ' ').substring(0, 19);
}
function parseTimeString(value) {
const text = String(value || '').trim().toLowerCase();
if (!text || text === 'never') return null;
const match = text.match(/^(\d+)(secs?|mins?|hours?|days?)$/);
if (!match) return undefined;
const amount = Number(match[1]);
const unit = match[2];
if (!Number.isInteger(amount) || amount < 1) return undefined;
if (unit.startsWith('sec')) return amount;
if (unit.startsWith('min')) return amount * 60;
if (unit.startsWith('hour')) return amount * 60 * 60;
if (unit.startsWith('day')) return amount * 24 * 60 * 60;
return undefined;
}
function optionValue(name, fallback = '') {
const index = args.indexOf(name);
if (index < 0) return fallback;
const value = args[index + 1];
if (!value || value.startsWith('--')) fail(`${name} requires a value`);
return value;
}
function hasFlag(name) {
return args.includes(name);
}
function sendIpcMessage(action, payload = {}) {
return new Promise((resolve) => {
const client = net.createConnection(IPC_SOCKET_PATH, () => {
client.write(JSON.stringify({ action, payload }) + '\n');
});
let buffer = '';
client.on('data', (data) => {
buffer += data.toString();
if (!buffer.includes('\n')) return;
const line = buffer.split('\n')[0];
try {
resolve(JSON.parse(line));
} catch {
resolve({ ok: false, error: 'Invalid response' });
}
client.end();
});
client.on('error', () => {
resolve({ ok: false, serverOffline: true });
});
client.setTimeout(2000, () => {
client.destroy();
resolve({ ok: false, error: 'Timeout' });
});
});
}
async function getPendingRows() {
const response = await sendIpcMessage('ip-guard-pending-list');
if (!response.ok) return { rows: [], error: response.serverOffline ? 'Server is not running' : response.error || 'Pending state unavailable' };
return { rows: Array.isArray(response.pending) ? response.pending : [], error: '' };
}
function printEntry(entry) {
const expires = entry.expiresAt ? formatDate(entry.expiresAt) : 'never';
console.log(
entry.ip.padEnd(40) +
entry.source.padEnd(9) +
formatDate(entry.createdAt).padEnd(22) +
expires.padEnd(22) +
(entry.reason || '-')
);
}
function listEntries() {
const includeExpired = hasFlag('--all');
const asJson = hasFlag('--json');
const rows = sessions.listIpBlacklistEntries({ includeExpired });
if (asJson) {
console.log(JSON.stringify(rows, null, 2));
return;
}
if (rows.length === 0) {
console.log(includeExpired ? 'No blacklist entries found.' : 'No active blacklist entries found.');
return;
}
console.log('IP'.padEnd(40) + 'Source'.padEnd(9) + 'Created'.padEnd(22) + 'Expires'.padEnd(22) + 'Reason');
console.log('-'.repeat(120));
for (const row of rows) printEntry(row);
console.log(`\nTotal: ${rows.length} entr${rows.length === 1 ? 'y' : 'ies'}`);
}
async function listPending() {
const asJson = hasFlag('--json');
const { rows, error } = await getPendingRows();
if (asJson) {
console.log(JSON.stringify({ pending: rows, error }, null, 2));
return;
}
if (error) {
console.log(`Pending state unavailable: ${error}`);
return;
}
if (rows.length === 0) {
console.log('No pending unauthenticated IPs.');
return;
}
console.log('IP'.padEnd(40) + 'Remaining'.padEnd(12) + 'Requests'.padEnd(10) + 'First Seen'.padEnd(22) + 'Path');
console.log('-'.repeat(120));
for (const row of rows) {
console.log(
row.ip.padEnd(40) +
String(row.remainingSeconds).padEnd(12) +
String(row.requestCount).padEnd(10) +
formatDate(row.firstSeen).padEnd(22) +
(row.path || '-')
);
}
}
async function checkIp(ipArg) {
const ip = validateIp(ipArg);
const entry = sessions.getIpBlacklistEntry(ip);
console.log(`IP: ${ip}`);
if (entry) {
console.log('Status: blacklisted');
console.log(`Source: ${entry.source}`);
console.log(`Created: ${formatDate(entry.createdAt)}`);
console.log(`Expires: ${entry.expiresAt ? formatDate(entry.expiresAt) : 'never'}`);
console.log(`Reason: ${entry.reason || '-'}`);
} else {
console.log('Status: not blacklisted');
}
const { rows, error } = await getPendingRows();
if (!error) {
const pending = rows.find((row) => row.ip === ip);
if (pending) {
console.log(`Pending: yes, ${pending.remainingSeconds}s remaining, ${pending.requestCount} request(s)`);
console.log(`Pending path: ${pending.path || '-'}`);
} else {
console.log('Pending: no');
}
}
}
async function addEntry(ipArg) {
const ip = validateIp(ipArg);
const reason = optionValue('--reason', 'Manual blacklist entry');
const expiresRaw = optionValue('--expires', 'never');
const duration = parseTimeString(expiresRaw);
if (duration === undefined) fail('Invalid --expires value; use Nsecs, Nmins, Nhours, Ndays, or never');
const now = Math.floor(Date.now() / 1000);
const expiresAt = duration === null ? null : now + duration;
sessions.addIpBlacklistEntry(ip, {
source: 'manual',
reason,
createdAt: now,
expiresAt,
});
await sendIpcMessage('ip-blacklist-updated', { ip });
console.log(`Blacklisted ${ip}${expiresAt ? ` until ${formatDate(expiresAt)}` : ' permanently'}.`);
}
async function removeEntry(ipArg) {
const ip = validateIp(ipArg);
const removed = sessions.removeIpBlacklistEntry(ip);
await sendIpcMessage('ip-blacklist-updated', { ip });
console.log(removed ? `Removed ${ip} from blacklist.` : `${ip} was not blacklisted.`);
}
async function clearEntries(scope) {
const value = String(scope || '').trim().toLowerCase();
if (!['expired', 'auto', 'manual', 'all'].includes(value)) {
fail('clear requires one of: expired, auto, manual, all');
}
let count = 0;
if (value === 'expired') {
count = sessions.clearIpBlacklistEntries({ expiredOnly: true });
} else if (value === 'all') {
count = sessions.clearIpBlacklistEntries();
await sendIpcMessage('ip-guard-clear-pending');
} else {
count = sessions.clearIpBlacklistEntries({ source: value });
await sendIpcMessage('ip-guard-clear-pending');
}
console.log(`Cleared ${count} blacklist entr${count === 1 ? 'y' : 'ies'}.`);
}
sessions.initDb();
(async () => {
try {
switch (command) {
case 'list':
listEntries();
break;
case 'pending':
await listPending();
break;
case 'check':
await checkIp(args[1]);
break;
case 'add':
await addEntry(args[1]);
break;
case 'remove':
await removeEntry(args[1]);
break;
case 'clear':
await clearEntries(args[1]);
break;
case 'help':
case '--help':
case '-h':
case undefined:
usage();
process.exit(command ? 1 : 0);
break;
default:
console.error(`Unknown command: ${command}`);
usage();
process.exit(1);
}
} finally {
sessions.closeDb();
}
})();