How to use the isPublic function from ip

Find comprehensive JavaScript ip.isPublic code examples handpicked from public code repositorys.

The ip.isPublic method checks if an IP address is public or not.

1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
traceroute.trace(value.checkHost || "8.8.8.8", (err, hops, destination) => {
  if (err) {
    this.simpleTxData(msg, {}, err, callback);
  } else {
    let secondStepIp = hops[1] ? hops[1].ip : "";
    let isPublic = iptool.isPublic(secondStepIp);
    this.simpleTxData(msg, { hops: hops, secondStepIp: secondStepIp, isPublic: isPublic, destination: destination }, null, callback);
  }
})
break;
fork icon117
star icon463
watch icon0

+ 3 other calls in file

666
667
668
669
670
671
672
673
674
675
}

// filter Carrier-Grade NAT address pool accordinig to rfc6598
filterPublicIp4(ipArray) {
  const rfc6598Net = iptool.subnet("100.64.0.0", "255.192.0.0")
  return ipArray.filter(ip => iptool.isPublic(ip) && !rfc6598Net.contains(ip));
}

filterPublicIp6(ip6Array) {
  return ip6Array.filter(ip => iptool.isPublic(ip));
fork icon117
star icon463
watch icon0

+ 7 other calls in file

How does ip.isPublic work?

ip.isPublic() is a function in the ip module of Node.js that checks whether a given IP address is public or not by comparing it with a list of private IP address ranges defined by RFC 1918 and RFC 4193. If the IP address is not within any of the private ranges, it is considered public.

168
169
170
171
172
173
174
175
176
177
  if (_.isArray(i.ip6_subnets)) {
    const subnets6 = i.ip6_subnets.map(s => {
      const addr = new Address6(s);
      return `${addr.startAddress().correctForm()}/${addr.subnetMask}`;
    });
    hostSubnets6 = hostSubnets6.concat(subnets6.filter(ip6 => iptool.isPublic(ip6)));
  }
}
if (service.hasOwnProperty("environment") && (_.isObject(service["environment"]) || _.isArray(service["environment"]))) {
  const env = service["environment"];
fork icon117
star icon463
watch icon0

95
96
97
98
99
100
101
102
103
104
105
 */
module.exports.isValidContact = function(contact, loopback) {
  const [, info] = contact;
  const isValidAddr = ip.isV4Format(info.hostname) ||
                      ip.isV6Format(info.hostname) ||
                      ip.isPublic(info.hostname);
  const isValidPort = info.port > 0;
  const isAllowedAddr = ip.isLoopback(info.hostname) ? !!loopback : true;


  return isValidPort && isValidAddr && isAllowedAddr;
fork icon20
star icon1
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
const ip = require("ip");

const myIP = "192.168.1.100";
const isPublic = ip.isPublic(myIP);

console.log(isPublic); // false

This code will check if the IP address 192.168.1.100 is a public IP address, and it will log false to the console since 192.168.1.100 is a private IP address.

20
21
22
23
24
25
26
27
28
29
// to prevent bypassing private ip limitation
const parsed2 = new UrlParse(url)
const host = parsed.hostname.replace(/\[|\]/g, '')
const host2 = parsed2.hostname.replace(/\[|\]/g, '')
// dont allow domain to prevent dns rebinding XD
if (isIp(host2) && IP.isPublic(host) && IP.isPublic(host2)) {
  return config
} else {
  throw new Error('No SSRF')
}
fork icon2
star icon4
watch icon0

18
19
20
21
22
23
24
25
26
27
28
29


//
// ### function isPublic (addr)
// Determines if an IP address is public.
//
exports.isPublic = ip.isPublic;


//
// ### function serverPass (server)
// #### @server {Object} Server to extract the serverPass from.
fork icon0
star icon0
watch icon0

70
71
72
73
74
75
76
77
78
79
      if (details.family !== family || ip.isLoopback(details.address)) {
        return false;
      } else if (!name) {
        return true;
      }
      return name === 'public' ? ip.isPublic(details.address) : ip.isPrivate(details.address);
    });
    return addresses.length ? addresses : undefined;
  }).filter(Boolean);
}
fork icon0
star icon0
watch icon0

59
60
61
62
63
64
65
66
67
68
} = p2pOptions

const nodeIdPath = path.join(peersDbDir, 'nodeId')
const identity = priv.getNodeIdentity(nodeIdPath)
const pubIp = (publicIp || '').trim().toLowerCase()
const isPublicIp = !!pubIp && ip.isPublic(pubIp) && pubIp !== '0.0.0.0' && pubIp !== 'localhost'
const p2pNode = new P2PNode({
  config: {
    seeds: seedList.map(n => priv.getAddress(n.ip, n.port, n.id)),
    publicIp: isPublicIp ? pubIp : '',
fork icon0
star icon0
watch icon0

+ 2 other calls in file

188
189
190
191
192
193
194
195
196
197
  return false;
}

var isValidAddr = ip.isV4Format(contact.address) ||
                  ip.isV6Format(contact.address) ||
                  ip.isPublic(contact.address);
var isValidPort = contact.port > 0;
var isAllowedAddr = ip.isLoopback(contact.address) ? !!loopback : true;

return isValidPort && isValidAddr && isAllowedAddr;
fork icon0
star icon0
watch icon0

89
90
91
92
93
94
95
96
97
98
  });
}

if (self._doNotTraverseNat) {
  self._isPublic = true; // used to determine tunneling
  self._publicIp = ip.isPublic(self._contact.address); // used for UI status
  self._checkIfReachable(function(reachable) {
    self._portOpen = reachable;
  });
  self._requiresTraversal = false;
fork icon0
star icon0
watch icon0