UNPKG

nginx-cf-realip

Version:

Command line utility for updating NGINX real-ip rules for CloudFlare's IPs

55 lines (54 loc) 3.98 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const CloudFlareIps_1 = __importDefault(require("./CloudFlareIps")); const expect_1 = __importDefault(require("expect")); const rangeCheck = require('range_check'); describe('cloudflare ips', () => { const ips = new CloudFlareIps_1.default(); it('can fetch ipv4 plaintext lists', () => __awaiter(this, void 0, void 0, function* () { const ipv4List = yield ips.getList(4); expect_1.default(ipv4List.length).toBeGreaterThan(0); })); it('can fetch ipv6 plaintext lists', () => __awaiter(this, void 0, void 0, function* () { const ipv6List = yield ips.getList(6); expect_1.default(ipv6List.length).toBeGreaterThan(0); })); it('can fetch ipv4 arrays', () => __awaiter(this, void 0, void 0, function* () { const ipv4Array = yield ips.getArray(4); expect_1.default(ipv4Array.length).toBeGreaterThan(0); })); it('can fetch ipv6 arrays', () => __awaiter(this, void 0, void 0, function* () { const ipv6Array = yield ips.getArray(6); expect_1.default(ipv6Array.length).toBeGreaterThan(0); })); it('fetches valid ipv4 ranges', () => __awaiter(this, void 0, void 0, function* () { const ipv4Array = yield ips.getArray(4); ipv4Array.forEach((ip) => { expect_1.default(ip.match(/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/([0-9]|[1-6][0-9])$/)).toBeTruthy(); expect_1.default(rangeCheck.isRange(ip)).toBeTruthy(); }); })); it('fetches valid ipv6 ranges', () => __awaiter(this, void 0, void 0, function* () { const ipv6Array = yield ips.getArray(6); ipv6Array.forEach((ip) => { expect_1.default(ip.match(/^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/i)).toBeTruthy(); expect_1.default(rangeCheck.isRange(ip)).toBeTruthy(); }); })); it('all fetched ip ranges are valid', () => __awaiter(this, void 0, void 0, function* () { (yield ips.all()).forEach((ip) => { expect_1.default(rangeCheck.isRange(ip)).toBe(true); }); })); });