1
- import { forwarded , IPv4 , IPv6 , isValid , parse } from './deps.ts'
2
- import type { RequestWithConnection } from './deps.ts'
1
+ import { forwarded , isValid , parse } from './deps.ts'
2
+ import type { IPv4 , IPv6 , RequestWithConnection } from './deps.ts'
3
3
export { RequestWithConnection }
4
4
5
5
type Trust = ( ( addr : string , i ?: number ) => boolean ) | string [ ] | string
@@ -98,19 +98,18 @@ export function parseIPNotation(note: string) {
98
98
99
99
if ( ! isValid ( str ) ) throw new TypeError ( 'invalid IP address: ' + str )
100
100
101
- let ip = parse ( str )
101
+ let ip = parse ( str ) as IPv4 | IPv6
102
102
103
103
if ( pos === - 1 && ip . kind ( ) === 'ipv6' ) {
104
- ip = ip as typeof IPv6
104
+ ip = ip as IPv6
105
105
106
106
if ( ip . isIPv4MappedAddress ( ) ) ip = ip . toIPv4Address ( )
107
107
}
108
108
109
109
const max = ip . kind ( ) === 'ipv6' ? 128 : 32
110
110
111
- let range : string | number | null = pos !== - 1
112
- ? note . substring ( pos + 1 , note . length )
113
- : null
111
+ let range : string | number | null =
112
+ pos !== - 1 ? note . substring ( pos + 1 , note . length ) : null
114
113
115
114
if ( range === null ) range = max
116
115
else if ( DIGIT_REGEXP . test ( range ) ) range = parseInt ( range , 10 )
@@ -170,13 +169,15 @@ function trustMulti(subnets: (IPv4 | IPv6)[][]) {
170
169
}
171
170
172
171
if ( ! ipconv ) {
173
- ipconv = subnetkind === 'ipv4'
174
- ? ip . toIPv4Address ( )
175
- : ip . toIPv4MappedAddress ( )
172
+ ipconv =
173
+ subnetkind === 'ipv4'
174
+ ? ( ip as IPv6 ) . toIPv4Address ( )
175
+ : ( ip as IPv6 ) . toIPv4MappedAddress ( )
176
176
}
177
177
178
178
trusted = ipconv
179
179
}
180
+ // @ts -ignore types
180
181
if ( trusted . match ( subnetip , subnetrange ) ) return true
181
182
}
182
183
return false
@@ -192,17 +193,26 @@ function trustSingle(subnet: (IPv4 | IPv6)[]) {
192
193
const subnetkind = subnetip . kind ( )
193
194
const subnetisipv4 = subnetkind === 'ipv4'
194
195
const subnetrange = subnet [ 1 ]
196
+
195
197
return function trust ( addr : string ) {
196
198
if ( ! isValid ( addr ) ) return false
199
+
197
200
let ip = parse ( addr )
198
201
const kind = ip . kind ( )
202
+
199
203
if ( kind !== subnetkind ) {
200
204
if ( subnetisipv4 && ! ( ip as IPv6 ) . isIPv4MappedAddress ( ) ) {
205
+ // Incompatible IP addresses
201
206
return false
202
207
}
203
208
204
- ip = subnetisipv4 ? ip . toIPv4Address ( ) : ip . toIPv4MappedAddress ( )
209
+ // Convert IP to match subnet IP kind
210
+ ip = subnetisipv4
211
+ ? ( ip as IPv6 ) . toIPv4Address ( )
212
+ : ( ip as IPv6 ) . toIPv4MappedAddress ( )
205
213
}
214
+
215
+ // @ts -ignore types
206
216
return ip . match ( subnetip , subnetrange )
207
217
}
208
218
}
0 commit comments