@chdlabs/my-shield 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +212 -226
- package/package.json +5 -1
- package/src/admin/otpGate.js +370 -0
- package/src/client/collector.js +303 -53
- package/src/client/index.js +2 -1
- package/src/client/shieldClient.js +121 -0
- package/src/core/ipResolver.js +225 -0
- package/src/index.d.ts +152 -10
- package/src/index.js +113 -4
- package/src/middleware/behavioralGuard.js +172 -24
- package/src/middleware/honeypot.js +6 -4
- package/src/middleware/notFoundCollector.js +6 -4
- package/src/middleware/shieldGuard.js +3 -0
- package/src/storage/BaseAdapter.js +58 -0
- package/src/storage/CallbackStorageAdapter.js +69 -0
- package/src/storage/FileStorageAdapter.js +174 -0
- package/src/storage/MemoryAdapter.js +56 -0
- package/src/storage/index.js +53 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-Memory Storage Adapter
|
|
3
|
+
*
|
|
4
|
+
* Default ephemeral adapter for non-clustered, single-process deployments
|
|
5
|
+
* with zero filesystem or network overhead.
|
|
6
|
+
*
|
|
7
|
+
* @author ChdLabs
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
import { BaseStorageAdapter } from './BaseAdapter.js';
|
|
11
|
+
|
|
12
|
+
export class MemoryAdapter extends BaseStorageAdapter {
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
this.records = new Map();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async init(shield) {
|
|
19
|
+
await super.init(shield);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async loadBlockedIPs() {
|
|
23
|
+
const now = Date.now();
|
|
24
|
+
const active = [];
|
|
25
|
+
for (const [ip, item] of this.records.entries()) {
|
|
26
|
+
if (item.expiresAt && item.expiresAt <= now) {
|
|
27
|
+
this.records.delete(ip);
|
|
28
|
+
} else {
|
|
29
|
+
active.push(item);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return active;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async saveBlockedIP(ip, reason, metadata = {}, expiresAt = null) {
|
|
36
|
+
if (!ip) return;
|
|
37
|
+
this.records.set(ip, {
|
|
38
|
+
ip,
|
|
39
|
+
reason,
|
|
40
|
+
metadata,
|
|
41
|
+
expiresAt,
|
|
42
|
+
savedAt: Date.now()
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async removeBlockedIP(ip) {
|
|
47
|
+
if (!ip) return;
|
|
48
|
+
this.records.delete(ip);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async close() {
|
|
52
|
+
this.records.clear();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default MemoryAdapter;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage Subsystem Exports & Factory
|
|
3
|
+
*
|
|
4
|
+
* @author ChdLabs
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
import { BaseStorageAdapter } from './BaseAdapter.js';
|
|
8
|
+
import { MemoryAdapter } from './MemoryAdapter.js';
|
|
9
|
+
import { FileStorageAdapter } from './FileStorageAdapter.js';
|
|
10
|
+
import { CallbackStorageAdapter } from './CallbackStorageAdapter.js';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
BaseStorageAdapter,
|
|
14
|
+
MemoryAdapter,
|
|
15
|
+
FileStorageAdapter,
|
|
16
|
+
CallbackStorageAdapter
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Instantiate appropriate storage adapter based on options
|
|
21
|
+
* @param {string|Object|BaseStorageAdapter} [storageOption]
|
|
22
|
+
* @param {Object} [logger]
|
|
23
|
+
* @returns {BaseStorageAdapter}
|
|
24
|
+
*/
|
|
25
|
+
export function createStorageAdapter(storageOption, logger = console) {
|
|
26
|
+
if (!storageOption || storageOption === 'memory') {
|
|
27
|
+
return new MemoryAdapter();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (storageOption instanceof BaseStorageAdapter) {
|
|
31
|
+
return storageOption;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (typeof storageOption === 'object') {
|
|
35
|
+
// Callback adapter: has onLoad, onBlock, or onUnblock
|
|
36
|
+
if (storageOption.onLoad || storageOption.onBlock || storageOption.onUnblock) {
|
|
37
|
+
return new CallbackStorageAdapter({ ...storageOption, logger });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// File adapter: has type === 'file' or directory or filename
|
|
41
|
+
if (storageOption.type === 'file' || storageOption.directory || storageOption.filename) {
|
|
42
|
+
return new FileStorageAdapter({ ...storageOption, logger });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (storageOption === 'file') {
|
|
47
|
+
return new FileStorageAdapter({ logger });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return new MemoryAdapter();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default createStorageAdapter;
|