isInNet.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const netmask_1 = require("netmask");
  4. const util_1 = require("./util");
  5. /**
  6. * True iff the IP address of the host matches the specified IP address pattern.
  7. *
  8. * Pattern and mask specification is done the same way as for SOCKS configuration.
  9. *
  10. * Examples:
  11. *
  12. * ``` js
  13. * isInNet(host, "198.95.249.79", "255.255.255.255")
  14. * // is true iff the IP address of host matches exactly 198.95.249.79.
  15. *
  16. * isInNet(host, "198.95.0.0", "255.255.0.0")
  17. * // is true iff the IP address of the host matches 198.95.*.*.
  18. * ```
  19. *
  20. * @param {String} host a DNS hostname, or IP address. If a hostname is passed,
  21. * it will be resoved into an IP address by this function.
  22. * @param {String} pattern an IP address pattern in the dot-separated format mask.
  23. * @param {String} mask for the IP address pattern informing which parts of the
  24. * IP address should be matched against. 0 means ignore, 255 means match.
  25. * @return {Boolean}
  26. */
  27. async function isInNet(host, pattern, mask) {
  28. const family = 4;
  29. try {
  30. const ip = await (0, util_1.dnsLookup)(host, { family });
  31. if (typeof ip === 'string') {
  32. const netmask = new netmask_1.Netmask(pattern, mask);
  33. return netmask.contains(ip);
  34. }
  35. }
  36. catch (err) {
  37. // ignore
  38. }
  39. return false;
  40. }
  41. exports.default = isInNet;
  42. //# sourceMappingURL=isInNet.js.map