Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
b.writeUInt32BE(eip[i], offset)
offset += 4
}
b.writeUInt32BE(locId >>> 0, 32)
lat = Math.round(parseFloat(fields[7]) * 10000)
lon = Math.round(parseFloat(fields[8]) * 10000)
area = parseInt(fields[9], 10)
b.writeInt32BE(lat, 36)
b.writeInt32BE(lon, 40)
b.writeInt32BE(area, 44)
} else {
// IPv4
bsz = 24
rngip = new Address4(fields[0])
sip = parseInt(rngip.startAddress().bigInteger(), 10)
eip = parseInt(rngip.endAddress().bigInteger(), 10)
locId = parseInt(fields[1], 10)
locId = cityLookup[locId]
b = Buffer.alloc(bsz)
b.fill(0)
b.writeUInt32BE(sip >>> 0, 0)
b.writeUInt32BE(eip >>> 0, 4)
b.writeUInt32BE(locId >>> 0, 8)
lat = Math.round(parseFloat(fields[7]) * 10000)
lon = Math.round(parseFloat(fields[8]) * 10000)
area = parseInt(fields[9], 10)
b.writeInt32BE(lat, 12)
b.writeInt32BE(lon, 16)
b.writeInt32BE(area, 20)
b.writeUInt32BE(eip[i], offset);
offset += 4;
}
b.writeUInt32BE(locId>>>0, 32);
lat = Math.round(parseFloat(fields[7]) * 10000);
lon = Math.round(parseFloat(fields[8]) * 10000);
area = parseInt(fields[9], 10);
b.writeInt32BE(lat,36);
b.writeInt32BE(lon,40);
b.writeInt32BE(area,44);
} else {
// IPv4
bsz = 24;
rngip = new Address4(fields[0]);
sip = parseInt(rngip.startAddress().bigInteger(),10);
eip = parseInt(rngip.endAddress().bigInteger(),10);
locId = parseInt(fields[1], 10);
locId = cityLookup[locId];
b = Buffer.alloc(bsz);
b.fill(0);
b.writeUInt32BE(sip>>>0, 0);
b.writeUInt32BE(eip>>>0, 4);
b.writeUInt32BE(locId>>>0, 8);
lat = Math.round(parseFloat(fields[7]) * 10000);
lon = Math.round(parseFloat(fields[8]) * 10000);
area = parseInt(fields[9], 10);
b.writeInt32BE(lat,12);
b.writeInt32BE(lon,16);
b.writeInt32BE(area,20);
sip = utils.aton6(rngip.startAddress().correctForm())
eip = utils.aton6(rngip.endAddress().correctForm())
b = Buffer.alloc(bsz)
for (i = 0; i < sip.length; i++) {
b.writeUInt32BE(sip[i], i * 4)
}
for (i = 0; i < eip.length; i++) {
b.writeUInt32BE(eip[i], 16 + i * 4)
}
} else {
// IPv4
bsz = 10
rngip = new Address4(fields[0])
sip = parseInt(rngip.startAddress().bigInteger(), 10)
eip = parseInt(rngip.endAddress().bigInteger(), 10)
b = Buffer.alloc(bsz)
b.fill(0)
b.writeUInt32BE(sip, 0)
b.writeUInt32BE(eip, 4)
}
b.write(cc, bsz - 2)
fs.writeSync(datFile, b, 0, bsz, null)
if (Date.now() - tstart > 5000) {
tstart = Date.now()
process.stdout.write('\nStill working (' + lines + ') ...')
}
.map(function(line) {
var data = line.split(',');
if (!data || data.length < 2) {
console.error('[ERROR]', 'Bad line:', line);
return;
}
var geonameId = parseInt(data[1].replace(/"/g, ''));
var ipv4Cidr = data[0].replace(/"/g, '');
var ipv4Address = new Address4(ipv4Cidr);
var ipv4StartAddress = ipv4Address.startAddress().correctForm();
var ipv4EndAddress = ipv4Address.endAddress().correctForm();
var block = {
geoname_id: geonameId,
ipv4_start_int: utils.ipv4ToInt(ipv4StartAddress)
};
if (ipv4StartAddress !== ipv4EndAddress) {
block.ipv4_end_int = utils.ipv4ToInt(ipv4EndAddress);
}
countryBlocksIpv4.push(block);
})
.on('pipe', function() {
exports.ipToASNs = function(ip) {
if (net.isIP(ip)) {
const ipv4 = new ipAddress.Address4(ip);
if (ipv4.isValid()) {
return lookup(ipv4.toArray().reverse().join(".") + ".origin.asn.cymru.com");
}
const ipv6 = new ipAddress.Address6(ip);
if (ipv6.isValid()) {
return lookup(ipv6.reverseForm({ omitSuffix: true }) + ".origin6.asn.cymru.com");
}
}
return Promise.resolve([]);
};
try {
seedURL = new URL(seed)
toURL = new URL(to)
} catch (err) {
log.debug({ err, seedURL, toURL }, `Rejecting due to invalid URL`)
return false
}
if (!toURL.hostname) {
log.debug(`Rejecting ${toURL} as invalid`)
return false
}
if (seedURL.hostname === toURL.hostname) {
return true
}
if (new Address4(toURL.hostname).isValid()) {
log.debug(`Rejecting ${toURL} as it's an IP address`)
return false
}
const parsedSeed = parseDomain(seedURL.hostname)
const parsedTo = parseDomain(toURL.hostname)
if (
!parsedSeed ||
!parsedTo ||
parsedSeed.domain !== parsedTo.domain ||
parsedSeed.tld !== parsedTo.tld
) {
log.debug(`Rejecting ${to} from a different domain to seed ${seed}`)
return false
}
return true
}
const addrs = groupBy(multiaddrs, ma => {
const addr = new Address4(ma.toOptions().host)
if (addr.isInSubnet(new Address4('127.0.0.1/8'))) return 'loopback'
if (
addr.isInSubnet(new Address4('10.0.0.0/8')) ||
addr.isInSubnet(new Address4('172.16.0.0/12')) ||
addr.isInSubnet(new Address4('192.168.0.0/16'))
)
return 'local'
if (addr.isInSubnet(new Address4('169.254.0.0/16'))) return 'link-local'
return 'global'
})
function cidrSubnetV4(ipRange, newBits, netNum) {
const ipAddress = require("ip-address");
const BigInteger = require("jsbn").BigInteger;
const ipv4 = new ipAddress.Address4(ipRange);
if (!ipv4.isValid()) {
throw new Error(`Invalid IP address range: ${ipRange}`);
}
const newSubnetMask = ipv4.subnetMask + newBits;
if (newSubnetMask > 32) {
throw new Error(`Requested ${newBits} new bits, but ` +
`only ${32 - ipv4.subnetMask} are available.`);
}
const addressBI = ipv4.bigInteger();
const newAddressBase = Math.pow(2, 32 - newSubnetMask);
const netNumBI = new BigInteger(netNum.toString());
const newAddressBI = addressBI.add(new BigInteger(newAddressBase.toString()).multiply(netNumBI));
const newAddress = ipAddress.Address4.fromBigInteger(newAddressBI).address;
return `${newAddress}/${newSubnetMask}`;
}
/**
function cidrSubnetV4(ipRange: string, newBits: number, netNum: number): string {
const ipAddress = require("ip-address");
const BigInteger = require("jsbn").BigInteger;
const ipv4 = new ipAddress.Address4(ipRange);
if (!ipv4.isValid()) {
throw new Error(`Invalid IP address range: ${ipRange}`);
}
const newSubnetMask = ipv4.subnetMask + newBits;
if (newSubnetMask > 32) {
throw new Error(`Requested ${newBits} new bits, but ` +
`only ${32 - ipv4.subnetMask} are available.`);
}
const addressBI = ipv4.bigInteger();
const newAddressBase = Math.pow(2, 32 - newSubnetMask);
const netNumBI = new BigInteger(netNum.toString());
const newAddressBI = addressBI.add(new BigInteger(newAddressBase.toString()).multiply(netNumBI));
const newAddress = ipAddress.Address4.fromBigInteger(newAddressBI).address;
function subnetV4(ipRange, newBits, netNum) {
const ipAddress = require("ip-address");
const BigInteger = require("jsbn").BigInteger;
const ipv4 = new ipAddress.Address4(ipRange);
if (!ipv4.isValid()) {
throw new Error(`Invalid IP address range: ${ipRange}`);
}
const newSubnetMask = ipv4.subnetMask + newBits;
if (newSubnetMask > 32) {
throw new Error(`Requested ${newBits} new bits, but ` +
`only ${32 - ipv4.subnetMask} are available.`);
}
const addressBI = ipv4.bigInteger();
const newAddressBase = Math.pow(2, 32 - newSubnetMask);
const netNumBI = new BigInteger(netNum.toString());
const newAddressBI = addressBI.add(new BigInteger(newAddressBase.toString()).multiply(netNumBI));
const newAddress = ipAddress.Address4.fromBigInteger(newAddressBI).address;
return `${newAddress}/${newSubnetMask}`;
}
exports.subnetV4 = subnetV4;