File size: 2,057 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Check if a hostname is an IP. You should be aware that this only works
 * because `hostname` is already garanteed to be a valid hostname!
 */
function isProbablyIpv4(hostname: string): boolean {
  // Cannot be shorted than 1.1.1.1
  if (hostname.length < 7) {
    return false;
  }

  // Cannot be longer than: 255.255.255.255
  if (hostname.length > 15) {
    return false;
  }

  let numberOfDots = 0;

  for (let i = 0; i < hostname.length; i += 1) {
    const code = hostname.charCodeAt(i);

    if (code === 46 /* '.' */) {
      numberOfDots += 1;
    } else if (code < 48 /* '0' */ || code > 57 /* '9' */) {
      return false;
    }
  }

  return (
    numberOfDots === 3 &&
    hostname.charCodeAt(0) !== 46 /* '.' */ &&
    hostname.charCodeAt(hostname.length - 1) !== 46 /* '.' */
  );
}

/**
 * Similar to isProbablyIpv4.
 */
function isProbablyIpv6(hostname: string): boolean {
  if (hostname.length < 3) {
    return false;
  }

  let start = hostname.startsWith('[') ? 1 : 0;
  let end = hostname.length;

  if (hostname[end - 1] === ']') {
    end -= 1;
  }

  // We only consider the maximum size of a normal IPV6. Note that this will
  // fail on so-called "IPv4 mapped IPv6 addresses" but this is a corner-case
  // and a proper validation library should be used for these.
  if (end - start > 39) {
    return false;
  }

  let hasColon = false;

  for (; start < end; start += 1) {
    const code = hostname.charCodeAt(start);

    if (code === 58 /* ':' */) {
      hasColon = true;
    } else if (
      !(
        (
          (code >= 48 && code <= 57) || // 0-9
          (code >= 97 && code <= 102) || // a-f
          (code >= 65 && code <= 90)
        ) // A-F
      )
    ) {
      return false;
    }
  }

  return hasColon;
}

/**
 * Check if `hostname` is *probably* a valid ip addr (either ipv6 or ipv4).
 * This *will not* work on any string. We need `hostname` to be a valid
 * hostname.
 */
export default function isIp(hostname: string): boolean {
  return isProbablyIpv6(hostname) || isProbablyIpv4(hostname);
}