Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example of IP blocking #213

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion edgecompute/examples/traffic-filtering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 28 additions & 0 deletions edgecompute/examples/traffic-filtering/block-based-on-ip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# block-based-on-ip

*Keyword(s):* constructed-response, edge logic, blocklist, allow list<br>
*[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
{}
````
## Property Variable
To enable EdgeWorker to get a user's IP address, the Set Variable behavior must be configured in Property Manager.
![Property Variable](propertyvariable.png)
![Set Variable](setvariable.png)

## 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"edgeworker-version": "0.1",
"description" : "Deny based on ip list"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const blockedIPs = [
'1.2.3.4',
'5.6.7.8'
];
14 changes: 14 additions & 0 deletions edgecompute/examples/traffic-filtering/block-based-on-ip/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { blockedIPs } from './ipList.js';

export async function onClientRequest(request) {
const clientIP = request.getVariable('PMUSER_IP');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using the new request.clientIp property instead of the PM variable. It will be easier to set up.


if (blockedIPs.includes(clientIP)) {
request.respondWith(
403,
{ 'Content-Type': ['application/json;charset=utf-8'] },
'{}',
'Denied Response'
);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.