forked from airtower-luna/referer-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
176 lines (167 loc) · 4.14 KB
/
engine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Referer Modifier: Modify the Referer header in HTTP requests
* Copyright (C) 2017-2021 Fiona Klute
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
/* exported RefererModEngine */
class RefererModEngine
{
constructor(config)
{
if (!config)
{
config = {
/* Default empty domain configuration */
domains: [],
/* Default configuration for same domain requests */
sameConf: { action: "keep", referer: "" },
/* Default configuration for any other requests */
anyConf: { action: "prune", referer: "" }
};
}
this.setConfig(config);
}
setConfig(config)
{
/*
* Configuration for specific domains
*
* Elements are expected to have this structure in the stored
* configuration:
*
* { domain: "www.example.com", action: "keep", referer: "" }
*
* While loading the configuration, a "regexp" field will be added that
* contains the regular expression compiled form the "domain" field to
* include subdomains.
*/
this._domains = RefererModEngine.createDomainRegex(config.domains);
/* Configuration for same domain requests */
this._sameConf = config.sameConf;
/* Configuration for any other requests */
this._anyConf = config.anyConf;
}
/*
* Look up the action to take on any referer based on target URL (url)
* and URL of the request origin (originURL).
*/
findHostConf(url, originUrl)
{
let target = new URL(url);
/* Check if we have a specific configuration for the target
* domain, if yes return it. The reduce step chooses the longest
* (most precise) match in case we have multiple filter
* matches. */
let match = this._domains
.filter(d => d.regexp.test(target.hostname))
.reduce((acc, current) =>
{
if (acc == null)
{
return current;
}
else if (acc.domain.length >= current.domain.length)
{
return acc;
}
else
{
return current;
}
}, null);
if (match != null)
{
return match;
}
if (originUrl === "" || originUrl === null || originUrl === undefined)
{
return this._anyConf;
}
let source = new URL(originUrl);
if (target.hostname === source.hostname)
{
/* same domain */
return this._sameConf;
}
else
{
/* default */
return this._anyConf;
}
}
/*
* Compute referrer for a given pair of URL and its origin.
*/
computeReferrer(url, originUrl)
{
if (url.startsWith("about:") || url.startsWith("data:"))
{
return originUrl;
}
const conf = this.findHostConf(url, originUrl);
var referrer = null;
switch (conf.action)
{
case "prune":
if (!originUrl)
{
referrer = "";
}
else
{
referrer = new URL(originUrl).origin + "/";
}
break;
case "target":
referrer = new URL(url).origin + "/";
break;
case "replace":
referrer = conf.referer;
break;
case "remove":
referrer = "";
break;
default:
/* "keep" */
referrer = originUrl;
}
return referrer;
}
/*
* Escape function from
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
static escapeRegExp(string)
{
// eslint-disable-next-line no-useless-escape
return string.replace(/[.*+?^${}()|\[\]\\]/g, "\\$&");
// $& means the whole matched string
}
/*
* Create regular expressions from domains to also match subdomains.
*/
static createDomainRegex(domains)
{
for (let domain of domains)
{
let pattern = "(\\.|^)"
+ RefererModEngine.escapeRegExp(domain.domain) + "$";
//console.log(`domain '${domain.domain}', pattern: ${pattern}`);
domain.regexp = new RegExp(pattern);
}
return domains;
}
}