MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / isPrivate

Function isPrivate

client/src/components/formHelpers/form-ip-utils.ts:15–51  ·  view source on GitHub ↗
(address: string)

Source from the content-addressed store, hash-verified

13*/
14
15export function isPrivate(address: string) {
16 // Strip `https?` prefix
17 let addr = address.replace(/^https?:\/\//, '');
18
19 // Check if localhost
20 if (/^localhost:/.test(addr)) {
21 return true;
22 }
23 // Check loopback addresses first
24 if (isLoopback(addr)) {
25 return true;
26 }
27
28 // Ensure the ipv4 address is valid
29 if (!isV6Format(addr)) {
30 const ipl = normalizeToLong(addr);
31 if (ipl < 0) {
32 return false;
33 }
34 // Normalize the address for the private range checks that follow
35 addr = fromLong(ipl);
36 }
37
38 // Check private ranges
39 return (
40 /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
41 /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
42 /^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(
43 addr
44 ) ||
45 /^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
46 /^f[cd][0-9a-f]{2}:/i.test(addr) ||
47 /^fe80:/i.test(addr) ||
48 /^::1$/.test(addr) ||
49 /^::$/.test(addr)
50 );
51}
52
53function isLoopback(addr: string) {
54 // If addr is an IPv4 address in long integer form (no dots and no colons), convert it

Callers 3

localhostValidatorFunction · 0.90
httpValidatorFunction · 0.90

Calls 4

isLoopbackFunction · 0.85
isV6FormatFunction · 0.85
normalizeToLongFunction · 0.85
fromLongFunction · 0.85

Tested by

no test coverage detected