Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function ipMatch(ip1: string, ip2: string): boolean {
// check ip1
if (!(ip.isV4Format(ip1) || ip.isV6Format(ip1))) {
throw new Error('invalid argument: ip1 in ipMatch() function is not an IP address.');
}
// check ip2
const cidrParts: string[] = ip2.split('/');
if (cidrParts.length === 2) {
return ip.cidrSubnet(ip2).contains(ip1);
} else {
if (!(ip.isV4Format(ip2) || ip.isV6Format(ip2))) {
console.log(ip2);
throw new Error('invalid argument: ip2 in ipMatch() function is not an IP address.');
}
return ip.isEqual(ip1, ip2);
}
}
try {
addr = maddr.nodeAddress()
} catch (_) {
// Might explode if maddr does not have an IP or cannot be converted
// to a node address. This might happen if it's a relay. We do not print
// or handle the error, otherwise we would get perhaps thousands of logs.
return { isPrivate, isNearby }
}
// At this point, addr.address and publicIP must be valid IP addresses. Hence,
// none of the calls bellow for ip library should fail.
isPrivate = ip.isPrivate(addr.address)
if (publicIP) {
if (ip.isV4Format(addr.address)) {
isNearby = ip.cidrSubnet(`${publicIP}/24`).contains(addr.address)
} else if (ip.isV6Format(addr.address)) {
isNearby = ip.cidrSubnet(`${publicIP}/48`).contains(addr.address) &&
!ip.cidrSubnet('fc00::/8').contains(addr.address)
// peerIP6 ∉ fc00::/8 to fix case of cjdns where IPs are not spatial allocated.
}
}
return { isPrivate, isNearby }
}
function maybeParseCIDR(ipAddr) {
if (typeof ipAddr === 'string') {
return ip.cidrSubnet(ipAddr);
}
return ipAddr;
}
const addCIDR = cidr => {
if (cidr) {
const cidrData = ip.cidrSubnet(cidr);
const ipStrStart = cidrData.firstAddress;
const ipStrEnd = cidrData.lastAddress;
const ipLongStart = ip.toLong(ipStrStart);
const ipLongEnd = ip.toLong(ipStrEnd);
ipRepo.push([ipLongStart, ipLongEnd]);
}
};
async createServer(srv,reinstall = false)
{
if (this.srvExists(srv.code))
return null
let r = await this.openvpn.createServer(srv,reinstall)
if (r)
{
log.error(`Can't create server <${srv.code}>`)
return r
}
srv.intranet = ip.cidrSubnet(srv.network.intranet)
srv.clients = []
this._servers[srv.code] = srv
await this.saveServers()
}
this.app.post('/api/confirmip',(req,res)=>{
this._ip = req.body.ip
let admIntranet = ip.cidrSubnet(conf.servers.adm.network.intranet)
let admIP = admIntranet.firstAddress
res.json({success:true,admIP:admIP}).end()
if (this._installing)
return
this._installing = true
this.iptables.dev(this._ip).then((dev)=>{
this._dev = dev
this.saveIP()
this.loadIP()
this.installRequired().then(()=>{
this._status = 'install1'
this._installing = false
})
})
})
module.exports = function cidrRange(cidr, opts) {
if (typeof cidr !== "string") {
throw new Error("Expected a string");
}
if (!opts) {
opts = {};
}
const range = ip.cidrSubnet(cidr);
const start = ip.toLong(range.networkAddress);
const end = ip.toLong(range.broadcastAddress);
const out = [];
for (let i = start; i <= end; i++) {
out.push(ip.fromLong(i));
}
if (opts.onlyHosts && range.subnetMaskLength <= 30) {
out.splice(0, 1);
out.splice(out.length - 1, 1);
}
return out;
};
if (netname == undefined || netname == "") {
response.message = "VNET creation: Please specify a network name!";
response.result = "ERROR";
logger.error("[VNET] --> " + response.message);
res.status(500).send(response);
} else if (value == undefined || value == "") {
response.message = "VNET creation: Please specify a network address!";
response.result = "ERROR";
logger.error("[VNET] --> " + response.message);
res.status(500).send(response);
} else {
try {
var network = ip.cidrSubnet(value);
logger.debug("[VNET] --> VNET configuration:\n" + JSON.stringify(network, null, "\t"));
var first = ip.toLong(network.networkAddress);
var last = ip.toLong(network.broadcastAddress);
var overlap = false;
var vlan_name = netname;
var vlan_ip = network.networkAddress;
var vlan_mask = network.subnetMaskLength;
}catch (err){
response.message = "Error elaborate address: " + err;
response.result = "ERROR";
logger.error("[VNET] --> " + response.message);
res.status(500).send(response);