-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
221 lines (192 loc) · 5.36 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
const DIGIT_REGEXP = /^[0-9]+$/
/**
* Pre-defined IP ranges.
*/
const IP_RANGES: Record<string, string[]> = {
linklocal: ['169.254.0.0/16', 'fe80::/10'],
loopback: ['127.0.0.1/8', '::1/128'],
uniquelocal: ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7'],
}
/**
* Get all addresses in the request, optionally stopping
* at the first untrusted.
*
* @param request
* @param trust
*/
function alladdrs(req: RequestWithConnection, trust?: Trust) {
// get addresses
const addrs = forwarded(req)
if (!trust) return addrs
if (typeof trust !== 'function') trust = compile(trust)
for (let i = 0; i < addrs.length - 1; i++) {
if (trust(addrs[i], i)) continue
addrs.length = i + 1
}
return addrs
}
/**
* Compile argument into trust function.
*
* @param val
*/
function compile(val: string | string[]) {
let trust
if (typeof val === 'string') trust = [val]
else if (Array.isArray(val)) trust = val.slice()
else throw new TypeError('unsupported trust argument')
for (let i = 0; i < trust.length; i++) {
val = trust[i]
if (!Object.prototype.hasOwnProperty.call(IP_RANGES, val)) {
continue
}
// Splice in pre-defined range
val = IP_RANGES[val]
trust.splice.apply(trust, [i, 1, ...val])
i += val.length - 1
}
return compileTrust(compileRangeSubnets(trust))
}
/**
* Compile `arr` elements into range subnets.
*/
function compileRangeSubnets(arr: string[]) {
const rangeSubnets = new Array(arr.length)
for (let i = 0; i < arr.length; i++) {
rangeSubnets[i] = parseIPNotation(arr[i])
}
return rangeSubnets
}
/**
* Compile range subnet array into trust function.
*
* @param rangeSubnets
*/
function compileTrust(rangeSubnets: (IPv4 | IPv6)[][]) {
// Return optimized function based on length
const len = rangeSubnets.length
return len === 0
? trustNone
: len === 1
? trustSingle(rangeSubnets[0])
: trustMulti(rangeSubnets)
}
/**
* Parse IP notation string into range subnet.
*
* @param {String} note
* @private
*/
export function parseIPNotation(note: string) {
const pos = note.lastIndexOf('/')
const str = pos !== -1 ? note.substring(0, pos) : note
if (!isValid(str)) throw new TypeError('invalid IP address: ' + str)
let ip = parse(str) as IPv4 | IPv6
if (pos === -1 && ip.kind() === '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
if (range === null) range = max
else if (DIGIT_REGEXP.test(range)) range = parseInt(range, 10)
else if (ip.kind() === 'ipv4' && isValid(range)) range = parseNetmask(range)
else range = null
if (range && typeof range === 'number' && (range <= 0 || range > max)) {
throw new TypeError('invalid range on address: ' + note)
}
return [ip, range]
}
/**
* Parse netmask string into CIDR range.
*
* @param netmask
* @private
*/
function parseNetmask(netmask: string) {
const ip = parse(netmask)
return ip.kind() === 'ipv4' ? ip.prefixLengthFromSubnetMask() : null
}
/**
* Determine address of proxied request.
*
* @param request
* @param trust
* @public
*/
export function proxyaddr(req: RequestWithConnection, trust?: Trust) {
const addrs = alladdrs(req, trust)
return addrs[addrs.length - 1]
}
/**
* Static trust function to trust nothing.
*/
const trustNone = () => false
/**
* Compile trust function for multiple subnets.
*/
function trustMulti(subnets: (IPv4 | IPv6)[][]) {
return function trust(addr: string) {
if (!isValid(addr)) return false
const ip = parse(addr)
let ipconv
const kind = ip.kind()
for (let i = 0; i < subnets.length; i++) {
const subnet = subnets[i]
const subnetip = subnet[0]
const subnetkind = subnetip.kind()
const subnetrange = subnet[1]
let trusted = ip
if (kind !== subnetkind) {
if (subnetkind === 'ipv4' && !(ip as IPv6).isIPv4MappedAddress()) {
continue
}
if (!ipconv) {
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
}
}
/**
* Compile trust function for single subnet.
*
* @param subnet
*/
function trustSingle(subnet: (IPv4 | IPv6)[]) {
const subnetip = subnet[0]
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
}
// 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)
}
}
export { alladdrs as all }
export { compile }