Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fogmoon/8ea4adb331ac04d22b70976b3ddd4c47 to your computer and use it in GitHub Desktop.

Select an option

Save fogmoon/8ea4adb331ac04d22b70976b3ddd4c47 to your computer and use it in GitHub Desktop.

Revisions

  1. @diegoparrilla diegoparrilla revised this gist Mar 19, 2018. No changes.
  2. @diegoparrilla diegoparrilla revised this gist Mar 14, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare-workers-apilityio.js
    Original 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, { headers: {'Accept': 'application/json'}} );
    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) {
  3. @diegoparrilla diegoparrilla created this gist Feb 28, 2018.
    53 changes: 53 additions & 0 deletions cloudflare-workers-apilityio.js
    Original 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)
    }