-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
207 lines (193 loc) · 6.55 KB
/
handler.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { APIGatewayEvent, APIGatewayProxyHandler } from "aws-lambda";
import fetch from "node-fetch";
import "source-map-support/register";
import geoip from 'geoip-lite';
/**
* This is the doc comment for handler.ts
* @namespace handler.ts
* GitHub Repository: {@link https://github.com/johnweland/ipblock-microservice}
*/
/**
* FileType Interface for custome typing
* @interface
*/
interface FireHolFile {
path: string;
mode: string;
type: string;
sha: string;
size: number;
url: string;
}
/**
* Primary function for comparing input/request IP to a list of blocked IPSets from Firehol
* @param {APIGatewayEvent} event
* @param {Context} _context
*
* @returns {json} JSON object with success or error data
*/
export const ipcheck: APIGatewayProxyHandler = async (event, _context) => {
try {
let { ip, origin } = await evaluateRequest(event);
let valid = await validateIp(ip);
if (!valid) {
throw new Error(`invalid IP address ${ip}`);
}
let lists: Array<FireHolFile> = await getFireholLists();
let flagged: boolean = false;
let count: number = 0;
let foundIn: string|null = null;
// NOTE: add let n for length caching, should improve performance some
for (let i:number = 0, n:number = lists.length; i < n; ++i) {
let lines: string[] = await readIPset(lists[i]);
count = count + lines.length;
if (lines.includes(ip)) {
flagged = true;
foundIn = `https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/${lists[i].path}`;
break;
}
}
let telemetry = await getTelemetry(ip);
let message = `The IP address ${ip}`;
flagged
? message += ` was found amoung an ipset, please see 'request_results'.`
: message += ` is safe.`;
return {
statusCode: 200,
body: JSON.stringify(
{
message: message,
request_results: {
ip,
telemetry: JSON.parse(telemetry),
addresses_searched: count,
ipsets_count: lists.length,
found: flagged,
ipset: foundIn
},
request_origin: origin
},
null,
2,
),
};
} catch (err) {
return {
statusCode: 400,
body: JSON.stringify(
{
error: err.message,
},
null,
2,
),
};
}
};
/**
* Evaluate incoming request for querystring AND|OR origin
* @param {object} request Inbound request
*
* @throws {InValidArgumentException} Invalid IP Address
* @return {object} IP and origin
*/
export const evaluateRequest = (request:APIGatewayEvent) => {
let ip: string = null;
let requestOrigin: string = null;
if (
typeof request.queryStringParameters !== "undefined" &&
request.queryStringParameters !== null
) {
if (
typeof request.queryStringParameters.ip !== "undefined" &&
request.queryStringParameters.ip.length &&
request.queryStringParameters.ip !== null
) {
ip = request.queryStringParameters.ip;
} else {
throw new Error(
"The only query parameter excepted is '?ip=<SOMEVALUE>'.",
);
}
}
if (
typeof request.requestContext !== "undefined" &&
typeof request.requestContext.identity !== "undefined" &&
typeof request.requestContext.identity.sourceIp !== "undefined"
) {
requestOrigin = request.requestContext.identity.sourceIp;
}
if (requestOrigin !== null && ip == null) {
ip = requestOrigin;
}
return {
ip,
origin: {
ip: requestOrigin,
country: request.headers['CloudFront-Viewer-Country'],
userAgent: request.headers['User-Agent']
},
};
};
/**
* Validate IP Address as either ipV4 or ipV6
* @param {string} ip v4 or v6 IP address
*
* @returns {boolean} Valid IP address?
*/
export const validateIp = (ip) => {
let ipV4: boolean = new RegExp(
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]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\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/).test(
ip,
);
if (!ipV4 && !ipV6) {
return false;
}
return true;
};
/**
* Return a array of Files ending in .ipset from the git repository master branch
*
* @returns {[File]} An array of file:File
*/
export const getFireholLists = async () => {
const response = await fetch(
`https://api.github.com/repos/firehol/blocklist-ipsets/git/trees/master?recursive=1`,
);
const json = await response.json();
const files: [] = await json.tree.filter((file) => {
if (file.path.endsWith(".ipset")) {
return file.path;
}
});
return files;
};
/**
* Strips out comment lines and converts the IP address lines to an array of IP addresses
* @param {FireHolFile} file File object from which to grab the path
*
* @returns {array} A sanitized list of IP Addresses
*/
export const readIPset = async (file: FireHolFile) => {
const response = await fetch(
`https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/${file.path}`,
);
const text = await response.text();
const sanitized: [] = await text.split("\n").filter((line) => {
return line.indexOf("#") !== 0;
});
return sanitized;
};
/**
*
* @param ip
*/
export const getTelemetry = async (ip:string) => {
let telemetry = await geoip.lookup(ip);
if (telemetry) {
return JSON.stringify(telemetry);
}
return JSON.stringify("no telemetry data found");
}