@webdecoy/express 0.2.1 → 0.3.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Web Decoy middleware for Express.js applications - Advanced bot detection with TLS fingerprinting.
4
4
 
5
- [![npm version](https://badge.fury.io/js/%40webdecoy%2Fexpress.svg)](https://www.npmjs.com/package/@webdecoy/express)
5
+ [![npm version](https://img.shields.io/npm/v/@webdecoy/express.svg)](https://www.npmjs.com/package/@webdecoy/express)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
 
8
8
  ## Installation
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { Request, Response, NextFunction } from 'express';
2
- import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
3
- export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
1
+ import { Request, Response, NextFunction, RequestHandler } from 'express';
2
+ import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Express Middleware
@@ -77,4 +77,24 @@ declare global {
77
77
  }
78
78
  }
79
79
 
80
- export { type WebDecoyMiddlewareOptions, webdecoy };
80
+ /**
81
+ * Express captcha endpoints.
82
+ *
83
+ * Mounts the WebDecoy captcha routes (challenge / verify / score / token verify)
84
+ * under a base path. Requires JSON body parsing (`express.json()`) ahead of it.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * import express from 'express';
89
+ * import { webdecoyCaptcha } from '@webdecoy/express';
90
+ *
91
+ * const app = express();
92
+ * app.use(express.json());
93
+ * app.use(webdecoyCaptcha({ secret: process.env.WEBDECOY_SECRET }));
94
+ * // → GET /__webdecoy/challenge, POST /__webdecoy/verify, /score, /token/verify
95
+ * ```
96
+ */
97
+
98
+ declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
99
+
100
+ export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Request, Response, NextFunction } from 'express';
2
- import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
3
- export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
1
+ import { Request, Response, NextFunction, RequestHandler } from 'express';
2
+ import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Express Middleware
@@ -77,4 +77,24 @@ declare global {
77
77
  }
78
78
  }
79
79
 
80
- export { type WebDecoyMiddlewareOptions, webdecoy };
80
+ /**
81
+ * Express captcha endpoints.
82
+ *
83
+ * Mounts the WebDecoy captcha routes (challenge / verify / score / token verify)
84
+ * under a base path. Requires JSON body parsing (`express.json()`) ahead of it.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * import express from 'express';
89
+ * import { webdecoyCaptcha } from '@webdecoy/express';
90
+ *
91
+ * const app = express();
92
+ * app.use(express.json());
93
+ * app.use(webdecoyCaptcha({ secret: process.env.WEBDECOY_SECRET }));
94
+ * // → GET /__webdecoy/challenge, POST /__webdecoy/verify, /score, /token/verify
95
+ * ```
96
+ */
97
+
98
+ declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
99
+
100
+ export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
package/dist/index.js CHANGED
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- webdecoy: () => webdecoy
23
+ webdecoy: () => webdecoy,
24
+ webdecoyCaptcha: () => webdecoyCaptcha
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
@@ -61,7 +62,7 @@ function shouldSkipPath(path, skipPaths) {
61
62
  }
62
63
  function webdecoy(config) {
63
64
  const sdk = new import_node.WebDecoy(config);
64
- const getIP = config.getIP || defaultGetIP;
65
+ const getIP2 = config.getIP || defaultGetIP;
65
66
  const onBlocked = config.onBlocked || defaultOnBlocked;
66
67
  const onError = config.onError || defaultOnError;
67
68
  const skipPaths = config.skipPaths;
@@ -73,7 +74,7 @@ function webdecoy(config) {
73
74
  const metadata = {
74
75
  method: req.method,
75
76
  path: req.path,
76
- ip: getIP(req),
77
+ ip: getIP2(req),
77
78
  user_agent: req.headers["user-agent"],
78
79
  headers: req.headers,
79
80
  timestamp: Date.now()
@@ -116,7 +117,48 @@ function webdecoy(config) {
116
117
  }
117
118
  };
118
119
  }
120
+
121
+ // src/captcha.ts
122
+ var import_node2 = require("@webdecoy/node");
123
+ function getIP(req) {
124
+ const forwardedFor = req.headers["x-forwarded-for"];
125
+ if (forwardedFor) {
126
+ const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
127
+ return ips.split(",")[0].trim();
128
+ }
129
+ const realIP = req.headers["x-real-ip"];
130
+ if (realIP) return Array.isArray(realIP) ? realIP[0] : realIP;
131
+ return req.ip || req.socket.remoteAddress || "127.0.0.1";
132
+ }
133
+ function normalizeQuery(query) {
134
+ const out = {};
135
+ for (const [k, v] of Object.entries(query)) {
136
+ out[k] = Array.isArray(v) ? String(v[0]) : typeof v === "string" ? v : void 0;
137
+ }
138
+ return out;
139
+ }
140
+ function webdecoyCaptcha(options) {
141
+ const endpoints = (0, import_node2.createCaptchaEndpoints)(options);
142
+ return async (req, res, next) => {
143
+ const result = await endpoints.handle({
144
+ method: req.method,
145
+ pathname: req.path,
146
+ query: normalizeQuery(req.query),
147
+ headers: req.headers,
148
+ body: req.body,
149
+ ip: getIP(req)
150
+ });
151
+ if (!result) {
152
+ next();
153
+ return;
154
+ }
155
+ res.status(result.status);
156
+ for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
157
+ res.json(result.body);
158
+ };
159
+ }
119
160
  // Annotate the CommonJS export names for ESM import in node:
120
161
  0 && (module.exports = {
121
- webdecoy
162
+ webdecoy,
163
+ webdecoyCaptcha
122
164
  });
package/dist/index.mjs CHANGED
@@ -35,7 +35,7 @@ function shouldSkipPath(path, skipPaths) {
35
35
  }
36
36
  function webdecoy(config) {
37
37
  const sdk = new WebDecoy(config);
38
- const getIP = config.getIP || defaultGetIP;
38
+ const getIP2 = config.getIP || defaultGetIP;
39
39
  const onBlocked = config.onBlocked || defaultOnBlocked;
40
40
  const onError = config.onError || defaultOnError;
41
41
  const skipPaths = config.skipPaths;
@@ -47,7 +47,7 @@ function webdecoy(config) {
47
47
  const metadata = {
48
48
  method: req.method,
49
49
  path: req.path,
50
- ip: getIP(req),
50
+ ip: getIP2(req),
51
51
  user_agent: req.headers["user-agent"],
52
52
  headers: req.headers,
53
53
  timestamp: Date.now()
@@ -90,6 +90,47 @@ function webdecoy(config) {
90
90
  }
91
91
  };
92
92
  }
93
+
94
+ // src/captcha.ts
95
+ import { createCaptchaEndpoints } from "@webdecoy/node";
96
+ function getIP(req) {
97
+ const forwardedFor = req.headers["x-forwarded-for"];
98
+ if (forwardedFor) {
99
+ const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
100
+ return ips.split(",")[0].trim();
101
+ }
102
+ const realIP = req.headers["x-real-ip"];
103
+ if (realIP) return Array.isArray(realIP) ? realIP[0] : realIP;
104
+ return req.ip || req.socket.remoteAddress || "127.0.0.1";
105
+ }
106
+ function normalizeQuery(query) {
107
+ const out = {};
108
+ for (const [k, v] of Object.entries(query)) {
109
+ out[k] = Array.isArray(v) ? String(v[0]) : typeof v === "string" ? v : void 0;
110
+ }
111
+ return out;
112
+ }
113
+ function webdecoyCaptcha(options) {
114
+ const endpoints = createCaptchaEndpoints(options);
115
+ return async (req, res, next) => {
116
+ const result = await endpoints.handle({
117
+ method: req.method,
118
+ pathname: req.path,
119
+ query: normalizeQuery(req.query),
120
+ headers: req.headers,
121
+ body: req.body,
122
+ ip: getIP(req)
123
+ });
124
+ if (!result) {
125
+ next();
126
+ return;
127
+ }
128
+ res.status(result.status);
129
+ for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
130
+ res.json(result.body);
131
+ };
132
+ }
93
133
  export {
94
- webdecoy
134
+ webdecoy,
135
+ webdecoyCaptcha
95
136
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Web Decoy middleware for Express.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "url": "https://github.com/WebDecoy/node-sdk/issues"
41
41
  },
42
42
  "dependencies": {
43
- "@webdecoy/node": "^0.2.1"
43
+ "@webdecoy/node": "^0.3.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"