-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
49 lines (43 loc) · 1.3 KB
/
index.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
/**
* Local Network Addresses
*/
export const localNetworkAddresses = new Set([
'localhost',
'127.0.0.1',
'::1',
'::ffff:127.0.0.1'
])
/*
* Private Network Address Ranges
*/
export const privateNetworkAddressRanges = [
/^(::ffff:)?10(\.\d{1,3}){3}$/,
/^(::ffff:)?172\.(1[6-9]|2\d|3[01])(\.\d{1,3}){2}$/,
/^(::ffff:)?192\.168(\.\d{1,3}){2}$/,
/^f[cd][\da-f]{0,2}(:([\da-f]){0,4}){0,7}$/
]
/**
* Tests if a given network address is a local network address.
* @param networkAddress - A network address string.
* @returns `true` if the network address string represents a local network address.
*/
export function isLocal(networkAddress: string): boolean {
return localNetworkAddresses.has(networkAddress.toLowerCase())
}
/**
* Tests if a given network address is a private network address.
* @param networkAddress - A network address string.
* @returns `true` if the network address string represents a private network address.
*/
export function isPrivate(networkAddress: string): boolean {
if (isLocal(networkAddress)) {
return true
}
const networkAddressLowerCase = networkAddress.toLowerCase()
for (const privateNetworkAddressRange of privateNetworkAddressRanges) {
if (privateNetworkAddressRange.test(networkAddressLowerCase)) {
return true
}
}
return false
}