Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import ip6addr from 'ip6addr';

const DEV_MODE = process.env?.['NODE_ENV'] == 'development';

interface MaintenanceFile {
whitelist: Array<string>;
sites: Record<string, boolean>;
}

export function maintenanceModePlugin(maintenanceFilePath: string): Plugin {
return {
onRequest({ request, fetchAPI, endResponse }) {
Expand All @@ -15,27 +20,36 @@ export function maintenanceModePlugin(maintenanceFilePath: string): Plugin {
return;
}

const maintFile = getMaintenanceFile(maintenanceFilePath);
const requestIp = request.headers.get('x-forwarded-for')?.split(',')[0];
const host = request.headers.get('host') || 'default';

if (requestIp) {
const allowedIpAddresses = readFileSync(maintenanceFilePath, {
encoding: 'utf-8',
}).split(',');
if (inMaintenanceMode(maintFile, host)) {
if (requestIp) {
if (isIpInWhiteList(maintFile.whitelist, requestIp)) {
return;
}
}

if (isIpInWhiteList(allowedIpAddresses, requestIp)) {
return;
}
endResponse(
new fetchAPI.Response('In Maintenance Mode', {
status: 503,
})
);
}

endResponse(
new fetchAPI.Response('In Maintenance Mode', {
status: 503,
})
);
},
};
}

const getMaintenanceFile = (filePath: string): MaintenanceFile => {
const fileContents = readFileSync(filePath, "utf-8");
return JSON.parse(fileContents) as MaintenanceFile;
};

const inMaintenanceMode = (maintFile: MaintenanceFile, host: string): boolean => {
return maintFile.sites[host] === true;
};

function isCIDR(str: string) {
const cidrRegex = /^(([0-9]{1,3}\.){3}[0-9]{1,3}|([a-fA-F0-9:]+))\/\d+$/;
return cidrRegex.test(str);
Expand Down
Loading