If you’re not familiar with a Proxy Auto-Configuration (PAC) file, it’s a JavaScript function that determines whether web requests should be forwarded to a proxy server or not.

There’s a minimal set of JavaScript functions defined that you can use to conditionally send your web traffic through a proxy. One of those functions, isInNet(), returns true if an address (an IPv4 address) is included in a given network range. Unfortunately there are no functions in this standard set that provide similar support for IPv6 addresses.

There are many IPv6 parsing libraries on GitHub, but all of them depend on at least a few npm packages. I’m normally not one to complain about npm dependencies, but this is once instance where the dependency model is not really tenable.

Here I’ve put together a copy/pastable inIPv6Range() function that provides limited functionality for determining whether a given IPv6 address is in a predetermined range.

function expandIPv6( ipv6 ) { const parts = ipv6 .replace( '::', ':' ) .split( ':' ) .filter( p => !! p ); const zerofill = 8 - parts.length; // Fill in :: with missing zeros return ipv6 .replace( '::', `:${ '0:'.repeat( zerofill ) }` ) .replace( /:$/, '' ); } function parseIPv6( ipv6 ) { ipv6 = expandIPv6( ipv6 ); // Check is valid IPv6 address ipv6 = ipv6.split( ':' ); if ( ipv6.length !== 8 ) { return false; } const parts = []; ipv6.forEach( function( part ) { let bin = parseInt( part, 16 ).toString( 2 ); while ( bin.length < 16 ) { // left pad bin = '0' + bin; } parts.push( bin ); }); const bin = parts.join( '' ); return parseInt( bin, 2 ); } function inIPv6Range( ipv6, low = '::', high = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ) { ipv6 = parseIPv6( ipv6 ); low = parseIPv6( low ); high = parseIPv6( high ); if ( false === ipv6 || false === low || false === high ) { return false; } return ipv6 >= low && ipv6 <= high; }

If you clean this up or add CIDR support, let me know!