diff --git a/config.js b/config.js index 90b176d..5b49a15 100644 --- a/config.js +++ b/config.js @@ -1,11 +1,10 @@ const env = process.env.APP__ENV_NAME || "local"; const isLocal = env === "local" || env === "test"; -const proxyDomain = process.env.PROXY_DOMAIN || "localhost"; export default { isLocalEnv: isLocal, httpPort: process.env.PORT || 80, - proxyDomain: proxyDomain, // Your domain + proxyDomain: "", // Domain to proxy calls through. Leave it empty to use the requested domain as a proxy domain proxy: { // Proxy configuration is here domains: [ // These domains are replaced in any proxied response (including scripts, URLs and redirects) "adservice.google.com", @@ -48,7 +47,7 @@ export default { "www.googletagmanager.com": [ { regex: /"https\:\/\/s","http:\/\/a","\.adroll\.com/, - replace: `"https://${ proxyDomain }/s","http://${ proxyDomain }/a",".adroll.com` + replace: ({ host }) => `"https://${ host }/s","http://${ host }/a",".adroll.com` } ], "eb2.3lift.com": [ diff --git a/readme.md b/readme.md index 9edbc97..7fbb0ac 100644 --- a/readme.md +++ b/readme.md @@ -60,6 +60,8 @@ Proxied: www.google-analytics.com/collect?v=1&_v=j73&a=531530768&t=pageview&_s=1 Check the [test-static/index.html](test-static/index.html) file's code to see how to bind the proxied analytics to your front end. +Later, you can containerize this repository and route the incoming traffic to `/gtm-proxy` path (for example) through this container in order to avoid analytics blocking. + ## Configuration You can configure which third-parties to proxy/replace and how to do it in the config file. Find the actual configuration in [config.js](config.js) file: diff --git a/src/modules/proxy.js b/src/modules/proxy.js index 338a88b..26d5a9b 100644 --- a/src/modules/proxy.js +++ b/src/modules/proxy.js @@ -15,13 +15,13 @@ const maskRegex = new RegExp( Array.from(maskPaths).join("|").replace(/\//g, "\\/"), "gi" ); -const replaceDomains = (match, pos, str) => { +const replaceDomainsForHost = (host) => (match, pos, str) => { const escapedSlashes = str[pos - 2] === "\\" && str[pos - 2] === "/" const r = `${ escapedSlashes - ? config.proxyDomain.replace(/\//g, "\\/") + "\\" - : config.proxyDomain - }/${ match }` + ? (config.proxyDomain || host).replace(/\//g, "\\/") + "\\" + : (config.proxyDomain || host) + }/${ match }`; return r; }; @@ -37,7 +37,7 @@ export function createDefaultProxy (targetDomain, proxyOptionsOverride = {}) { ? proxyOptionsOverride["proxyReqOptDecorator"](proxyRequest, originalRequest) : proxyRequest; }, - userResHeaderDecorator: (headers) => { + userResHeaderDecorator: (headers, { headers: { host } }) => { if (headers.location) { if (config.proxy.specialContentReplace[servername]) { // Keep before other replacements const replacements = config.proxy.specialContentReplace[servername]; @@ -46,20 +46,20 @@ export function createDefaultProxy (targetDomain, proxyOptionsOverride = {}) { } } headers.location = headers.location - .replace(replaceDomainRegex, replaceDomains) + .replace(replaceDomainRegex, replaceDomainsForHost(host)) .replace(maskRegex, match => mask(match)); } return headers; }, - userResDecorator: (_, proxyResData) => { + userResDecorator: (_, proxyResData, { headers: { host } }) => { if (_.req.res && _.req.res.client && _.req.res.client.servername) { servername = _.req.res.client.servername; } - let pre = proxyResData.toString().replace(replaceDomainRegex, replaceDomains); + let pre = proxyResData.toString().replace(replaceDomainRegex, replaceDomainsForHost(host)); if (config.proxy.specialContentReplace[servername]) { const replacements = config.proxy.specialContentReplace[servername]; for (const r of replacements) { - pre = pre.replace(r.regex, r.replace); + pre = pre.replace(r.regex, r.replace instanceof Function ? r.replace({ host }) : r.replace); } } pre = pre.replace(maskRegex, (match) => { // Mask configured URLs