diff --git a/edgecompute/examples/traffic-filtering/README.md b/edgecompute/examples/traffic-filtering/README.md index 0f55b6f..21c29c4 100644 --- a/edgecompute/examples/traffic-filtering/README.md +++ b/edgecompute/examples/traffic-filtering/README.md @@ -5,8 +5,9 @@ The examples in this section describe how to use EdgeWorkers to control traffic * **/conference-details**: This code implements an API call to validate a conference code * **/trace-headers**: Demonstrates how EdgeWorkers can be used to inspect forward request headers during the lifetime of a request * **/traffic-allow-list**: Implements an allow list depending on the geographic locale of the end user. +* **/block-based-on-ip**: Implements a block list depending on ip addresses. ## Related Resources - [EdgeWorkers CLI](https://developer.akamai.com/cli/packages/edgeworkers.html) - [EdgeWorkers documentation](https://techdocs.akamai.com/edgeworkers/docs) -- [EdgeKV documentation](https://techdocs.akamai.com/edgekv/docs) +- [EdgeKV documentation](https://techdocs.akamai.com/edgekv/docs) \ No newline at end of file diff --git a/edgecompute/examples/traffic-filtering/block-based-on-ip/README.md b/edgecompute/examples/traffic-filtering/block-based-on-ip/README.md new file mode 100644 index 0000000..2dd2e1e --- /dev/null +++ b/edgecompute/examples/traffic-filtering/block-based-on-ip/README.md @@ -0,0 +1,23 @@ +# block-based-on-ip + +*Keyword(s):* constructed-response, edge logic, blocklist, allow list
+*[Since](https://techdocs.akamai.com/edgeworkers/docs/about-the-javascript-api):* 1.0 + +This example implements a block list depending on ip addresses. If a user's IP address is on the list, a 403 deny will occur. + +## Usage Examples +```` +// Incoming Request +GET / HTTP/1.1 +Host: www.example.com +(User's IP in a list) + +// Response to Browser +HTTP/1.1 403 Forbidden +{} +```` +## Similar Uses +Allowlist or blocklist capabilities can be setup based on other end user request context information such as connecting IP, Geo, Device Characteristics, Accept-Language, User-Agent, etc. + +## Resources +See the repo [README](https://github.com/akamai/edgeworkers-examples#Resources) for additional guidance. diff --git a/edgecompute/examples/traffic-filtering/block-based-on-ip/bundle.json b/edgecompute/examples/traffic-filtering/block-based-on-ip/bundle.json new file mode 100644 index 0000000..9cf624d --- /dev/null +++ b/edgecompute/examples/traffic-filtering/block-based-on-ip/bundle.json @@ -0,0 +1,4 @@ +{ + "edgeworker-version": "0.1", + "description" : "Deny based on ip list" +} \ No newline at end of file diff --git a/edgecompute/examples/traffic-filtering/block-based-on-ip/ipList.js b/edgecompute/examples/traffic-filtering/block-based-on-ip/ipList.js new file mode 100644 index 0000000..47c1d5c --- /dev/null +++ b/edgecompute/examples/traffic-filtering/block-based-on-ip/ipList.js @@ -0,0 +1,4 @@ +export const blockedIPs = [ + '1.2.3.4', + '5.6.7.8' +]; \ No newline at end of file diff --git a/edgecompute/examples/traffic-filtering/block-based-on-ip/main.js b/edgecompute/examples/traffic-filtering/block-based-on-ip/main.js new file mode 100644 index 0000000..2d5cd53 --- /dev/null +++ b/edgecompute/examples/traffic-filtering/block-based-on-ip/main.js @@ -0,0 +1,17 @@ +/* +(c) Copyright 2024 Akamai Technologies, Inc. Licensed under Apache 2 license. +Purpose: Block by IP address +*/ + +import { blockedIPs } from './ipList.js'; + +export async function onClientRequest(request) { + if (blockedIPs.includes(request.clientIp)) { + request.respondWith( + 403, + { 'Content-Type': ['application/json;charset=utf-8'] }, + '{}', + 'Denied Response' + ); + } +}