ipaddr.min.js
8.26 KB
(function(){var r,t,e,n,i,o,a,s;t={},s=this,"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:s.ipaddr=t,a=function(r,t,e,n){var i,o;if(r.length!==t.length)throw new Error("ipaddr: cannot match CIDR for objects with different lengths");for(i=0;n>0;){if((o=e-n)<0&&(o=0),r[i]>>o!=t[i]>>o)return!1;n-=e,i+=1}return!0},t.subnetMatch=function(r,t,e){var n,i,o,a,s;null==e&&(e="unicast");for(o in t)for(!(a=t[o])[0]||a[0]instanceof Array||(a=[a]),n=0,i=a.length;n<i;n++)if(s=a[n],r.match.apply(r,s))return o;return e},t.IPv4=function(){function r(r){var t,e,n;if(4!==r.length)throw new Error("ipaddr: ipv4 octet count should be 4");for(t=0,e=r.length;t<e;t++)if(!(0<=(n=r[t])&&n<=255))throw new Error("ipaddr: ipv4 octet should fit in 8 bits");this.octets=r}return r.prototype.kind=function(){return"ipv4"},r.prototype.toString=function(){return this.octets.join(".")},r.prototype.toByteArray=function(){return this.octets.slice(0)},r.prototype.match=function(r,t){var e;if(void 0===t&&(r=(e=r)[0],t=e[1]),"ipv4"!==r.kind())throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");return a(this.octets,r.octets,8,t)},r.prototype.SpecialRanges={unspecified:[[new r([0,0,0,0]),8]],broadcast:[[new r([255,255,255,255]),32]],multicast:[[new r([224,0,0,0]),4]],linkLocal:[[new r([169,254,0,0]),16]],loopback:[[new r([127,0,0,0]),8]],carrierGradeNat:[[new r([100,64,0,0]),10]],private:[[new r([10,0,0,0]),8],[new r([172,16,0,0]),12],[new r([192,168,0,0]),16]],reserved:[[new r([192,0,0,0]),24],[new r([192,0,2,0]),24],[new r([192,88,99,0]),24],[new r([198,51,100,0]),24],[new r([203,0,113,0]),24],[new r([240,0,0,0]),4]]},r.prototype.range=function(){return t.subnetMatch(this,this.SpecialRanges)},r.prototype.toIPv4MappedAddress=function(){return t.IPv6.parse("::ffff:"+this.toString())},r.prototype.prefixLengthFromSubnetMask=function(){var r,t,e,n,i,o,a;for(a={0:8,128:7,192:6,224:5,240:4,248:3,252:2,254:1,255:0},r=0,i=!1,t=e=3;e>=0;t=e+=-1){if(!((n=this.octets[t])in a))return null;if(o=a[n],i&&0!==o)return null;8!==o&&(i=!0),r+=o}return 32-r},r}(),e="(0?\\d+|0x[a-f0-9]+)",n={fourOctet:new RegExp("^"+e+"\\."+e+"\\."+e+"\\."+e+"$","i"),longValue:new RegExp("^"+e+"$","i")},t.IPv4.parser=function(r){var t,e,i,o,a;if(e=function(r){return"0"===r[0]&&"x"!==r[1]?parseInt(r,8):parseInt(r)},t=r.match(n.fourOctet))return function(){var r,n,o,a;for(a=[],r=0,n=(o=t.slice(1,6)).length;r<n;r++)i=o[r],a.push(e(i));return a}();if(t=r.match(n.longValue)){if((a=e(t[1]))>4294967295||a<0)throw new Error("ipaddr: address outside defined range");return function(){var r,t;for(t=[],o=r=0;r<=24;o=r+=8)t.push(a>>o&255);return t}().reverse()}return null},t.IPv6=function(){function r(r){var t,e,n,i,o,a;if(16===r.length)for(this.parts=[],t=e=0;e<=14;t=e+=2)this.parts.push(r[t]<<8|r[t+1]);else{if(8!==r.length)throw new Error("ipaddr: ipv6 part count should be 8 or 16");this.parts=r}for(n=0,i=(a=this.parts).length;n<i;n++)if(!(0<=(o=a[n])&&o<=65535))throw new Error("ipaddr: ipv6 part should fit in 16 bits")}return r.prototype.kind=function(){return"ipv6"},r.prototype.toString=function(){var r,t,e,n,i,o,a;for(a=function(){var r,t,e,i;for(i=[],r=0,t=(e=this.parts).length;r<t;r++)n=e[r],i.push(n.toString(16));return i}.call(this),r=[],i=function(t){return r.push(t)},o=0,t=0,e=a.length;t<e;t++)switch(n=a[t],o){case 0:i("0"===n?"":n),o=1;break;case 1:"0"===n?o=2:i(n);break;case 2:"0"!==n&&(i(""),i(n),o=3);break;case 3:i(n)}return 2===o&&(i(""),i("")),r.join(":")},r.prototype.toByteArray=function(){var r,t,e,n,i;for(r=[],t=0,e=(i=this.parts).length;t<e;t++)n=i[t],r.push(n>>8),r.push(255&n);return r},r.prototype.toNormalizedString=function(){var r;return function(){var t,e,n,i;for(i=[],t=0,e=(n=this.parts).length;t<e;t++)r=n[t],i.push(r.toString(16));return i}.call(this).join(":")},r.prototype.match=function(r,t){var e;if(void 0===t&&(r=(e=r)[0],t=e[1]),"ipv6"!==r.kind())throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");return a(this.parts,r.parts,16,t)},r.prototype.SpecialRanges={unspecified:[new r([0,0,0,0,0,0,0,0]),128],linkLocal:[new r([65152,0,0,0,0,0,0,0]),10],multicast:[new r([65280,0,0,0,0,0,0,0]),8],loopback:[new r([0,0,0,0,0,0,0,1]),128],uniqueLocal:[new r([64512,0,0,0,0,0,0,0]),7],ipv4Mapped:[new r([0,0,0,0,0,65535,0,0]),96],rfc6145:[new r([0,0,0,0,65535,0,0,0]),96],rfc6052:[new r([100,65435,0,0,0,0,0,0]),96],"6to4":[new r([8194,0,0,0,0,0,0,0]),16],teredo:[new r([8193,0,0,0,0,0,0,0]),32],reserved:[[new r([8193,3512,0,0,0,0,0,0]),32]]},r.prototype.range=function(){return t.subnetMatch(this,this.SpecialRanges)},r.prototype.isIPv4MappedAddress=function(){return"ipv4Mapped"===this.range()},r.prototype.toIPv4Address=function(){var r,e,n;if(!this.isIPv4MappedAddress())throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");return n=this.parts.slice(-2),r=n[0],e=n[1],new t.IPv4([r>>8,255&r,e>>8,255&e])},r}(),i="(?:[0-9a-f]+::?)+",o={native:new RegExp("^(::)?("+i+")?([0-9a-f]+)?(::)?$","i"),transitional:new RegExp("^((?:"+i+")|(?:::)(?:"+i+")?)"+e+"\\."+e+"\\."+e+"\\."+e+"$","i")},r=function(r,t){var e,n,i,o,a;if(r.indexOf("::")!==r.lastIndexOf("::"))return null;for(e=0,n=-1;(n=r.indexOf(":",n+1))>=0;)e++;if("::"===r.substr(0,2)&&e--,"::"===r.substr(-2,2)&&e--,e>t)return null;for(a=t-e,o=":";a--;)o+="0:";return":"===(r=r.replace("::",o))[0]&&(r=r.slice(1)),":"===r[r.length-1]&&(r=r.slice(0,-1)),function(){var t,e,n,o;for(o=[],t=0,e=(n=r.split(":")).length;t<e;t++)i=n[t],o.push(parseInt(i,16));return o}()},t.IPv6.parser=function(t){var e,n,i,a,s,p;if(t.match(o.native))return r(t,8);if((i=t.match(o.transitional))&&(p=r(i[1].slice(0,-1),6))){for(e=0,n=(s=[parseInt(i[2]),parseInt(i[3]),parseInt(i[4]),parseInt(i[5])]).length;e<n;e++)if(!(0<=(a=s[e])&&a<=255))return null;return p.push(s[0]<<8|s[1]),p.push(s[2]<<8|s[3]),p}return null},t.IPv4.isIPv4=t.IPv6.isIPv6=function(r){return null!==this.parser(r)},t.IPv4.isValid=function(r){try{return new this(this.parser(r)),!0}catch(r){return r,!1}},t.IPv4.isValidFourPartDecimal=function(r){return!(!t.IPv4.isValid(r)||!r.match(/^\d+(\.\d+){3}$/))},t.IPv6.isValid=function(r){if("string"==typeof r&&-1===r.indexOf(":"))return!1;try{return new this(this.parser(r)),!0}catch(r){return r,!1}},t.IPv4.parse=t.IPv6.parse=function(r){var t;if(null===(t=this.parser(r)))throw new Error("ipaddr: string is not formatted like ip address");return new this(t)},t.IPv4.parseCIDR=function(r){var t,e;if((e=r.match(/^(.+)\/(\d+)$/))&&(t=parseInt(e[2]))>=0&&t<=32)return[this.parse(e[1]),t];throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range")},t.IPv4.subnetMaskFromPrefixLength=function(r){var e,n;if(r<0||r>32)throw new Error("ipaddr: invalid prefix length");for(n=Array(4).fill(0),e=0;e<Math.floor(r/8);)n[e]=255,e++;return n[Math.floor(r/8)]=Math.pow(2,r%8)-1<<8-r%8,new t.IPv4(n)},t.IPv4.broadcastAddressFromCIDR=function(r){var e,n,i,o;try{for(n=t.IPv4.parseCIDR(r)[0],o=this.subnetMaskFromPrefixLength([t.IPv4.parseCIDR(r)[1]]),i=[],e=0;e<4;)i.push(parseInt(n.octets[e],10)|255^parseInt(o.octets[e],10)),e++;return new t.IPv4(i)}catch(r){throw r,new Error("ipaddr: the address does not have IPv4 CIDR format")}},t.IPv4.networkAddressFromCIDR=function(r){var e,n,i,o;try{for(n=t.IPv4.parseCIDR(r)[0],o=this.subnetMaskFromPrefixLength([t.IPv4.parseCIDR(r)[1]]),i=[],e=0;e<4;)i.push(parseInt(n.octets[e],10)&parseInt(o.octets[e],10)),e++;return new t.IPv4(i)}catch(r){throw r,new Error("ipaddr: the address does not have IPv4 CIDR format")}},t.IPv6.parseCIDR=function(r){var t,e;if((e=r.match(/^(.+)\/(\d+)$/))&&(t=parseInt(e[2]))>=0&&t<=128)return[this.parse(e[1]),t];throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range")},t.isValid=function(r){return t.IPv6.isValid(r)||t.IPv4.isValid(r)},t.parse=function(r){if(t.IPv6.isValid(r))return t.IPv6.parse(r);if(t.IPv4.isValid(r))return t.IPv4.parse(r);throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format")},t.parseCIDR=function(r){try{return t.IPv6.parseCIDR(r)}catch(e){e;try{return t.IPv4.parseCIDR(r)}catch(r){throw r,new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format")}}},t.fromByteArray=function(r){var e;if(4===(e=r.length))return new t.IPv4(r);if(16===e)return new t.IPv6(r);throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address")},t.process=function(r){var t;return t=this.parse(r),"ipv6"===t.kind()&&t.isIPv4MappedAddress()?t.toIPv4Address():t}}).call(this);