nginx-cf-realip
Version:
Command line utility for updating NGINX real-ip rules for CloudFlare's IPs
65 lines (64 loc) • 2.21 kB
JavaScript
;
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 axios_1 = __importDefault(require("axios"));
class CloudFlareIps {
constructor() {
/**
* Axios instance for CloudFlare
*/
this.cloudflare = axios_1.default.create({
baseURL: 'https://cloudflare.com',
});
}
/**
* Fetch a list of IPs for the given IP version.
*
* @param version
*/
getList(version) {
return __awaiter(this, void 0, void 0, function* () {
const request = yield this.cloudflare.get(`/ips-v${version}`);
return request.data.trim();
});
}
/**
* Convert a list separated by linebreaks to an array.
*
* @param list
*/
listToArray(list) {
return list.split(/\n/);
}
/**
* Fetch an array of IPs for the given IP version.
*
* @param version
*/
getArray(version) {
return __awaiter(this, void 0, void 0, function* () {
return this.listToArray(yield this.getList(version));
});
}
/**
* All available CloudFlare IPs.
*/
all() {
return __awaiter(this, void 0, void 0, function* () {
const v4 = yield this.getArray(4);
const v6 = yield this.getArray(6);
return [...v4, ...v6];
});
}
}
exports.default = CloudFlareIps;