Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active December 1, 2023 15:52
Show Gist options
  • Select an option

  • Save dogweather/051f0743de9d941bb1a0b8a8a7f8f6ff to your computer and use it in GitHub Desktop.

Select an option

Save dogweather/051f0743de9d941bb1a0b8a8a7f8f6ff to your computer and use it in GitHub Desktop.

Revisions

  1. dogweather revised this gist Dec 1, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,11 @@ async function handleRequest(request) {
    // Is the visitor in the VIP list?
    let data = request.cf
    let visitor_asn = data.asn

    // Query the KV database
    let network_name = await VIP_API_ASN.get(visitor_asn)

    // Create the HTTP header value
    let is_vip = (network_name !== null)
    let msg = 'vip: ' + (is_vip ? 'yes' : 'no ') + '; ' + data.asn.toString().padStart(7) + ' - ' + data.asOrganization
    console.log(msg);
  2. dogweather created this gist Dec 1, 2023.
    26 changes: 26 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    async function handleRequest(request) {
    // Is the visitor in the VIP list?
    let data = request.cf
    let visitor_asn = data.asn
    let network_name = await VIP_API_ASN.get(visitor_asn)
    let is_vip = (network_name !== null)
    let msg = 'vip: ' + (is_vip ? 'yes' : 'no ') + '; ' + data.asn.toString().padStart(7) + ' - ' + data.asOrganization
    console.log(msg);

    // Clone the request so that it's no longer immutable
    const newRequest = new Request(request);

    // Add a custom header with the VIP message
    newRequest.headers.append("x-vip", msg)

    // Send the request on its way
    let response = await fetch(newRequest);

    // Return the response to the user
    return response
    }


    addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
    });