GithubHelp home page GithubHelp logo

louisthomaspro / vercel-ip-blocking Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 126 KB

Block IPs with Vercel's Edge Middleware and Edge Config

Home Page: https://vercel-ip-blocker.vercel.app

TypeScript 86.40% CSS 5.65% JavaScript 7.95%
nextjs vercel ip-blocking shadcn-ui

vercel-ip-blocking's Introduction

Simple IP Blocker

DEMO: https://vercel-ip-blocker.vercel.app/

Using Edge Middleware, you can easily block IP addresses from accessing your site. This repo is using Next.js as an example.

Documentation

You can use Edge Middleware with any framework. To add Middleware to your app, you need to create a middleware.ts or middleware.js file in the root of your project.

Here is a simple example of how to block IP addresses:

// middleware.ts
import { ipAddress } from "@vercel/edge";
import { NextResponse } from "next/server";

export default async function middleware(request: Request) {
  const blockedIps = ["1.2.3.4", "5.6.7.8"];
  const ip = ipAddress(request);

  if (ip && blockedIps.includes(ip)) {
    return new NextResponse("Access denied", { status: 403 });
  }
}

The blockedIps variable can be configured using Edge Config to simplify the process of changing IP addresses.

  1. Create the Edge Config store

  2. Update the Edge Config store with the following JSON

{
  "blockedIps": [
    "1.2.3.4",
    "5.6.7.8"
  ]
}
  1. Update the middleware.ts file to use the get function from @vercel/edge-config
// middleware.ts
import { ipAddress } from "@vercel/edge";
import { NextResponse } from "next/server";
import { get } from "@vercel/edge-config";

export default async function middleware(request: Request) {
  const blockedIps = (await get("blockedIps")) as string[];
  const ip = ipAddress(request);

  if (ip && blockedIps.includes(ip)) {
    return new NextResponse("Access denied", { status: 403 });
  }
}

vercel-ip-blocking's People

Contributors

dferber90 avatar louisthomaspro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dferber90

vercel-ip-blocking's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.