How to use the ip.cidrSubnet function in ip

To help you get started, we’ve selected a few ip examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github casbin / node-casbin / src / util / builtinOperators.ts View on Github external
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);
  }
}
github ipfs-shipyard / ipfs-webui / src / bundles / peer-locations.js View on Github external
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 }
}
github uber-common / paranoid-request / src / addr_validator.js View on Github external
function maybeParseCIDR(ipAddr) {
  if (typeof ipAddr === 'string') {
    return ip.cidrSubnet(ipAddr);
  }

  return ipAddr;
}
github wspl / CIDR2PAC / index.js View on Github external
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]);
  }
};
github abrakadobr / vpnface_lite / core.js View on Github external
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()
  }
github abrakadobr / vpnface_lite / core.js View on Github external
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
        })
      })
    })
github silverwind / cidr-range / cidr-range.js View on Github external
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;
};
github MDSLab / s4t-iotronic-standalone / lib / modules / vnet_iotronic_manager.js View on Github external
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);