A Next.js application demonstrating IP-based access control using Contentstack Edge Functions. This project shows how to implement IP restriction to block access to your application based on specific client IP addresses.
- IP Restriction Protection: Block access from specific IP addresses
- Edge Function Integration: Uses Contentstack Edge Functions for real-time IP validation
- Modern UI: Beautiful, responsive interface built with Next.js and Tailwind CSS
- Real-time Access Control: IP validation happens at the edge for optimal performance
- Node.js 18+
- npm or yarn
- Contentstack account (for Edge Functions deployment)
-
Clone the repository
git clone <repository-url> cd Edge-ip-blacklist-example-main
-
Install dependencies
npm install
-
Run the development server
npm run dev
-
Open your browser Navigate to http://localhost:3000
Edit the functions/[proxy].edge.js file to configure your blocked IP addresses:
const blockedIPs = [
"127.0.0.1", // Localhost
"::1", // IPv6 localhost
"192.168.1.100", // Office IP to block
"10.0.0.50", // VPN IP to block
"172.16.0.10" // Internal network IP to block
];To add IP addresses to the restriction list:
- Identify the IP addresses you want to block
- Add them to the
blockedIPsarray infunctions/[proxy].edge.js - Deploy the Edge Function
Edge-ip-blacklist-example-main/
├── app/ # Next.js app directory
│ ├── page.tsx # Main application page
│ ├── layout.tsx # Root layout component
│ └── globals.css # Global styles
├── functions/
│ └── [proxy].edge.js # Edge Function for IP validation
├── public/ # Static assets
├── package.json # Dependencies and scripts
└── next.config.ts # Next.js configuration
- Request Interception: All requests are intercepted by the Edge Function
- IP Extraction: The client's IP address is extracted from request headers
- Validation: The IP is checked against the blocked IP list
- Access Control:
- ✅ Allowed: Request proceeds to the application (IP not in blocked list)
- ❌ Denied: Returns 403 Forbidden response (IP is in blocked list)
The Edge Function handles various IP scenarios:
- Direct client connections
- Requests behind proxies/load balancers
- IPv4 and IPv6 addresses
- Multiple forwarded IPs
const clientIP = request.headers.get("x-forwarded-for") || "";
const clientIPList = clientIP.split(",").map(ip => ip.trim());npm run devnpm run build
npm start- Deploy the Edge Function to Contentstack
- Configure the function to intercept your desired routes
- Test with different IP addresses
- Non-blocked IP: Should see the success page
- Blocked IP: Should see "Forbidden" message
- Local Development: May be blocked if
127.0.0.1is in the blocked list
You can test the IP validation by:
- Using different network connections
- Using VPN services
- Modifying the
blockedIPsarray
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the Contentstack documentation
- Review the Edge Function logs in your Contentstack dashboard
- Ensure your IP addresses are correctly added to the restriction list
Note: This is an example implementation. In production, consider additional security measures such as rate limiting, logging, and monitoring.