outers
Version:
outers - a all in one package for your day to day use
110 lines • 5.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Middleware_Constant_1 = require("../../Config/Constant/Middleware.Constant"); // Import X-Powered-By Header
// Import Server Module
const outer_1 = require("../../Config/outer"); // Import Status Code
// Main Function
/**
* Middleware function to restrict access based on IP address or URL.
*
* @param AllowedIP - An array of allowed IP addresses. must be in string format
* @param StatusCode - Optional. The status code to be sent in the response if access is denied. Defaults to 406 (Not Acceptable).
* @param ErrorMessage - Optional. The error message to be sent in the response if access is denied. Defaults to "You are not allowed to access this server from this IP/URL."
* @param Reverse - Optional. If true, the middleware will allow access only if the requester IP/URL is not in the allowed list. Defaults to false.
*
* @returns The middleware function.
*/
function default_1(AllowedIP, StatusCode, ErrorMessage, Reverse) {
return (Request, Response, Next) => {
// Change Response X-Powered-By Header & Server Header
Response.setHeader("X-Powered-By", (0, Middleware_Constant_1.XPoweredBy)()); // Set X-Powered-By Header
Response.setHeader("Server", (0, Middleware_Constant_1.ServerName)()); // Set Server Header
// Check if Request has Headers
if (!Request.headers) {
return outer_1.Serve.JSON({
response: Response,
status: false,
statusCode: outer_1.StatusCodes.BAD_REQUEST,
Title: "Bad Request",
message: "No headers provided",
data: null,
cookieData: undefined,
contentType: "application/json",
});
}
const ReverseParams = Reverse !== null && Reverse !== void 0 ? Reverse : false; // Set Reverse to false if it is undefined
const RequesterIPaddress = String(Request.headers["x-forwarded-for"] ||
Request.connection.remoteAddress ||
Request.socket.remoteAddress ||
Request.socket.remoteAddress ||
Request.headers["x-real-ip"] ||
Request.ip); // Get Requester IP Address
let isAllowed = false; // Set isAllowed to false
try {
// Check if Request Hostname is available in Array or not
isAllowed = AllowedIP.some((IP) => {
const IPRegex = new RegExp(IP, "i"); // Create a Regular Expression for IP Address to match
return IP == "*"
? true
: IP.includes("127.0.0")
? true
: IPRegex.test(RequesterIPaddress); // Check if Requester IP is Allowed or not
}); // Check if Requester IP is Allowed or not
if (ReverseParams === false) {
if (isAllowed === true) {
Next(); // Next Middleware
}
else {
// Handle the case when no match is found
outer_1.Serve.JSON({
response: Response,
status: false,
Title: "IP Not Allowed to Access",
statusCode: StatusCode !== null && StatusCode !== void 0 ? StatusCode : outer_1.StatusCodes.NOT_ACCEPTABLE,
message: ErrorMessage !== null && ErrorMessage !== void 0 ? ErrorMessage : "You are not allowed to access this server from this IP.",
data: {
ClientIP: RequesterIPaddress,
ClientURL: `${Request.protocol}://${Request.headers.host}${Request.url}`,
},
cookieData: undefined,
contentType: "application/json",
}); // Serve JSON
// You may choose to send an error response or redirect the user to an error page
}
}
else {
if (isAllowed === false) {
Next(); // Next Middleware
}
else {
outer_1.Serve.JSON({
response: Response,
status: false,
Title: "URL Not Allowed to Access",
statusCode: StatusCode !== null && StatusCode !== void 0 ? StatusCode : outer_1.StatusCodes.NOT_ACCEPTABLE,
message: ErrorMessage !== null && ErrorMessage !== void 0 ? ErrorMessage : "You are not allowed to access this server from this URL.",
data: {
ClientIP: RequesterIPaddress,
ClientURL: `${Request.protocol}://${Request.headers.host}${Request.url}`,
},
cookieData: undefined,
contentType: "application/json",
}); // Serve JSON
}
}
}
catch (error) {
outer_1.Serve.JSON({
response: Response,
status: false,
statusCode: outer_1.StatusCodes.EXPECTATION_FAILED,
Title: "Failed To Proceed",
data: error,
contentType: "application/json",
message: "Unable to Proceed your Request further, there is some error in configuration in Server",
}); // Send Error Response
}
};
}
exports.default = default_1;
//# sourceMappingURL=Base.middleware.js.map