Skip to content

Commit

Permalink
Allow setting proxyDomain to auto
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaeverywhere committed May 10, 2019
1 parent 2417309 commit fda75ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
5 changes: 2 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions src/modules/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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];
Expand All @@ -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
Expand Down

0 comments on commit fda75ec

Please sign in to comment.