Forked from diegoparrilla/cloudflare-workers-apilityio.js
Created
April 19, 2023 12:20
-
-
Save fogmoon/8ea4adb331ac04d22b70976b3ddd4c47 to your computer and use it in GitHub Desktop.
Revisions
-
diegoparrilla revised this gist
Mar 19, 2018 . No changes.There are no files selected for viewing
-
diegoparrilla revised this gist
Mar 14, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ async function fetchAndCheckOrigin(req) { const ip = req.headers.get('cf-connecting-ip'); const es = req.headers.get('cf-ipcountry'); const apilityio = await fetch('http://api.apility.net/badip/' + ip + '?token=APILITYIO_API_KEY', { headers: {'Accept': 'application/json'}} ); const status = await apilityio.status; var blacklists = 'ERROR'; if (status == 404) { -
diegoparrilla created this gist
Feb 28, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ addEventListener('fetch', event => { event.respondWith(fetchAndCheckOrigin(event.request)) }) async function fetchAndCheckOrigin(req) { try { startTime = new Date(); const body = await req.body; const ip = req.headers.get('cf-connecting-ip'); const es = req.headers.get('cf-ipcountry'); const apilityio = await fetch('http://api.apility.net/badip/' + ip, { headers: {'Accept': 'application/json'}} ); const status = await apilityio.status; var blacklists = 'ERROR'; if (status == 404) { // The IP has not been found in any blacklist blacklists = ''; } if (status == 200) { // The IP has been found in any blacklist blacklists = (await apilityio.json())['response']; } if (status == 429) { // Quota Exceeded. Too many requests. blacklists = 'QUOTA_EXCEEDED'; } var endTime = new Date(); let newHdrs = new Headers(req.headers) newHdrs.set('Apilityio-Badip', blacklists) newHdrs.set('Apilityio-Elapsed-Time', endTime - startTime); if ((req.method == 'GET') || (req.method == 'HEAD')) { const init = { method: req.method, headers: newHdrs, }; return await fetch(req.url, init); } else { const init = { method: req.method, headers: newHdrs, body: body }; return await fetch(req.url, init); } } catch (err) { console.log(err); return new Response('Internal Error') } return await fetch(req) }