-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve-test-config.mjs
More file actions
257 lines (221 loc) Β· 8.63 KB
/
serve-test-config.mjs
File metadata and controls
257 lines (221 loc) Β· 8.63 KB
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
#!/usr/bin/env node
/**
* Test Config Server
*
* Serves a modified privacy configuration via HTTP for testing.
* The macOS browser can fetch this during config refresh cycles.
*
* Usage:
* node scripts/serve-test-config.mjs [--port=8899] [--config=path/to/config.json]
*
* Default port: 8899
* Default config: Uses base config from remote-config with Nintendo fix applied
*
* To use with macOS browser:
* 1. Start this server
* 2. Set custom config URL in UserDefaults:
* defaults write HKE973VLUW.com.duckduckgo.macos.browser.app-configuration.debug \
* "CustomConfigurationURL.privacyConfiguration" "http://localhost:8899/v4/macos-config.json"
* 3. Restart the browser
*
* Note: For startup testing, use `npm run build:macos-with-fix` instead to bake the config
* into the app binary.
*/
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, '..');
const metaRoot = path.resolve(projectRoot, '..');
// Parse CLI args
const args = process.argv.slice(2);
const portArg = args.find((a) => a.startsWith('--port='));
const configArg = args.find((a) => a.startsWith('--config='));
const PORT = portArg ? parseInt(portArg.split('=')[1], 10) : 8899;
const customConfigPath = configArg ? configArg.split('=')[1] : null;
/**
* Load the base macOS config and apply the Nintendo fix
*/
function loadConfigWithFix() {
// Try to load from remote-config generated output first
const generatedConfigPath = path.join(metaRoot, 'remote-config/generated/v4/macos-config.json');
const bundledConfigPath = path.join(
metaRoot,
'apple-browsers/macOS/DuckDuckGo/ContentBlocker/Resources/macos-config.json'
);
let baseConfig;
let sourceUsed;
if (fs.existsSync(generatedConfigPath)) {
baseConfig = JSON.parse(fs.readFileSync(generatedConfigPath, 'utf-8'));
sourceUsed = 'remote-config/generated';
} else if (fs.existsSync(bundledConfigPath)) {
baseConfig = JSON.parse(fs.readFileSync(bundledConfigPath, 'utf-8'));
sourceUsed = 'apple-browsers bundled';
} else {
throw new Error('No base config found. Run `npm run build` in remote-config first.');
}
console.log(`π¦ Base config loaded from: ${sourceUsed}`);
// Apply Nintendo fix - add to unprotectedTemporary
const nintendoEntry = {
domain: 'nintendo.com',
reason: 'Testing - checkout flow breakage investigation',
};
// Ensure unprotectedTemporary exists
if (!baseConfig.unprotectedTemporary) {
baseConfig.unprotectedTemporary = [];
}
// Check if Nintendo is already in the list
const alreadyExists = baseConfig.unprotectedTemporary.some(
(entry) => entry.domain === 'nintendo.com' || entry === 'nintendo.com'
);
if (!alreadyExists) {
baseConfig.unprotectedTemporary.push(nintendoEntry);
console.log('π§ Added nintendo.com to unprotectedTemporary');
} else {
console.log('βΉοΈ nintendo.com already in unprotectedTemporary');
}
// Also add to feature exceptions for complete protection disabling
const featuresToExcept = [
'contentBlocking',
'autoconsent',
'gpc',
'cookie',
'trackerAllowlist',
'fingerprintingCanvas',
'fingerprintingHardware',
'fingerprintingScreenSize',
];
for (const featureName of featuresToExcept) {
if (baseConfig.features[featureName]) {
if (!baseConfig.features[featureName].exceptions) {
baseConfig.features[featureName].exceptions = [];
}
const featureHasNintendo = baseConfig.features[featureName].exceptions.some(
(e) => e.domain === 'nintendo.com'
);
if (!featureHasNintendo) {
baseConfig.features[featureName].exceptions.push({
domain: 'nintendo.com',
reason: 'Testing checkout flow',
});
}
}
}
// Update version timestamp so the app thinks it's a new config
baseConfig.version = Date.now();
return baseConfig;
}
/**
* Load custom config file
*/
function loadCustomConfig(configPath) {
const absolutePath = path.isAbsolute(configPath) ? configPath : path.resolve(process.cwd(), configPath);
if (!fs.existsSync(absolutePath)) {
throw new Error(`Config file not found: ${absolutePath}`);
}
return JSON.parse(fs.readFileSync(absolutePath, 'utf-8'));
}
// Load the config
let config;
try {
if (customConfigPath) {
config = loadCustomConfig(customConfigPath);
console.log(`π Custom config loaded from: ${customConfigPath}`);
} else {
config = loadConfigWithFix();
}
} catch (error) {
console.error(`β Failed to load config: ${error.message}`);
process.exit(1);
}
// Calculate ETag
const configJson = JSON.stringify(config);
const etag = `"${Buffer.from(configJson).length}-${config.version}"`;
// Create HTTP server
const server = http.createServer((req, res) => {
const url = new URL(req.url, `http://localhost:${PORT}`);
console.log(`${new Date().toISOString()} ${req.method} ${url.pathname}`);
// CORS headers for debugging
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, If-None-Match');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// Serve config at /v4/macos-config.json (matching production URL structure)
if (url.pathname === '/v4/macos-config.json' || url.pathname === '/macos-config.json' || url.pathname === '/') {
// Check If-None-Match for caching
const clientEtag = req.headers['if-none-match'];
if (clientEtag === etag) {
res.writeHead(304);
res.end();
return;
}
res.writeHead(200, {
'Content-Type': 'application/json',
ETag: etag,
'Cache-Control': 'no-cache',
});
res.end(configJson);
return;
}
// Health check
if (url.pathname === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
status: 'ok',
config: {
version: config.version,
features: Object.keys(config.features).length,
unprotectedTemporary: config.unprotectedTemporary?.length || 0,
nintendoProtected:
!config.unprotectedTemporary?.some(
(e) => e.domain === 'nintendo.com' || e === 'nintendo.com'
),
},
})
);
return;
}
// 404 for everything else
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
error: 'Not found',
availableEndpoints: ['/v4/macos-config.json', '/macos-config.json', '/', '/health'],
})
);
});
server.listen(PORT, () => {
console.log(`
π Test Config Server Running
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
Config URL: http://localhost:${PORT}/v4/macos-config.json
Health: http://localhost:${PORT}/health
Config version: ${config.version}
Features: ${Object.keys(config.features).length}
Nintendo unprotected: ${config.unprotectedTemporary?.some((e) => e.domain === 'nintendo.com' || e === 'nintendo.com') ? 'YES β' : 'NO β'}
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
To use with macOS browser (for refresh-based testing):
defaults write HKE973VLUW.com.duckduckgo.macos.browser.app-configuration.debug \\
isInternalUser -bool true
defaults write HKE973VLUW.com.duckduckgo.macos.browser.app-configuration.debug \\
"CustomConfigurationURL.privacyConfiguration" \\
"http://localhost:${PORT}/v4/macos-config.json"
Note: For startup testing, the config must be baked into the build.
See: npm run build:macos-with-fix
Press Ctrl+C to stop.
`);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`β Port ${PORT} is already in use. Try --port=<different-port>`);
} else {
console.error(`β Server error: ${err.message}`);
}
process.exit(1);
});