isInNet.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. /**
  3. * Module dependencies.
  4. */
  5. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const netmask_1 = require("netmask");
  16. const util_1 = require("./util");
  17. /**
  18. * True iff the IP address of the host matches the specified IP address pattern.
  19. *
  20. * Pattern and mask specification is done the same way as for SOCKS configuration.
  21. *
  22. * Examples:
  23. *
  24. * ``` js
  25. * isInNet(host, "198.95.249.79", "255.255.255.255")
  26. * // is true iff the IP address of host matches exactly 198.95.249.79.
  27. *
  28. * isInNet(host, "198.95.0.0", "255.255.0.0")
  29. * // is true iff the IP address of the host matches 198.95.*.*.
  30. * ```
  31. *
  32. * @param {String} host a DNS hostname, or IP address. If a hostname is passed,
  33. * it will be resoved into an IP address by this function.
  34. * @param {String} pattern an IP address pattern in the dot-separated format mask.
  35. * @param {String} mask for the IP address pattern informing which parts of the
  36. * IP address should be matched against. 0 means ignore, 255 means match.
  37. * @return {Boolean}
  38. */
  39. function isInNet(host, pattern, mask) {
  40. return __awaiter(this, void 0, void 0, function* () {
  41. const family = 4;
  42. try {
  43. const ip = yield (0, util_1.dnsLookup)(host, { family });
  44. if (typeof ip === 'string') {
  45. const netmask = new netmask_1.Netmask(pattern, mask);
  46. return netmask.contains(ip);
  47. }
  48. }
  49. catch (err) { }
  50. return false;
  51. });
  52. }
  53. exports.default = isInNet;
  54. //# sourceMappingURL=isInNet.js.map