traffic-guard 0.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/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/battery.d.ts +19 -0
- package/dist/crypto.d.ts +16 -0
- package/dist/gate.d.ts +15 -0
- package/dist/index.cjs +1119 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.mjs +1064 -0
- package/dist/index.mjs.map +7 -0
- package/dist/normalizer.d.ts +6 -0
- package/dist/policy.d.ts +4 -0
- package/dist/types.d.ts +96 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hemanth HM <hemanth@h3manth.com> (https://h3manth.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# traffic-guard
|
|
2
|
+
|
|
3
|
+
High-throughput traffic and attack defense gate for incoming web traffic with zero required dependencies, wire-order analysis, and TypeSafe System One acceleration.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install traffic-guard
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import trafficguard from 'traffic-guard';
|
|
13
|
+
|
|
14
|
+
const decision = await trafficguard(req);
|
|
15
|
+
|
|
16
|
+
if (decision.shouldBlock) {
|
|
17
|
+
return res.status(403).json({ error: 'Forbidden', reasons: decision.reasons });
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`trafficguard(req)` inspects headers, wire sequence order, payload entropy, and velocity signals in under 30 µs. Returns `action`, `shouldBlock`, `shouldTarpit`, `riskScore`, and calibrated reasons.
|
|
22
|
+
|
|
23
|
+
## Express middleware
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import express from 'express';
|
|
27
|
+
import trafficguard from 'traffic-guard';
|
|
28
|
+
|
|
29
|
+
const app = express();
|
|
30
|
+
|
|
31
|
+
app.use(trafficguard.middleware({
|
|
32
|
+
policy: 'balanced',
|
|
33
|
+
allowGoodBots: true,
|
|
34
|
+
honeypotPaths: ['/__tg_trap'], // instant ban for web spiders touching hidden links
|
|
35
|
+
whitelistedPaths: ['/healthz', /^\/public\//]
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
app.get('/api/data', (req, res) => res.json({ message: 'Hello Human!' }));
|
|
39
|
+
app.listen(3000);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Advanced defense patterns implemented
|
|
43
|
+
|
|
44
|
+
1. **Header Order Sequence Analysis**: Real Chromium browsers send `Host` before `User-Agent` and Client Hints (`sec-ch-ua`) in a strict order. Bots forging user-agents in Python/cURL exhibit sequence disorder.
|
|
45
|
+
2. **Stateless HMAC Tokens & Velocity Tracking**: Signs a signed `__trafficguard` cookie via HMAC-SHA256 tracking request velocity in 10-second sliding windows with zero database dependency.
|
|
46
|
+
3. **Silent Proof-of-Work (PoW) Micro-Challenge**: Serves an inline 1.2 KB HashCash puzzle. Legitimate browsers solve it in 15–30 ms; automated CLI scrapers cannot execute JS.
|
|
47
|
+
4. **Tarpitting (Slowdown Defense)**: Configurable artificial latency delay for scrapers to exhaust their concurrency pools.
|
|
48
|
+
5. **Canary Honeypot Traps**: Immediate blocking of crawlers that scrape invisible honeypot URLs.
|
|
49
|
+
|
|
50
|
+
## Empirical benchmark
|
|
51
|
+
|
|
52
|
+
Evaluated on 25 canonical golden test cases (`bench/dataset.json`):
|
|
53
|
+
|
|
54
|
+
| Metric | In-Tree Zero-Dep Engine (JS) | TypeSafe Cloud Tier (Jev-latest) |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| Category Classification | 100.0% | 100.0% |
|
|
57
|
+
| Action Accuracy | 100.0% | 100.0% |
|
|
58
|
+
| Attack Block Rate (Recall) | 100.0% | 100.0% |
|
|
59
|
+
| Human False Positive Rate | 0.0% | 0.0% |
|
|
60
|
+
| Good Bot Passthrough Rate | 100.0% | 100.0% |
|
|
61
|
+
| Mean Latency | 30 µs (0.03 ms) | ~250 ms |
|
|
62
|
+
| p95 Latency | 56 µs | ~320 ms |
|
|
63
|
+
| External Dependencies | 0 required | Optional `@typesafe-ai/sdk` |
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT © [Hemanth.HM](https://h3manth.com)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AssessmentResult, NormalizedRequest, RequestContext } from './types.js';
|
|
2
|
+
export declare function loadTypeSafePrimitives(): Promise<{
|
|
3
|
+
choice: any;
|
|
4
|
+
noul: any;
|
|
5
|
+
score: any;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function createBotGateBattery(): {
|
|
8
|
+
is_bot: any;
|
|
9
|
+
is_attack: any;
|
|
10
|
+
is_spoofed: any;
|
|
11
|
+
traffic_type: any;
|
|
12
|
+
risk_level: any;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Computes Shannon entropy of a string using Uint8Array frequency table.
|
|
16
|
+
* Useful for detecting obfuscated attack strings and random scanner paths.
|
|
17
|
+
*/
|
|
18
|
+
export declare function calculateShannonEntropy(str: string): number;
|
|
19
|
+
export declare function heuristicAssessment(request: NormalizedRequest, context: RequestContext): AssessmentResult;
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface BotCookiePayload {
|
|
2
|
+
h: string;
|
|
3
|
+
c: number;
|
|
4
|
+
ws: number;
|
|
5
|
+
v?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function createClientHash(ip?: string, userAgent?: string): string;
|
|
8
|
+
export declare function signBotToken(payload: BotCookiePayload, secret?: string): string;
|
|
9
|
+
export declare function verifyBotToken(token: string, secret?: string): BotCookiePayload | null;
|
|
10
|
+
export declare function parseCookies(cookieHeader?: string): Record<string, string>;
|
|
11
|
+
export declare function createPoWChallenge(secret?: string, difficulty?: number): {
|
|
12
|
+
seed: string;
|
|
13
|
+
difficulty: number;
|
|
14
|
+
};
|
|
15
|
+
export declare function verifyPoW(seed: string, nonce: string, minDifficulty?: number, secret?: string): boolean;
|
|
16
|
+
export declare function generateChallengeHtml(seed: string, difficulty: number, returnUrl?: string): string;
|
package/dist/gate.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BotGateDecision, BotGateOptions, RequestInput } from './types.js';
|
|
2
|
+
export declare class BotGate {
|
|
3
|
+
private client;
|
|
4
|
+
private clientInitAttempted;
|
|
5
|
+
private options;
|
|
6
|
+
private battery;
|
|
7
|
+
constructor(options?: BotGateOptions);
|
|
8
|
+
private getClient;
|
|
9
|
+
inspect(input: RequestInput, runtimeOptions?: BotGateOptions): Promise<BotGateDecision>;
|
|
10
|
+
middleware(middlewareOptions?: BotGateOptions): (req: any, res: any, next: (err?: any) => void) => Promise<any>;
|
|
11
|
+
private createAllowedDecision;
|
|
12
|
+
private createBlockedDecision;
|
|
13
|
+
}
|
|
14
|
+
export type TrafficGuard = BotGate;
|
|
15
|
+
export declare const TrafficGuard: typeof BotGate;
|