Skip to content

Commit

Permalink
use ipaddrjs deno lib instead of esm.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Aug 3, 2023
1 parent 39f2c9b commit 1a34915
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 49 deletions.
20 changes: 20 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="./ipaddr.d.ts" />
export type { RequestWithConnection } from 'https://deno.land/x/[email protected]/mod.ts'
export { forwarded } from 'https://deno.land/x/[email protected]/mod.ts'
export {
IPv4,
IPv6,
isValid,
parse,
} from "https://esm.sh/[email protected]/lib/ipaddr.js?exports=isValid,parse";
export type { IPv4, IPv6 } from "./ipaddr.d.ts";
export type { RequestWithConnection } from "https://deno.land/x/[email protected]/mod.ts";
export { forwarded } from "https://deno.land/x/[email protected]/mod.ts";
} from 'https://deno.land/x/[email protected]/mod.ts'
2 changes: 1 addition & 1 deletion example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server } from 'https://deno.land/std@0.192.0/http/server.ts'
import { Server } from 'https://deno.land/std@0.197.0/http/server.ts'

import { proxyaddr, RequestWithConnection } from './mod.ts'

Expand Down
20 changes: 0 additions & 20 deletions ipaddr.d.ts

This file was deleted.

32 changes: 21 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forwarded, IPv4, IPv6, isValid, parse } from './deps.ts'
import type { RequestWithConnection } from './deps.ts'
import { forwarded, isValid, parse } from './deps.ts'
import type { IPv4, IPv6, RequestWithConnection } from './deps.ts'
export { RequestWithConnection }

type Trust = ((addr: string, i?: number) => boolean) | string[] | string
Expand Down Expand Up @@ -98,19 +98,18 @@ export function parseIPNotation(note: string) {

if (!isValid(str)) throw new TypeError('invalid IP address: ' + str)

let ip = parse(str)
let ip = parse(str) as IPv4 | IPv6

if (pos === -1 && ip.kind() === 'ipv6') {
ip = ip as typeof IPv6
ip = ip as IPv6

if (ip.isIPv4MappedAddress()) ip = ip.toIPv4Address()
}

const max = ip.kind() === 'ipv6' ? 128 : 32

let range: string | number | null = pos !== -1
? note.substring(pos + 1, note.length)
: null
let range: string | number | null =
pos !== -1 ? note.substring(pos + 1, note.length) : null

if (range === null) range = max
else if (DIGIT_REGEXP.test(range)) range = parseInt(range, 10)
Expand Down Expand Up @@ -170,13 +169,15 @@ function trustMulti(subnets: (IPv4 | IPv6)[][]) {
}

if (!ipconv) {
ipconv = subnetkind === 'ipv4'
? ip.toIPv4Address()
: ip.toIPv4MappedAddress()
ipconv =
subnetkind === 'ipv4'
? (ip as IPv6).toIPv4Address()
: (ip as IPv6).toIPv4MappedAddress()
}

trusted = ipconv
}
// @ts-ignore types
if (trusted.match(subnetip, subnetrange)) return true
}
return false
Expand All @@ -192,17 +193,26 @@ function trustSingle(subnet: (IPv4 | IPv6)[]) {
const subnetkind = subnetip.kind()
const subnetisipv4 = subnetkind === 'ipv4'
const subnetrange = subnet[1]

return function trust(addr: string) {
if (!isValid(addr)) return false

let ip = parse(addr)
const kind = ip.kind()

if (kind !== subnetkind) {
if (subnetisipv4 && !(ip as IPv6).isIPv4MappedAddress()) {
// Incompatible IP addresses
return false
}

ip = subnetisipv4 ? ip.toIPv4Address() : ip.toIPv4MappedAddress()
// Convert IP to match subnet IP kind
ip = subnetisipv4
? (ip as IPv6).toIPv4Address()
: (ip as IPv6).toIPv4MappedAddress()
}

// @ts-ignore types
return ip.match(subnetip, subnetrange)
}
}
Expand Down
25 changes: 13 additions & 12 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import {
run,
} from 'https://deno.land/x/[email protected]/mod.ts'
import { all, RequestWithConnection } from './mod.ts'
import { ConnInfo } from 'https://deno.land/std@0.192.0/http/server.ts'
import { ConnInfo } from 'https://deno.land/std@0.197.0/http/server.ts'

const createReq = (
hostname: string,
headers?: Record<string, string>,
): RequestWithConnection => ({
conn: {
remoteAddr: {
hostname,
port: 8081,
transport: 'tcp',
},
} as ConnInfo,
headers: new Headers(headers || {}),
} as unknown as RequestWithConnection)
headers?: Record<string, string>
): RequestWithConnection =>
({
conn: {
remoteAddr: {
hostname,
port: 8081,
transport: 'tcp',
},
} as ConnInfo,
headers: new Headers(headers || {}),
} as unknown as RequestWithConnection)

describe('all(req, trust)', () => {
it('with no headers should return socket address', () => {
Expand Down

0 comments on commit 1a34915

Please sign in to comment.