forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeRequestInfoPlugin.ts
More file actions
80 lines (68 loc) · 2.19 KB
/
NodeRequestInfoPlugin.ts
File metadata and controls
80 lines (68 loc) · 2.19 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
import {
EventPluginContext,
getCookies,
IEventPlugin,
isMatch,
KnownEventDataKeys,
RequestInfo,
stringify,
} from "@exceptionless/core";
export class NodeRequestInfoPlugin implements IEventPlugin {
public priority: number = 70;
public name: string = "NodeRequestInfoPlugin";
public run(context: EventPluginContext): Promise<void> {
if (!context.event.data[KnownEventDataKeys.RequestInfo]) {
const requestInfo: RequestInfo = this.getRequestInfo(context);
if (requestInfo) {
if (isMatch(requestInfo.user_agent, context.client.config.userAgentBotPatterns)) {
context.log.info("Cancelling event as the request user agent matches a known bot pattern");
context.cancelled = true;
} else {
context.event.data[KnownEventDataKeys.RequestInfo] = requestInfo;
}
}
}
return Promise.resolve();
}
private getRequestInfo(context: EventPluginContext): RequestInfo {
// TODO: Move this into a known keys.
const REQUEST_KEY: string = "@request";
if (!context.contextData[REQUEST_KEY]) {
return null;
}
const config = context.client.config;
const exclusions = config.dataExclusions;
const request: any = context.contextData[REQUEST_KEY];
const requestInfo: RequestInfo = {
user_agent: request.headers["user-agent"],
http_method: request.method,
is_secure: request.secure,
host: request.hostname,
path: request.path,
referrer: request.headers.referer
};
const host = request.headers.host;
const port: number = host &&
parseInt(host.slice(host.indexOf(":") + 1), 10);
if (port > 0) {
requestInfo.port = port;
}
if (config.includeIpAddress) {
requestInfo.client_ip_address = request.ip;
}
if (config.includeCookies) {
requestInfo.cookies = getCookies(request.headers.cookie, exclusions);
}
if (config.includeQueryString) {
requestInfo.query_string = JSON.parse(
stringify(request.params || {}, exclusions),
);
}
if (config.includePostData) {
requestInfo.post_data = JSON.parse(
stringify(request.body || {}, exclusions),
);
}
return requestInfo;
}
}