OLD | NEW |
(Empty) | |
| 1 /*! |
| 2 * Parts of original code from ipv6.js <https://github.com/beaugunderson/javascr
ipt-ipv6> |
| 3 * Copyright 2011 Beau Gunderson |
| 4 * Available under MIT license <http://mths.be/mit> |
| 5 */ |
| 6 |
| 7 const RE_V4 = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|0x[0-9a-f][0-9a-f]?|0
[0-7]{3})\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|0x[0-9a-f][0-9a-f]?|0[0-7
]{3})$/i; |
| 8 const RE_V4_HEX = /^0x([0-9a-f]{8})$/i; |
| 9 const RE_V4_NUMERIC = /^[0-9]+$/; |
| 10 const RE_V4inV6 = /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2
[0-4][0-9]|[01]?[0-9][0-9]?)$/; |
| 11 |
| 12 const RE_BAD_CHARACTERS = /([^0-9a-f:])/i; |
| 13 const RE_BAD_ADDRESS = /([0-9a-f]{5,}|:{3,}|[^:]:$|^:[^:]$)/i; |
| 14 |
| 15 function isIPv4(address) |
| 16 { |
| 17 if (RE_V4.test(address)) |
| 18 return true; |
| 19 if (RE_V4_HEX.test(address)) |
| 20 return true; |
| 21 if (RE_V4_NUMERIC.test(address)) |
| 22 return true; |
| 23 return false; |
| 24 } |
| 25 |
| 26 function isIPv6(address) |
| 27 { |
| 28 var a4addon = 0; |
| 29 var address4 = address.match(RE_V4inV6); |
| 30 if (address4) |
| 31 { |
| 32 var temp4 = address4[0].split('.'); |
| 33 for (var i = 0; i < 4; i++) |
| 34 { |
| 35 if (/^0[0-9]+/.test(temp4[i])) |
| 36 return false; |
| 37 } |
| 38 address = address.replace(RE_V4inV6, ''); |
| 39 if (/[0-9]$/.test(address)) |
| 40 return false; |
| 41 |
| 42 address = address + temp4.join(':'); |
| 43 a4addon = 2; |
| 44 } |
| 45 |
| 46 if (RE_BAD_CHARACTERS.test(address)) |
| 47 return false; |
| 48 |
| 49 if (RE_BAD_ADDRESS.test(address)) |
| 50 return false; |
| 51 |
| 52 function count(string, substring) |
| 53 { |
| 54 return (string.length - string.replace(new RegExp(substring,"g"), '').length
) / substring.length; |
| 55 } |
| 56 |
| 57 var halves = count(address, '::'); |
| 58 if (halves == 1 && count(address, ':') <= 6 + 2 + a4addon) |
| 59 return true; |
| 60 if (halves == 0 && count(address, ':') == 7 + a4addon) |
| 61 return true; |
| 62 return false; |
| 63 } |
| 64 |
| 65 // Returns base domain for specified host based on Public Suffix List |
| 66 function getBaseDomain(hostname) |
| 67 { |
| 68 // remove trailing dot(s) |
| 69 hostname = hostname.replace(/\.+$/, ''); |
| 70 |
| 71 // return IP address untouched |
| 72 if (isIPv6(hostname) || isIPv4(hostname)) |
| 73 return hostname; |
| 74 |
| 75 // decode punycode if exists |
| 76 if (hostname.indexOf('xn--') >= 0) |
| 77 { |
| 78 hostname = punycode.toUnicode(hostname); |
| 79 } |
| 80 |
| 81 // search through PSL |
| 82 var prevDomains = []; |
| 83 var curDomain = hostname; |
| 84 var nextDot = curDomain.indexOf('.'); |
| 85 var tld = 0; |
| 86 |
| 87 while (true) |
| 88 { |
| 89 var suffix = publicSuffixes[curDomain]; |
| 90 if (typeof(suffix) != 'undefined') |
| 91 { |
| 92 tld = suffix; |
| 93 break; |
| 94 } |
| 95 |
| 96 if (nextDot < 0) |
| 97 { |
| 98 tld = 1; |
| 99 break; |
| 100 } |
| 101 |
| 102 prevDomains.push(curDomain.substring(0,nextDot)); |
| 103 curDomain = curDomain.substring(nextDot+1); |
| 104 nextDot = curDomain.indexOf('.'); |
| 105 } |
| 106 |
| 107 while (tld > 0 && prevDomains.length > 0) |
| 108 { |
| 109 curDomain = prevDomains.pop() + '.' + curDomain; |
| 110 tld--; |
| 111 } |
| 112 |
| 113 return curDomain; |
| 114 } |
OLD | NEW |