-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipaddr.js
More file actions
1 lines (1 loc) · 9.6 KB
/
ipaddr.js
File metadata and controls
1 lines (1 loc) · 9.6 KB
1
!function(t){!function(t){"use strict";const r="(0?\\d+|0x[a-f0-9]+)",n={fourOctet:new RegExp("^"+r+"\\."+r+"\\."+r+"\\."+r+"$","i"),longValue:new RegExp("^"+r+"$","i")},e="(?:[0-9a-f]+::?)+",i={zoneIndex:new RegExp("%[0-9a-z]{1,}","i"),native:new RegExp("^(::)?("+e+")?([0-9a-f]+)?(::)?(%[0-9a-z]{1,})?$","i"),transitional:new RegExp("^((?:"+e+")|(?:::)(?:"+e+")?)"+r+"\\."+r+"\\."+r+"\\."+r+"(%[0-9a-z]{1,})?$","i")};function o(t,r){if(t.indexOf("::")!==t.lastIndexOf("::"))return null;let n,e,o=0,s=-1,a=(t.match(i.zoneIndex)||[])[0];for(a&&(a=a.substring(1),t=t.replace(/%.+$/,""));(s=t.indexOf(":",s+1))>=0;)o++;if("::"===t.substr(0,2)&&o--,"::"===t.substr(-2,2)&&o--,o>r)return null;for(e=r-o,n=":";e--;)n+="0:";return":"===(t=t.replace("::",n))[0]&&(t=t.slice(1)),":"===t[t.length-1]&&(t=t.slice(0,-1)),{parts:r=function(){const r=t.split(":"),n=[];for(let t=0;t<r.length;t++)n.push(parseInt(r[t],16));return n}(),zoneId:a}}function s(t,r,n,e){if(t.length!==r.length)throw new Error("ipaddr: cannot match CIDR for objects with different lengths");let i,o=0;for(;e>0;){if((i=n-e)<0&&(i=0),t[o]>>i!=r[o]>>i)return!1;e-=n,o+=1}return!0}function a(t){return"0"===t[0]&&"x"!==t[1]?parseInt(t,8):parseInt(t)}function p(t,r){for(;t.length<r;)t="0"+t;return t}const u={subnetMatch:function(t,r,n){let e,i,o,s;for(i in void 0!==n&&null!==n||(n="unicast"),r)if(Object.prototype.hasOwnProperty.call(r,i))for(!(o=r[i])[0]||o[0]instanceof Array||(o=[o]),e=0;e<o.length;e++)if(s=o[e],t.kind()===s[0].kind()&&t.match.apply(t,s))return i;return n}};u.IPv4=function(){function t(t){if(4!==t.length)throw new Error("ipaddr: ipv4 octet count should be 4");let r,n;for(r=0;r<t.length;r++)if(!(0<=(n=t[r])&&n<=255))throw new Error("ipaddr: ipv4 octet should fit in 8 bits");this.octets=t}return t.prototype.kind=function(){return"ipv4"},t.prototype.toString=function(){return this.octets.join(".")},t.prototype.toNormalizedString=function(){return this.toString()},t.prototype.toByteArray=function(){return this.octets.slice(0)},t.prototype.match=function(t,r){let n;if(void 0===r&&(t=(n=t)[0],r=n[1]),"ipv4"!==t.kind())throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");return s(this.octets,t.octets,8,r)},t.prototype.SpecialRanges={unspecified:[[new t([0,0,0,0]),8]],broadcast:[[new t([255,255,255,255]),32]],multicast:[[new t([224,0,0,0]),4]],linkLocal:[[new t([169,254,0,0]),16]],loopback:[[new t([127,0,0,0]),8]],carrierGradeNat:[[new t([100,64,0,0]),10]],private:[[new t([10,0,0,0]),8],[new t([172,16,0,0]),12],[new t([192,168,0,0]),16]],reserved:[[new t([192,0,0,0]),24],[new t([192,0,2,0]),24],[new t([192,88,99,0]),24],[new t([198,51,100,0]),24],[new t([203,0,113,0]),24],[new t([240,0,0,0]),4]]},t.prototype.range=function(){return u.subnetMatch(this,this.SpecialRanges)},t.prototype.toIPv4MappedAddress=function(){return u.IPv6.parse("::ffff:"+this.toString())},t.prototype.prefixLengthFromSubnetMask=function(){let t=0,r=!1;const n={0:8,128:7,192:6,224:5,240:4,248:3,252:2,254:1,255:0};let e,i,o;for(e=3;e>=0;e-=1){if(!((i=this.octets[e])in n))return null;if(o=n[i],r&&0!==o)return null;8!==o&&(r=!0),t+=o}return 32-t},t}(),u.IPv4.parser=function(t){let r,e,i;if(r=t.match(n.fourOctet))return function(){const t=r.slice(1,6),n=[];for(let r=0;r<t.length;r++)e=t[r],n.push(a(e));return n}();if(r=t.match(n.longValue)){if((i=a(r[1]))>4294967295||i<0)throw new Error("ipaddr: address outside defined range");return function(){const t=[];let r;for(r=0;r<=24;r+=8)t.push(i>>r&255);return t}().reverse()}return null},u.IPv6=function(){function t(t,r){let n,e;if(16===t.length)for(this.parts=[],n=0;n<=14;n+=2)this.parts.push(t[n]<<8|t[n+1]);else{if(8!==t.length)throw new Error("ipaddr: ipv6 part count should be 8 or 16");this.parts=t}for(n=0;n<this.parts.length;n++)if(!(0<=(e=this.parts[n])&&e<=65535))throw new Error("ipaddr: ipv6 part should fit in 16 bits");r&&(this.zoneId=r)}return t.prototype.kind=function(){return"ipv6"},t.prototype.toString=function(){return this.toNormalizedString().replace(/((^|:)(0(:|$))+)/,"::")},t.prototype.toRFC5952String=function(){const t=/((^|:)(0(:|$)){2,})/g,r=this.toNormalizedString();let n,e=0,i=-1;for(;n=t.exec(r);)n[0].length>i&&(e=n.index,i=n[0].length);return i<0?r:r.substring(0,e)+"::"+r.substring(e+i)},t.prototype.toByteArray=function(){let t;const r=[],n=this.parts;for(let e=0;e<n.length;e++)t=n[e],r.push(t>>8),r.push(255&t);return r},t.prototype.toNormalizedString=function(){const t=function(){const t=[];for(let r=0;r<this.parts.length;r++)t.push(this.parts[r].toString(16));return t}.call(this).join(":");let r="";return this.zoneId&&(r="%"+this.zoneId),t+r},t.prototype.toFixedLengthString=function(){const t=function(){const t=[];for(let r=0;r<this.parts.length;r++)t.push(p(this.parts[r].toString(16),4));return t}.call(this).join(":");let r="";return this.zoneId&&(r="%"+this.zoneId),t+r},t.prototype.match=function(t,r){let n;if(void 0===r&&(t=(n=t)[0],r=n[1]),"ipv6"!==t.kind())throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");return s(this.parts,t.parts,16,r)},t.prototype.SpecialRanges={unspecified:[new t([0,0,0,0,0,0,0,0]),128],linkLocal:[new t([65152,0,0,0,0,0,0,0]),10],multicast:[new t([65280,0,0,0,0,0,0,0]),8],loopback:[new t([0,0,0,0,0,0,0,1]),128],uniqueLocal:[new t([64512,0,0,0,0,0,0,0]),7],ipv4Mapped:[new t([0,0,0,0,0,65535,0,0]),96],rfc6145:[new t([0,0,0,0,65535,0,0,0]),96],rfc6052:[new t([100,65435,0,0,0,0,0,0]),96],"6to4":[new t([8194,0,0,0,0,0,0,0]),16],teredo:[new t([8193,0,0,0,0,0,0,0]),32],reserved:[[new t([8193,3512,0,0,0,0,0,0]),32]]},t.prototype.range=function(){return u.subnetMatch(this,this.SpecialRanges)},t.prototype.isIPv4MappedAddress=function(){return"ipv4Mapped"===this.range()},t.prototype.toIPv4Address=function(){if(!this.isIPv4MappedAddress())throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");const t=this.parts.slice(-2),r=t[0],n=t[1];return new u.IPv4([r>>8,255&r,n>>8,255&n])},t.prototype.prefixLengthFromSubnetMask=function(){let t=0,r=!1;const n={0:16,32768:15,49152:14,57344:13,61440:12,63488:11,64512:10,65024:9,65280:8,65408:7,65472:6,65504:5,65520:4,65528:3,65532:2,65534:1,65535:0};let e,i;for(let o=7;o>=0;o-=1){if(!((e=this.parts[o])in n))return null;if(i=n[e],r&&0!==i)return null;16!==i&&(r=!0),t+=i}return 128-t},t}(),u.IPv6.parser=function(t){let r,n,e,s,a,p;if(i.native.test(t))return o(t,8);if((e=t.match(i.transitional))&&(p=e[6]||"",(r=o(e[1].slice(0,-1)+p,6)).parts)){for(a=[parseInt(e[2]),parseInt(e[3]),parseInt(e[4]),parseInt(e[5])],n=0;n<a.length;n++)if(!(0<=(s=a[n])&&s<=255))return null;return r.parts.push(a[0]<<8|a[1]),r.parts.push(a[2]<<8|a[3]),{parts:r.parts,zoneId:r.zoneId}}return null},u.IPv4.isIPv4=function(t){return null!==this.parser(t)},u.IPv6.isIPv6=function(t){return null!==this.parser(t)},u.IPv4.isValid=function(t){try{return new this(this.parser(t)),!0}catch(t){return!1}},u.IPv4.isValidFourPartDecimal=function(t){return!(!u.IPv4.isValid(t)||!t.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/))},u.IPv6.isValid=function(t){if("string"==typeof t&&-1===t.indexOf(":"))return!1;try{const r=this.parser(t);return new this(r.parts,r.zoneId),!0}catch(t){return!1}},u.IPv4.parse=function(t){const r=this.parser(t);if(null===r)throw new Error("ipaddr: string is not formatted like ip address");return new this(r)},u.IPv6.parse=function(t){const r=this.parser(t);if(null===r.parts)throw new Error("ipaddr: string is not formatted like ip address");return new this(r.parts,r.zoneId)},u.IPv4.parseCIDR=function(t){let r;if(r=t.match(/^(.+)\/(\d+)$/)){const t=parseInt(r[2]);if(t>=0&&t<=32){const n=[this.parse(r[1]),t];return Object.defineProperty(n,"toString",{value:function(){return this.join("/")}}),n}}throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range")},u.IPv4.subnetMaskFromPrefixLength=function(t){if((t=parseInt(t))<0||t>32)throw new Error("ipaddr: invalid IPv4 prefix length");const r=[0,0,0,0];let n=0;const e=Math.floor(t/8);for(;n<e;)r[n]=255,n++;return e<4&&(r[e]=Math.pow(2,t%8)-1<<8-t%8),new this(r)},u.IPv4.broadcastAddressFromCIDR=function(t){try{const r=this.parseCIDR(t),n=r[0].toByteArray(),e=this.subnetMaskFromPrefixLength(r[1]).toByteArray(),i=[];let o=0;for(;o<4;)i.push(parseInt(n[o],10)|255^parseInt(e[o],10)),o++;return new this(i)}catch(t){throw new Error("ipaddr: the address does not have IPv4 CIDR format")}},u.IPv4.networkAddressFromCIDR=function(t){let r,n,e,i,o;try{for(e=(r=this.parseCIDR(t))[0].toByteArray(),o=this.subnetMaskFromPrefixLength(r[1]).toByteArray(),i=[],n=0;n<4;)i.push(parseInt(e[n],10)&parseInt(o[n],10)),n++;return new this(i)}catch(t){throw new Error("ipaddr: the address does not have IPv4 CIDR format")}},u.IPv6.parseCIDR=function(t){let r,n,e;if((n=t.match(/^(.+)\/(\d+)$/))&&(r=parseInt(n[2]))>=0&&r<=128)return e=[this.parse(n[1]),r],Object.defineProperty(e,"toString",{value:function(){return this.join("/")}}),e;throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range")},u.isValid=function(t){return u.IPv6.isValid(t)||u.IPv4.isValid(t)},u.parse=function(t){if(u.IPv6.isValid(t))return u.IPv6.parse(t);if(u.IPv4.isValid(t))return u.IPv4.parse(t);throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format")},u.parseCIDR=function(t){try{return u.IPv6.parseCIDR(t)}catch(r){try{return u.IPv4.parseCIDR(t)}catch(t){throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format")}}},u.fromByteArray=function(t){const r=t.length;if(4===r)return new u.IPv4(t);if(16===r)return new u.IPv6(t);throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address")},u.process=function(t){const r=this.parse(t);return"ipv6"===r.kind()&&r.isIPv4MappedAddress()?r.toIPv4Address():r},"undefined"!=typeof module&&module.exports?module.exports=u:t.ipaddr=u}(this)}("undefined"==typeof window?window={}:window);