-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprivacy.js
364 lines (330 loc) · 9.6 KB
/
privacy.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* @fileoverview I2P Privacy Manager
* Handles Firefox privacy settings and data cleanup for I2P container tabs
*/
const PRIVACY_CONFIG = {
TITLE_PREFACE: chrome.i18n.getMessage("titlePreface"),
SNOWFLAKE_ID: "{b11bea1f-a888-4332-8d8a-cec2be7d24b9}",
WINDOWS: {
OS: "win",
},
BROWSING_DATA: {
SINCE: "forever",
TYPES: ["downloads", "passwords", "formData", "localStorage", "history"],
},
};
/**
* Privacy Manager for handling browser privacy settings
*/
class PrivacyManager {
/**
* Set browser privacy settings
* @param {Object} settings Privacy setting configuration
* @return {Promise<boolean>}
*/
static async setSetting(setting, value) {
try {
const result = await setting.set({ value });
console.info(`Privacy setting updated : ${value}`);
return true;
} catch (error) {
console.error("Privacy setting failed:", error);
return false;
}
}
/**
* Configure hyperlink auditing
* @param {boolean} enabled Whether to enable auditing
*/
static async configureHyperlinkAuditing(enabled = false) {
await this.setSetting(
browser.privacy.websites.hyperlinkAuditingEnabled,
enabled
);
}
/**
* Configure first party isolation
* @param {boolean} enabled Whether to enable isolation
*/
static async configureFirstPartyIsolation(enabled = true) {
await this.setSetting(browser.privacy.websites.firstPartyIsolate, enabled);
}
/**
* Configure cookie behavior
* @param {boolean} allowThirdParty Whether to allow third party cookies
*/
static async configureCookies(allowThirdParty = false) {
try {
const cookieConfig = await browser.privacy.websites.cookieConfig.get({});
await browser.privacy.websites.cookieConfig.set({
value: {
behavior: allowThirdParty ? "allow_all" : "reject_third_party",
nonPersistentCookies: cookieConfig.value.nonPersistentCookies,
},
});
} catch (error) {
console.error("Cookie configuration failed:", error);
}
}
/**
* Configure referrer policy
* @param {boolean} enabled Whether to enable referrers
*/
static async configureReferrers(enabled = false) {
await this.setSetting(browser.privacy.websites.referrersEnabled, enabled);
}
/**
* Configure fingerprinting resistance
* @param {boolean} enabled Whether to enable resistance
*/
static async configureFingerprinting(enabled = true) {
await this.setSetting(
browser.privacy.websites.resistFingerprinting,
enabled
);
}
/**
* Configure tracking protection
* @param {boolean} enabled Whether to enable protection
*/
static async configureTrackingProtection(enabled = true) {
await this.setSetting(
browser.privacy.websites.trackingProtectionMode,
enabled ? "always" : "never"
);
}
/**
* Configure DRM content
* @param {boolean} enabled Whether to enable DRM
*/
static async configureDRM(enabled = false) {
const platformInfo = await browser.runtime.getPlatformInfo();
if (platformInfo.os === PRIVACY_CONFIG.WINDOWS.OS) {
await this.setSetting(
browser.privacy.websites.protectedContentEnabled,
enabled
);
}
}
/**
* Configure WebRTC
* @param {boolean} enabled Whether to enable WebRTC
*/
static async configureWebRTC(enabled = false) {
try {
const snowflake = await browser.management.get(
PRIVACY_CONFIG.SNOWFLAKE_ID
);
console.info("Snowflake detected, preserving WebRTC");
return;
} catch {
await this.setSetting(
browser.privacy.network.peerConnectionEnabled,
enabled
);
await this.setSetting(
chrome.privacy.network.webRTCIPHandlingPolicy,
enabled ? "disable_non_proxied_udp" : "default"
);
}
}
/**
* Configure password saving
* @param {boolean} enabled Whether to enable password saving
*/
static async configurePasswordSaving(enabled = false) {
await this.setSetting(
browser.privacy.services.passwordSavingEnabled,
enabled
);
}
/**
* Apply recommended privacy settings
*/
static async applyRecommendedSettings() {
await Promise.all([
this.configureHyperlinkAuditing(false),
this.configureFirstPartyIsolation(true),
this.configureCookies(false),
this.configureFingerprinting(true),
this.configureTrackingProtection(true),
this.configureDRM(false),
this.configureWebRTC(false),
this.configurePasswordSaving(false),
]);
}
/**
* Reset all privacy settings to defaults
*/
static async resetAllSettings() {
await Promise.all([
browser.privacy.websites.hyperlinkAuditingEnabled.clear(),
browser.privacy.websites.firstPartyIsolate.clear(),
browser.privacy.websites.cookieConfig.clear(),
browser.privacy.websites.referrersEnabled.clear(),
browser.privacy.websites.resistFingerprinting.clear(),
browser.privacy.websites.trackingProtectionMode.clear(),
browser.privacy.websites.protectedContentEnabled.clear(),
browser.privacy.network.peerConnectionEnabled.clear(),
browser.privacy.services.passwordSavingEnabled.clear(),
]);
}
}
/**
* Data Cleanup Manager for handling browsing data
*/
class DataCleanupManager {
/**
* Clean browsing data for I2P domains
* @param {Object} options Cleanup options
*/
static async cleanBrowsingData(options = {}) {
const since = this.calculateCleanupTime(
options.since || PRIVACY_CONFIG.BROWSING_DATA.SINCE
);
try {
const i2pHistory = await browser.history.search({
text: "i2p",
startTime: 0,
});
for (const item of i2pHistory) {
if (this.isI2PUrl(item.url)) {
await this.cleanupForDomain(item.url, since);
}
}
await this.notifyCleanup();
} catch (error) {
console.error("Data cleanup failed:", error);
}
}
/**
* Calculate cleanup timestamp
* @param {string} timeframe Cleanup timeframe
* @returns {number} Timestamp
*/
static calculateCleanupTime(timeframe) {
const times = {
hour: () => 1000 * 60 * 60,
day: () => 1000 * 60 * 60 * 24,
week: () => 1000 * 60 * 60 * 24 * 7,
forever: () => 0,
};
return timeframe === "forever"
? 0
: Date.now() - (times[timeframe] || times.forever)();
}
/**
* Clean up data for specific domain
* @param {string} url Domain URL
* @param {number} since Timestamp
*/
static async cleanupForDomain(url, since) {
const hostname = this.extractI2PHostname(url);
await Promise.all([
browser.history.deleteUrl({ url }),
browser.browsingData.removeCache({}),
browser.browsingData.removePasswords({ hostnames: [hostname], since }),
browser.browsingData.removeDownloads({ hostnames: [hostname], since }),
browser.browsingData.removeFormData({ hostnames: [hostname], since }),
browser.browsingData.removeLocalStorage({ hostnames: [hostname], since }),
]);
await this.cleanupContainerCookies(url);
}
/**
* Clean up container cookies
* @param {string} url Domain URL
*/
static async cleanupContainerCookies(url) {
const containers = await browser.contextualIdentities.query({
name: PRIVACY_CONFIG.TITLE_PREFACE,
});
for (const container of containers) {
const cookies = await browser.cookies.getAll({
firstPartyDomain: null,
storeId: container.cookieStoreId,
});
for (const cookie of cookies) {
await browser.cookies.remove({
firstPartyDomain: cookie.firstPartyDomain,
name: cookie.name,
url: url,
});
}
}
}
/**
* Extract I2P hostname from URL
* @param {string} url URL to parse
* @returns {string} I2P hostname
*/
static extractI2PHostname(url) {
try {
const urlObj = new URL(url);
if (urlObj.host.endsWith(".i2p")) {
return urlObj.host;
}
if (url.includes(".i2p")) {
const parts = url.split("=");
for (const part of parts) {
const items = part.split("%");
for (const item of items) {
if (item.includes(".i2p")) {
return item.replace("3D", "");
}
}
}
}
return url.split("/")[2] || url.split("/")[0];
} catch (error) {
console.error("Hostname extraction failed:", error);
return "";
}
}
/**
* Check if URL is I2P
* @param {string} url URL to check
* @returns {boolean}
*/
static isI2PUrl(url) {
const hostname = this.extractI2PHostname(url);
return hostname.split(":")[0].endsWith(".i2p");
}
/**
* Send cleanup notification
*/
static async notifyCleanup() {
await browser.notifications.create({
type: "basic",
title: "Removed browsing data",
message: "Cleaned I2P browsing data and history",
});
}
}
// Initialize privacy settings
PrivacyManager.applyRecommendedSettings();
// Listen for uninstall
browser.management.onUninstalled.addListener(async (info) => {
const selfInfo = await browser.management.getSelf();
if (info.name === selfInfo.name) {
await PrivacyManager.resetAllSettings();
}
});
// Listen for messages
browser.runtime.onMessage.addListener(async (message) => {
switch (message.type) {
case "cleanupData":
await DataCleanupManager.cleanBrowsingData(message.options);
break;
case "updatePrivacy":
await PrivacyManager.applyRecommendedSettings();
break;
}
});
// Export for testing
if (typeof module !== "undefined" && module.exports) {
module.exports = {
PrivacyManager,
DataCleanupManager,
PRIVACY_CONFIG,
};
}