Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
67 lines
2.3 KiB
Plaintext
67 lines
2.3 KiB
Plaintext
import { MiddlewareHandler } from "../../types.js";
|
|
import { Context } from "../../context.js";
|
|
import { AddressType, GetConnInfo } from "../../helper/conninfo/types.js";
|
|
//#region src/middleware/ip-restriction/index.d.ts
|
|
/**
|
|
* Function to get IP Address
|
|
*/
|
|
type GetIPAddr = GetConnInfo | ((c: Context) => string);
|
|
export type IPRestrictionRule = string | ((addr: {
|
|
addr: string;
|
|
type: AddressType;
|
|
}) => boolean);
|
|
/**
|
|
* Rules for IP Restriction Middleware
|
|
*/
|
|
export interface IPRestrictionRules {
|
|
denyList?: IPRestrictionRule[];
|
|
allowList?: IPRestrictionRule[];
|
|
}
|
|
/**
|
|
* IP Restriction Middleware for Hono.
|
|
*
|
|
* @see {@link https://hono.dev/docs/middleware/builtin/ip-restriction}
|
|
*
|
|
* @param {GetConnInfo | ((c: Context) => string)} getIP - A function to retrieve the client IP address. Use `getConnInfo` from the appropriate runtime adapter.
|
|
* @param {IPRestrictionRules} rules - An object with optional `denyList` and `allowList` arrays of IP rules. Each rule can be a static IP, a CIDR range, or a custom function.
|
|
* @param {(remote: { addr: string; type: AddressType }, c: Context) => Response | Promise<Response>} [onError] - Optional custom handler invoked when a request is blocked. Defaults to returning a 403 Forbidden response.
|
|
* @returns {MiddlewareHandler} The middleware handler function.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* import { Hono } from 'hono'
|
|
* import { ipRestriction } from 'hono/ip-restriction'
|
|
* import { getConnInfo } from 'hono/cloudflare-workers'
|
|
*
|
|
* const app = new Hono()
|
|
*
|
|
* app.use(
|
|
* '*',
|
|
* ipRestriction(getConnInfo, {
|
|
* // Block a specific IP and an entire subnet
|
|
* denyList: ['192.168.0.5', '10.0.0.0/8'],
|
|
* // Only allow requests from localhost and a private range
|
|
* allowList: ['127.0.0.1', '::1', '192.168.1.0/24'],
|
|
* })
|
|
* )
|
|
*
|
|
* // With a custom error handler
|
|
* app.use(
|
|
* '/admin/*',
|
|
* ipRestriction(
|
|
* getConnInfo,
|
|
* { allowList: ['203.0.113.0/24'] },
|
|
* (remote, c) => c.text(`Access denied for ${remote.addr}`, 403)
|
|
* )
|
|
* )
|
|
*
|
|
* app.get('/', (c) => c.text('Hello!'))
|
|
* ```
|
|
*/
|
|
export declare const ipRestriction: (getIP: GetIPAddr, { denyList, allowList }: IPRestrictionRules, onError?: (remote: {
|
|
addr: string;
|
|
type: AddressType;
|
|
}, c: Context) => Response | Promise<Response>) => MiddlewareHandler;
|
|
//#endregion
|
|
export {};
|