1
- // deno-lint-ignore-file
2
- import { default as ipaddr , IPv4 , IPv6 } from 'https://esm.sh/[email protected] /'
3
- import { forwarded } from 'https://deno.land/x/[email protected] /mod.ts'
4
- import type { RequestWithConnection } from 'https://deno.land/x/[email protected] /mod.ts'
1
+ import { forwarded , IPv4 , IPv6 , isValid , parse } from './deps.ts'
2
+ import type { RequestWithConnection } from './deps.ts'
3
+ export { RequestWithConnection }
5
4
6
- export type { RequestWithConnection }
5
+ type Trust = ( ( addr : string , i ?: number ) => boolean ) | string [ ] | string
7
6
8
7
const DIGIT_REGEXP = / ^ [ 0 - 9 ] + $ /
9
- const isip = ipaddr . isValid
10
- const parseip = ipaddr . parse
11
8
/**
12
9
* Pre-defined IP ranges.
13
10
*/
@@ -23,10 +20,7 @@ const IP_RANGES: Record<string, string[]> = {
23
20
* @param request
24
21
* @param trust
25
22
*/
26
- function alladdrs (
27
- req : RequestWithConnection ,
28
- trust ?: ( ( ...args : any [ ] ) => any ) | any [ ] | string [ ] | string ,
29
- ) {
23
+ function alladdrs ( req : RequestWithConnection , trust ?: Trust ) {
30
24
// get addresses
31
25
32
26
const addrs = forwarded ( req )
@@ -70,9 +64,11 @@ function compile(val: string | string[]) {
70
64
/**
71
65
* Compile `arr` elements into range subnets.
72
66
*/
73
- function compileRangeSubnets ( arr : any [ ] ) {
67
+ function compileRangeSubnets ( arr : string [ ] ) {
74
68
const rangeSubnets = new Array ( arr . length )
75
- for ( let i = 0 ; i < arr . length ; i ++ ) rangeSubnets [ i ] = parseIPNotation ( arr [ i ] )
69
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
70
+ rangeSubnets [ i ] = parseIPNotation ( arr [ i ] )
71
+ }
76
72
77
73
return rangeSubnets
78
74
}
@@ -81,7 +77,7 @@ function compileRangeSubnets(arr: any[]) {
81
77
*
82
78
* @param rangeSubnets
83
79
*/
84
- function compileTrust ( rangeSubnets : any [ ] ) {
80
+ function compileTrust ( rangeSubnets : ( IPv4 | IPv6 ) [ ] [ ] ) {
85
81
// Return optimized function based on length
86
82
const len = rangeSubnets . length
87
83
return len === 0
@@ -100,9 +96,9 @@ export function parseIPNotation(note: string) {
100
96
const pos = note . lastIndexOf ( '/' )
101
97
const str = pos !== - 1 ? note . substring ( 0 , pos ) : note
102
98
103
- if ( ! isip ( str ) ) throw new TypeError ( 'invalid IP address: ' + str )
99
+ if ( ! isValid ( str ) ) throw new TypeError ( 'invalid IP address: ' + str )
104
100
105
- let ip = parseip ( str )
101
+ let ip = parse ( str )
106
102
107
103
if ( pos === - 1 && ip . kind ( ) === 'ipv6' ) {
108
104
ip = ip as typeof IPv6
@@ -118,7 +114,7 @@ export function parseIPNotation(note: string) {
118
114
119
115
if ( range === null ) range = max
120
116
else if ( DIGIT_REGEXP . test ( range ) ) range = parseInt ( range , 10 )
121
- else if ( ip . kind ( ) === 'ipv4' && isip ( range ) ) range = parseNetmask ( range )
117
+ else if ( ip . kind ( ) === 'ipv4' && isValid ( range ) ) range = parseNetmask ( range )
122
118
else range = null
123
119
124
120
if ( range && typeof range === 'number' && ( range <= 0 || range > max ) ) {
@@ -134,7 +130,7 @@ export function parseIPNotation(note: string) {
134
130
* @private
135
131
*/
136
132
function parseNetmask ( netmask : string ) {
137
- const ip = parseip ( netmask )
133
+ const ip = parse ( netmask )
138
134
return ip . kind ( ) === 'ipv4' ? ip . prefixLengthFromSubnetMask ( ) : null
139
135
}
140
136
/**
@@ -144,10 +140,7 @@ function parseNetmask(netmask: string) {
144
140
* @param trust
145
141
* @public
146
142
*/
147
- export function proxyaddr (
148
- req : RequestWithConnection ,
149
- trust ?: ( ( ...args : any [ ] ) => any ) | any [ ] | string [ ] | string ,
150
- ) {
143
+ export function proxyaddr ( req : RequestWithConnection , trust ?: Trust ) {
151
144
const addrs = alladdrs ( req , trust )
152
145
153
146
return addrs [ addrs . length - 1 ]
@@ -159,10 +152,10 @@ const trustNone = () => false
159
152
/**
160
153
* Compile trust function for multiple subnets.
161
154
*/
162
- function trustMulti ( subnets : any [ ] ) {
155
+ function trustMulti ( subnets : ( IPv4 | IPv6 ) [ ] [ ] ) {
163
156
return function trust ( addr : string ) {
164
- if ( ! isip ( addr ) ) return false
165
- const ip = parseip ( addr )
157
+ if ( ! isValid ( addr ) ) return false
158
+ const ip = parse ( addr )
166
159
let ipconv
167
160
const kind = ip . kind ( )
168
161
for ( let i = 0 ; i < subnets . length ; i ++ ) {
@@ -172,19 +165,19 @@ function trustMulti(subnets: any[]) {
172
165
const subnetrange = subnet [ 1 ]
173
166
let trusted = ip
174
167
if ( kind !== subnetkind ) {
175
- if (
176
- subnetkind === 'ipv4' && ! ( ip as typeof IPv6 ) . isIPv4MappedAddress ( )
177
- ) continue
168
+ if ( subnetkind === 'ipv4' && ! ( ip as IPv6 ) . isIPv4MappedAddress ( ) ) {
169
+ continue
170
+ }
178
171
179
172
if ( ! ipconv ) {
180
173
ipconv = subnetkind === 'ipv4'
181
- ? ( ip as typeof IPv6 ) . toIPv4Address ( )
182
- : ( ip as typeof IPv4 ) . toIPv4MappedAddress ( )
174
+ ? ip . toIPv4Address ( )
175
+ : ip . toIPv4MappedAddress ( )
183
176
}
184
177
185
178
trusted = ipconv
186
179
}
187
- if ( ( trusted as typeof IPv4 ) . match ( subnetip , subnetrange ) ) return true
180
+ if ( trusted . match ( subnetip , subnetrange ) ) return true
188
181
}
189
182
return false
190
183
}
@@ -194,25 +187,23 @@ function trustMulti(subnets: any[]) {
194
187
*
195
188
* @param subnet
196
189
*/
197
- function trustSingle ( subnet : any [ ] ) {
190
+ function trustSingle ( subnet : ( IPv4 | IPv6 ) [ ] ) {
198
191
const subnetip = subnet [ 0 ]
199
192
const subnetkind = subnetip . kind ( )
200
193
const subnetisipv4 = subnetkind === 'ipv4'
201
194
const subnetrange = subnet [ 1 ]
202
195
return function trust ( addr : string ) {
203
- if ( ! isip ( addr ) ) return false
204
- let ip = parseip ( addr )
196
+ if ( ! isValid ( addr ) ) return false
197
+ let ip = parse ( addr )
205
198
const kind = ip . kind ( )
206
199
if ( kind !== subnetkind ) {
207
- if ( subnetisipv4 && ! ( ip as typeof IPv6 ) . isIPv4MappedAddress ( ) ) {
200
+ if ( subnetisipv4 && ! ( ip as IPv6 ) . isIPv4MappedAddress ( ) ) {
208
201
return false
209
202
}
210
203
211
- ip = subnetisipv4
212
- ? ( ip as typeof IPv6 ) . toIPv4Address ( )
213
- : ( ip as typeof IPv4 ) . toIPv4MappedAddress ( )
204
+ ip = subnetisipv4 ? ip . toIPv4Address ( ) : ip . toIPv4MappedAddress ( )
214
205
}
215
- return ( ip as typeof IPv6 ) . match ( subnetip , subnetrange )
206
+ return ip . match ( subnetip , subnetrange )
216
207
}
217
208
}
218
209
0 commit comments