Created
August 23, 2018 13:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
}); | |
} | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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