|
| 1 | +import type { Context } from "../context.ts"; |
| 2 | +import type { Middleware } from "./mod.ts"; |
| 3 | +import { isIPv4, matchSubnets } from "@std/net/unstable-ip"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Configuration rules for IP restriction middleware. |
| 7 | + */ |
| 8 | +export interface IpFilterRules { |
| 9 | + /** |
| 10 | + * List of IP addresses or CIDR blocks to deny access. |
| 11 | + * If an IP matches any entry in this list, access will be blocked. |
| 12 | + * |
| 13 | + * @example ["192.168.1.10", "10.0.0.0/8", "2001:db8::1"] |
| 14 | + */ |
| 15 | + denyList?: string[]; |
| 16 | + |
| 17 | + /** |
| 18 | + * List of IP addresses or CIDR blocks to allow access. |
| 19 | + * If specified, only IPs matching entries in this list will be allowed. |
| 20 | + * If empty or undefined, all IPs are allowed (unless in denyList). |
| 21 | + * |
| 22 | + * @example ["192.168.1.0/24", "203.0.113.0/24", "2001:db8::/32"] |
| 23 | + */ |
| 24 | + allowList?: string[]; |
| 25 | +} |
| 26 | + |
| 27 | +export interface IpFilterOptions { |
| 28 | + /** |
| 29 | + * Called when a request is blocked by the IP filter. |
| 30 | + * |
| 31 | + * The function receives the remote information and the current request |
| 32 | + * context and should return a Response (or a Promise resolving to one) |
| 33 | + * which will be sent back to the client. If not provided, a default |
| 34 | + * 403 Forbidden response will be used. |
| 35 | + * |
| 36 | + * Parameters: |
| 37 | + * - `remote.addr` - the remote IP address as a string. |
| 38 | + * - `remote.type` - the network family: "IPv4" or "IPv6". |
| 39 | + * - `ctx` - the request `Context` which can be used to inspect the |
| 40 | + * request or produce a custom response. |
| 41 | + * |
| 42 | + * @example |
| 43 | + * ```ts |
| 44 | + * const options: IpFilterOptions = { |
| 45 | + * onBlocked: (remote, ctx) => { |
| 46 | + * console.log(`Blocked ${remote.addr} (${remote.type})`, ctx.url); |
| 47 | + * return new Response("Access denied", { status: 401 }); |
| 48 | + * }, |
| 49 | + * }; |
| 50 | + * ``` |
| 51 | + */ |
| 52 | + onBlocked?: <State>( |
| 53 | + remote: { |
| 54 | + addr: string; |
| 55 | + type: Deno.NetworkInterfaceInfo["family"]; |
| 56 | + }, |
| 57 | + ctx: Context<State>, |
| 58 | + ) => Response | Promise<Response>; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * IP restriction Middleware for Fresh. |
| 63 | + * |
| 64 | + * @param rules Deny and allow rules object. |
| 65 | + * @param options Options for the IP Restriction middleware. |
| 66 | + * @returns The middleware handler function. |
| 67 | + * |
| 68 | + * Filters rule priority is denyList, then allowList. |
| 69 | + * If an IP is in the denyList, it will be blocked regardless of the allowList. |
| 70 | + * |
| 71 | + * @example Basic usage (with defaults) |
| 72 | + * ```ts |
| 73 | + * const app = new App<State>() |
| 74 | + * |
| 75 | + * app.use(ipFilter({ |
| 76 | + * denyList: ["192.168.1.10", "2001:db8::1"] |
| 77 | + * })); |
| 78 | + * ``` |
| 79 | + * |
| 80 | + * @example Custom blocked handler |
| 81 | + * ```ts |
| 82 | + * app.use(ipFilter({ |
| 83 | + * denyList: ["192.168.1.10", "2001:db8::1"] |
| 84 | + * }, { |
| 85 | + * onBlocked: (remote, ctx) => { |
| 86 | + * console.log( |
| 87 | + * `Request URL: ${ctx.url}, Blocked IP: ${remote.addr} of type ${remote.type}`, |
| 88 | + * ); |
| 89 | + * return new Response("Access denied", { status: 401 }); |
| 90 | + * }, |
| 91 | + * })); |
| 92 | + * ``` |
| 93 | + */ |
| 94 | +export function ipFilter<State>( |
| 95 | + rules: IpFilterRules, |
| 96 | + options?: IpFilterOptions, |
| 97 | +): Middleware<State> { |
| 98 | + const onBlock = options?.onBlocked ?? |
| 99 | + (() => new Response("Forbidden", { status: 403 })); |
| 100 | + return function ipFilter<State>(ctx: Context<State>) { |
| 101 | + if ( |
| 102 | + ctx.info.remoteAddr.transport !== "udp" && |
| 103 | + ctx.info.remoteAddr.transport !== "tcp" |
| 104 | + ) { |
| 105 | + return ctx.next(); |
| 106 | + } |
| 107 | + |
| 108 | + const addr = ctx.info.remoteAddr.hostname; |
| 109 | + const type = isIPv4(addr) ? "IPv4" : "IPv6"; |
| 110 | + |
| 111 | + if (matchSubnets(addr, rules.denyList || [])) { |
| 112 | + return onBlock({ addr, type }, ctx); |
| 113 | + } |
| 114 | + |
| 115 | + if ( |
| 116 | + (rules.allowList || []).length === 0 || |
| 117 | + matchSubnets(addr, rules.allowList || []) |
| 118 | + ) { |
| 119 | + return ctx.next(); |
| 120 | + } |
| 121 | + |
| 122 | + return onBlock({ addr, type }, ctx); |
| 123 | + }; |
| 124 | +} |
0 commit comments