-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): support http.file and http.isLocal
- Loading branch information
Showing
7 changed files
with
206 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Modified from https://github.com/sindresorhus/ip-regex/blob/3e220cae3eb66ecfdf4f7678bea7306ceaa41c76/index.js | ||
|
||
import { LookupAddress } from 'dns' | ||
import { HTTP } from '../index.js' | ||
|
||
const v4 = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/ | ||
|
||
const v6seg = '[a-fA-F\\d]{1,4}' | ||
|
||
/* eslint-disable no-multi-spaces */ | ||
const v6core = [ | ||
`(?:${v6seg}:){7}(?:${v6seg}|:)`, // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
`(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)`, // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4 | ||
`(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)`, // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4 | ||
`(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)`, // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4 | ||
`(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)`, // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4 | ||
`(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)`, // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4 | ||
`(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)`, // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4 | ||
`(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:))`, // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4 | ||
] | ||
/* eslint-enable no-multi-spaces */ | ||
|
||
const v6 = new RegExp(`^(?:${v6core.join('|')})(?:%[0-9a-zA-Z]{1,})?$`) | ||
|
||
export async function lookup(address: string): Promise<LookupAddress> { | ||
if (v4.test(address)) return { address, family: 4 } | ||
if (v6.test(address)) return { address, family: 6 } | ||
throw new Error('Invalid IP address') | ||
} | ||
|
||
export async function loadFile(url: string): Promise<HTTP.FileResponse | undefined> { | ||
return undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LookupAddress } from 'dns' | ||
import { HTTP } from '../index.ts' | ||
|
||
export function loadFile(url: string): Promise<HTTP.FileResponse | undefined> | ||
export function lookup(address: string): Promise<LookupAddress> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import { basename } from 'node:path' | ||
import { fromBuffer } from 'file-type' | ||
import { HTTP } from '../index.js' | ||
import { readFile } from 'node:fs/promises' | ||
|
||
export { lookup } from 'node:dns/promises' | ||
|
||
export async function loadFile(url: string): Promise<HTTP.FileResponse | undefined> { | ||
if (url.startsWith('file://')) { | ||
const data = await readFile(fileURLToPath(url)) | ||
const result = await fromBuffer(data) | ||
return { mime: result?.mime, name: basename(url), data } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { LookupAddress } from 'dns' | ||
|
||
/* eslint-disable no-multi-spaces */ | ||
const bogonV4 = [ | ||
'0.0.0.0/8', // RFC 1122 'this' network | ||
'10.0.0.0/8', // RFC 1918 private space | ||
'100.64.0.0/10', // RFC 6598 Carrier grade nat space | ||
'127.0.0.0/8', // RFC 1122 localhost | ||
'169.254.0.0/16', // RFC 3927 link local | ||
'172.16.0.0/12', // RFC 1918 private space | ||
'192.0.2.0/24', // RFC 5737 TEST-NET-1 | ||
'192.88.99.0/24', // RFC 7526 6to4 anycast relay | ||
'192.168.0.0/16', // RFC 1918 private space | ||
'198.18.0.0/15', // RFC 2544 benchmarking | ||
'198.51.100.0/24', // RFC 5737 TEST-NET-2 | ||
'203.0.113.0/24', // RFC 5737 TEST-NET-3 | ||
'224.0.0.0/4', // multicast | ||
'240.0.0.0/4', // reserved | ||
] | ||
|
||
const bogonV6 = [ | ||
'::/8', // RFC 4291 IPv4-compatible, loopback, et al | ||
'0100::/64', // RFC 6666 Discard-Only | ||
'2001:2::/48', // RFC 5180 BMWG | ||
'2001:10::/28', // RFC 4843 ORCHID | ||
'2001:db8::/32', // RFC 3849 documentation | ||
'2002::/16', // RFC 7526 6to4 anycast relay | ||
'3ffe::/16', // RFC 3701 old 6bone | ||
'fc00::/7', // RFC 4193 unique local unicast | ||
'fe80::/10', // RFC 4291 link local unicast | ||
'fec0::/10', // RFC 3879 old site local unicast | ||
'ff00::/8', // RFC 4291 multicast | ||
] | ||
/* eslint-enable no-multi-spaces */ | ||
|
||
function parseIPv4(ip: string) { | ||
return ip.split('.').reduce((a, b) => (a << 8n) + BigInt(b), 0n) | ||
} | ||
|
||
function parseIPv6(ip: string) { | ||
const exp = ip.indexOf('::') | ||
let num = 0n | ||
// :: 左边有内容 | ||
if (exp !== -1 && exp !== 0) { | ||
ip.slice(0, exp).split(':').forEach((piece, i) => { | ||
num |= BigInt(`0x${piece}`) << BigInt((7 - i) * 16) | ||
}) | ||
} | ||
// :: 在最右边 | ||
if (exp === ip.length - 2) { | ||
return num | ||
} | ||
// :: 右边的内容 | ||
const rest = exp === -1 ? ip : ip.slice(exp + 2) | ||
const v4 = rest.includes('.') | ||
const pieces = rest.split(':') | ||
let start = 0 | ||
if (v4) { | ||
start += 2 | ||
const [addr] = pieces.splice(-1, 1) | ||
num |= parseIPv4(addr) | ||
} | ||
pieces.reverse().forEach((piece, i) => { | ||
num |= BigInt(`0x${piece}`) << BigInt((start + i) * 8) | ||
}) | ||
return num | ||
} | ||
|
||
export async function isLocalAddress({ address, family }: LookupAddress) { | ||
if (family !== 4 && family !== 6) return false | ||
const { bogons, length, parse } = family === 4 | ||
? { bogons: bogonV4, length: 32, parse: parseIPv4 } | ||
: { bogons: bogonV6, length: 128, parse: parseIPv6 } | ||
const num = parse(address) | ||
for (const bogon of bogons) { | ||
const [prefix, cidr] = bogon.split('/') | ||
const mask = ((1n << BigInt(cidr)) - 1n) << BigInt(length - +cidr) | ||
if ((num & mask) === parse(prefix)) return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters