helm-analytics 1.0.0 → 1.1.0
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 +2 -2
- package/index.js +57 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,8 +17,8 @@ const HelmAnalytics = require('helm-analytics');
|
|
|
17
17
|
const app = express();
|
|
18
18
|
const helm = new HelmAnalytics({ siteId: 'YOUR_SITE_ID' });
|
|
19
19
|
|
|
20
|
-
// Use as middleware
|
|
21
|
-
app.use(helm.middleware());
|
|
20
|
+
// Use as middleware (with optional Shield filtering)
|
|
21
|
+
app.use(helm.middleware({ shield: true })); // Set to true to block requests from bad actors
|
|
22
22
|
|
|
23
23
|
app.get('/', (req, res) => {
|
|
24
24
|
res.send('Hello World');
|
package/index.js
CHANGED
|
@@ -3,16 +3,47 @@ const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch
|
|
|
3
3
|
class HelmAnalytics {
|
|
4
4
|
constructor(options = {}) {
|
|
5
5
|
this.siteId = options.siteId || process.env.HELM_SITE_ID;
|
|
6
|
-
this.apiUrl = options.apiUrl || 'https://api-sentinel.getmusterup.com
|
|
6
|
+
this.apiUrl = (options.apiUrl || 'https://api-sentinel.getmusterup.com').replace(/\/$/, '').replace(/\/track$/, '');
|
|
7
7
|
|
|
8
8
|
if (!this.siteId) {
|
|
9
9
|
console.warn('HelmAnalytics: No Site ID provided. Tracking will be disabled.');
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
// Check if request should be blocked
|
|
14
|
+
async checkShield(payload) {
|
|
15
|
+
if (!this.siteId) return { allowed: true };
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const checkPayload = {
|
|
19
|
+
siteId: payload.siteId,
|
|
20
|
+
ip: payload.clientIp,
|
|
21
|
+
userAgent: payload.userAgent,
|
|
22
|
+
url: payload.url
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const res = await fetch(`${this.apiUrl}/api/shield/decision`, {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
body: JSON.stringify(checkPayload),
|
|
28
|
+
headers: { 'Content-Type': 'application/json' }
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (res.ok) {
|
|
32
|
+
const decision = await res.json();
|
|
33
|
+
if (decision.action === 'block') {
|
|
34
|
+
return { allowed: false, reason: decision.reason };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} catch (err) {
|
|
38
|
+
// Fail open
|
|
39
|
+
console.error('Helm Shield Error:', err);
|
|
40
|
+
}
|
|
41
|
+
return { allowed: true };
|
|
42
|
+
}
|
|
43
|
+
|
|
13
44
|
// Generic track method
|
|
14
|
-
async track(req, eventType = 'pageview', metadata = {}) {
|
|
15
|
-
if (!this.siteId) return;
|
|
45
|
+
async track(req, eventType = 'pageview', metadata = {}, shield = false) {
|
|
46
|
+
if (!this.siteId) return true;
|
|
16
47
|
|
|
17
48
|
try {
|
|
18
49
|
const payload = {
|
|
@@ -27,25 +58,38 @@ class HelmAnalytics {
|
|
|
27
58
|
...metadata
|
|
28
59
|
};
|
|
29
60
|
|
|
30
|
-
//
|
|
31
|
-
|
|
61
|
+
// Shield Checking (Blocking)
|
|
62
|
+
if (shield) {
|
|
63
|
+
const { allowed, reason } = await this.checkShield(payload);
|
|
64
|
+
if (!allowed) {
|
|
65
|
+
console.warn(`Helm Shield Blocked: ${payload.clientIp} Reason: ${reason}`);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Fire and forget (don't await unless we want to ensure delivery, usually non-blocking is preferred for tracking)
|
|
71
|
+
fetch(`${this.apiUrl}/track`, {
|
|
32
72
|
method: 'POST',
|
|
33
73
|
body: JSON.stringify(payload),
|
|
34
74
|
headers: { 'Content-Type': 'application/json' }
|
|
35
|
-
}).catch(err => {
|
|
36
|
-
|
|
37
|
-
|
|
75
|
+
}).catch(err => {});
|
|
76
|
+
|
|
77
|
+
return true;
|
|
38
78
|
|
|
39
79
|
} catch (err) {
|
|
40
|
-
//
|
|
80
|
+
return true; // Fail open
|
|
41
81
|
}
|
|
42
82
|
}
|
|
43
83
|
|
|
44
84
|
// Express Middleware
|
|
45
|
-
middleware() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
85
|
+
middleware(options = {}) {
|
|
86
|
+
const shield = options.shield || false;
|
|
87
|
+
|
|
88
|
+
return async (req, res, next) => {
|
|
89
|
+
const allowed = await this.track(req, 'pageview', {}, shield);
|
|
90
|
+
if (!allowed) {
|
|
91
|
+
return res.status(403).send('Forbidden by Helm Aegis');
|
|
92
|
+
}
|
|
49
93
|
next();
|
|
50
94
|
};
|
|
51
95
|
}
|