Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegoparrilla/57f61315dd9c2420d3c954ce4efb93b7 to your computer and use it in GitHub Desktop.
Save diegoparrilla/57f61315dd9c2420d3c954ce4efb93b7 to your computer and use it in GitHub Desktop.
Using AWS Lambad@edge and Cloudfront with https://Apility.io API to add to the request headers information of the blacklists of abusers that contains the IP address of the client.
'use strict';
const http = require('http');
exports.handler = (event, context, callback) => {
var startTime = new Date();
const request = event.Records[0].cf.request;
let clientIp = request.clientIp;
var options = {
host: "api.apility.net",
path: "/badip/" + clientIp,
headers: {
"Accept": "application/json",
"X-AUTH-TOKEN": "APILITYIO_API_KEY"
}
};
http.get(options, (resp) => {
let code = resp.statusCode;
let blacklists = "";
if (code==200) {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
blacklists = JSON.parse(data)['response'].join(',');
request.headers['apilityio-badip'] = [{ 'key': 'Apilityio-Badip', 'value': blacklists }];
var endTime = new Date();
request.headers['apilityio-elapsed-time'] = [{ 'key': 'Apilityio-Elapsed-Time', 'value': (endTime - startTime).toString() }];
callback(null, request);
});
}
else {
if (code==429) {
blacklists = "Quota exceeded.";
}
request.headers['apilityio-badip'] = [{ 'key': 'Apilityio-Badip', 'value': blacklists }];
var endTime = new Date();
request.headers['apilityio-elapsed-time'] = [{ 'key': 'Apilityio-Elapsed-Time', 'value': (endTime - startTime).toString() }];
callback(null, request);
}
}).on("error", (err) => {
console.log("Error: " + err.message);
callback(null, request);
});
}
;
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
let clientIp = request.clientIp;
request.headers['client-ip'] = [{ 'key': 'Client-Ip', 'value': clientIp }];
callback(null, request);
}
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment