freshroute-server
Version:
Local development server for FreshRoute extension with API mocking and extended functionality
220 lines (219 loc) • 6.88 kB
JavaScript
;
(function() {
var globalScope = function() {
if (typeof self !== "undefined") return self;
if (typeof window !== "undefined") return window;
if (typeof global !== "undefined") return global;
return {};
}();
if (!globalScope.ALLOWED_DOMAINS) {
globalScope.ALLOWED_DOMAINS = [
// Local development
"localhost",
"127.0.0.1",
// Development environments (add your dev domains here)
"localhost.freshservice-dev.com",
// Fresh development environments
"freshgladiators.com",
"freshinfinitysquad.com",
"freshavtar.com",
"freshakatsuki.com",
"freshzerosquad.com",
"freshservice.com",
"frescarbons.com",
"freshapratim.com",
"freshblr.com",
"freshcodered.com",
"freshcout.com",
"freshcsk.com",
"freshembers.com",
"freshgroot.com",
"freshhydra.com",
"freshlegion.com",
"freshlocale.com",
"freshmrr.com",
"freshopsadmin.com",
"freshshields.com",
"freshstatusok.com",
"freshsuperkings.com",
"freshtransformers.com",
"freshttl.com",
"freshbase.com",
"freshbnbs.com",
"freshcamp.com",
"freshchart.com",
"freshcommit.com",
"freshcore.com",
"freshitsm.com",
"freshlink.com",
"freshmdm.com",
"freshops.com",
"freshregression.com",
"freshvvs.com",
"freshworld.com",
"freshadapt.com",
"freshalchemist.com",
"freshamigo.com",
"freshasgard.com",
"freshavengers.com",
"freshbilliondollars.com",
"freshblackfyre.com",
"freshbloodysweet.com",
"freshd42.com",
"freshesm.com",
"freshexeg.com",
"freshfightclub.com",
"freshikigai.com",
" freshmim.com",
"freshmkp.com",
"freshrobust.com",
"freshsherlock.com",
"freshstarks.com",
"freshsupernova.com",
"freshtd.com",
"freshtechtitans.com",
"freshtva.com",
"freshbelievers.com",
"freshcaptivators.com",
"fresheternals.com",
"freshexplorerssquad.com",
"freshgenome.com",
"freshincredibles.com",
"freshinfinity.com",
"freshphotonssquad.com",
"freshquarks.com",
"freshreignite.com",
"freshscooby.com",
"freshspartans.com",
"freshsunfyre.com",
"freshsyndicates.com",
"freshteamvermithor.com",
"freshtechstack.com",
"freshthundersquad.com",
"freshtitans.com",
"freshwhiteknights.com",
"freshzerosquad.com"
// Add your allowed development/staging domains here
// Examples:
// 'dev.mycompany.com',
// 'staging.mycompany.com',
// 'test.mycompany.com',
// 'qa.mycompany.com',
// 'demo.mycompany.com'
];
}
if (!globalScope.validateTargetUrl) {
globalScope.validateTargetUrl = function(targetUrl) {
if (!targetUrl || typeof targetUrl !== "string") {
return {
allowed: false,
reason: "Target URL is required and must be a valid string"
};
}
try {
const urlForValidation = targetUrl.replace(/\$\d+|\\\d+/g, "PLACEHOLDER");
let parsedUrl;
try {
parsedUrl = new URL(urlForValidation);
} catch (e) {
const domainMatch = urlForValidation.match(/^https?:\/\/([^\/\?#]+)/);
if (!domainMatch) {
return {
allowed: false,
reason: "Invalid URL format. Please provide a valid HTTP/HTTPS URL.",
suggestions: [
"Use format: https://dev.example.com/path",
"Ensure protocol (http:// or https://) is included"
]
};
}
try {
parsedUrl = new URL(`https://${domainMatch[1]}`);
} catch (e2) {
return {
allowed: false,
reason: "Could not parse domain from target URL",
suggestions: ["Check URL format and remove invalid characters"]
};
}
}
const hostname = parsedUrl.hostname.toLowerCase();
for (const allowedDomain of globalScope.ALLOWED_DOMAINS) {
const normalizedAllowedDomain = allowedDomain.toLowerCase();
if (hostname === normalizedAllowedDomain) {
return { allowed: true };
}
if (hostname.endsWith("." + normalizedAllowedDomain)) {
return { allowed: true };
}
}
return {
allowed: false,
reason: `Domain "${hostname}" is not in the allowed domains list`,
suggestions: [
"Use one of the allowed domains:",
...globalScope.ALLOWED_DOMAINS.map((domain) => `• ${domain}`),
"",
"Contact the plugin development team to add new development domains"
]
};
} catch (error) {
return {
allowed: false,
reason: `Error validating URL: ${error.message}`,
suggestions: ["Check URL format and try again"]
};
}
};
}
if (!globalScope.getAllowedDomains) {
globalScope.getAllowedDomains = function() {
return [...globalScope.ALLOWED_DOMAINS];
};
}
if (!globalScope.validateNewDomain) {
globalScope.validateNewDomain = function(domain) {
if (!domain || typeof domain !== "string") {
return {
valid: false,
reason: "Domain must be a valid string"
};
}
const cleanDomain = domain.toLowerCase().trim();
if (!/^[a-z0-9.-]+$/.test(cleanDomain)) {
return {
valid: false,
reason: "Domain contains invalid characters"
};
}
if (globalScope.ALLOWED_DOMAINS.some((d) => d.toLowerCase() === cleanDomain)) {
return {
valid: false,
reason: "Domain already exists in allowed list"
};
}
return { valid: true };
};
}
if (typeof module !== "undefined" && module.exports) {
module.exports = {
validateTargetUrl: globalScope.validateTargetUrl,
getAllowedDomains: globalScope.getAllowedDomains,
validateNewDomain: globalScope.validateNewDomain,
ALLOWED_DOMAINS: globalScope.ALLOWED_DOMAINS
};
} else if (typeof window !== "undefined") {
window.DomainValidator = {
validateTargetUrl: globalScope.validateTargetUrl,
getAllowedDomains: globalScope.getAllowedDomains,
validateNewDomain: globalScope.validateNewDomain,
ALLOWED_DOMAINS: globalScope.ALLOWED_DOMAINS
};
}
if (typeof self !== "undefined") {
self.ALLOWED_DOMAINS = globalScope.ALLOWED_DOMAINS;
self.validateTargetUrl = globalScope.validateTargetUrl;
self.getAllowedDomains = globalScope.getAllowedDomains;
self.validateNewDomain = globalScope.validateNewDomain;
}
})();