forked from QuiteAFancyEmerald/Holy-Unblocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyServiceValidator.js
334 lines (288 loc) · 11.8 KB
/
proxyServiceValidator.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
// This file is solely used for the automatically run GitHub job, which checks to
// see if all HU LTS code is working properly (at least on an Ubuntu machine).
const axios = require('axios');
const puppeteer = require('puppeteer');
const testEndpoint = async (url) => {
try {
const response = await axios.get(url);
return response.status === 200;
} catch (error) {
console.error(`Error while testing ${url}:`, error.message);
return false;
}
};
const generateUrl = async (omniboxId, urlPath, errorPrefix = 'failure') => {
// Wait for the document to load before getting the omnibox.
await new Promise((resolve) => {
if (document.readyState === 'complete') resolve();
else window.addEventListener('load', resolve);
});
let omnibox = document.getElementById(omniboxId);
omnibox = omnibox && omnibox.querySelector('input[type=text]');
if (omnibox) {
try {
// Send an artificial input to the omnibox. The omnibox will create
// a proxy URL and leave it as the input value in response.
omnibox.value = urlPath;
const generateInput = async () => {
await omnibox.dispatchEvent(
new KeyboardEvent('keydown', { code: 'Validator Test' })
);
};
/* Keep trying to send an input signal every second until it works.
* Implemented to account for a condition where the document has
* finished loading, but the event handler for DOMContentLoaded has
* not finished executing its script to listen for artificial inputs.
*/
await generateInput();
const inputInterval = setInterval(generateInput, 1000),
resolveHandler = (resolve) => () => {
clearInterval(inputInterval);
resolve(omnibox.value);
},
// Wait up to 40 seconds for the omnibox to finish updating.
loadUrl = new Promise((resolve) => {
if (omnibox.value !== urlPath) resolveHandler(resolve)();
else omnibox.addEventListener('change', resolveHandler(resolve));
}),
timeout = new Promise((resolve) => {
setTimeout(resolveHandler(resolve), 40000);
}),
// Return the proxy URL that the omnibox left here.
generatedUrl = await Promise.race([loadUrl, timeout]);
return generatedUrl !== urlPath ? generatedUrl : errorPrefix;
} catch (e) {
return errorPrefix + ': ' + e.message;
}
} else {
return errorPrefix + ': omnibox not defined';
}
};
const testGeneratedUrl = async (url, headers) => {
try {
console.log('Testing generated URL:', url);
const response = await axios.get(url, { headers });
console.log(`Response status for ${url}:`, response.status);
return response.status === 200;
} catch (error) {
console.error(`Error while testing generated URL ${url}:`, error.message);
return false;
}
};
const testServerResponse = async () => {
const endpoints = [
'http://localhost:8080/',
'http://localhost:8080/test-404',
'http://localhost:8080/browsing',
'http://localhost:8080/rammerhead',
'http://localhost:8080/ultraviolet',
'http://localhost:8080/documentation',
'http://localhost:8080/questions',
'http://localhost:8080/s',
'http://localhost:8080/credits',
'http://localhost:8080/bookmarklets',
'http://localhost:8080/terms',
'http://localhost:8080/games',
'http://localhost:8080/web-games',
'http://localhost:8080/emulators',
'http://localhost:8080/flash-games',
'http://localhost:8080/retro-games',
'http://localhost:8080/youtube',
'http://localhost:8080/apps',
'http://localhost:8080/flash',
'http://localhost:8080/webretro',
'http://localhost:8080/vibe-os',
'http://localhost:8080/assets/js/particlesjs/particles.js',
'http://localhost:8080/assets/js/bareTransport.js',
'http://localhost:8080/assets/js/card.js',
'http://localhost:8080/assets/js/common-16451543478.js',
'http://localhost:8080/assets/js/csel.js',
'http://localhost:8080/assets/js/register-sw.js',
'http://localhost:8080/assets/json/emu-nav.json',
'http://localhost:8080/assets/json/blacklist.json',
'http://localhost:8080/assets/json/emulib-nav.json',
'http://localhost:8080/assets/json/flash-nav.json',
'http://localhost:8080/assets/json/h5-nav.json',
'http://localhost:8080/assets/json/links.json',
'http://localhost:8080/baremux/index.js',
'http://localhost:8080/baremux/worker.js',
'http://localhost:8080/epoxy/index.mjs',
'http://localhost:8080/uv/uv.bundle.js',
'http://localhost:8080/uv/sw.js',
'http://localhost:8080/uv/uv.config.js',
'http://localhost:8080/uv/workerware.js',
'http://localhost:8080/uv/WWError.js',
];
const results = await Promise.all(endpoints.map(testEndpoint));
const allPassed = results.every((result) => result);
if (allPassed) {
console.log('All endpoints responded with status code 200. Test passed.');
await testCommonJSOnPage();
} else {
console.error(
'One or more endpoints failed to respond with status code 200. Test failed.'
);
process.exitCode = 1;
}
};
const testCommonJSOnPage = async () => {
const browser = await puppeteer.launch({
args: [
'--enable-features=NetworkService',
'--enable-features=ServiceWorker',
'--enable-features=InsecureOrigins',
],
headless: true,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
try {
const getHeaders = async () => {
const headers = {};
headers['User-Agent'] = await page.evaluate(() => navigator.userAgent);
headers['Referer'] = await page.evaluate(() => window.location.href);
return headers;
};
const testRammerhead = async () => {
const omniboxId = 'pr-rh',
urlPath = 'example.com';
await page.goto('http://localhost:8080/rammerhead');
const generatedUrl = await page.evaluate(generateUrl, omniboxId, urlPath);
const testResults = {};
testResults.rammerhead = generatedUrl;
console.log('Rammerhead test results:', testResults);
const headers = await getHeaders();
const rammerheadTestPassed =
testResults.rammerhead !== 'failure' &&
(await testGeneratedUrl(testResults.rammerhead, headers));
console.log(
`Rammerhead test result: ${
rammerheadTestPassed ? 'success' : 'failure'
}`
);
return rammerheadTestPassed;
};
/*
xx
xx xx
xxx xx
xxx xx
xxx xx
xxx xx
xx xx
xx xx
xx
xx
x x
xxxxxxxxxxxxxxx
xxxxxxxxxxxx xxxxx
xxxx xxx
xxx xxx
xxx xx
xx xx
xx xx
xxx x
xx x
xx xx
*/
const testUltraviolet = async () => {
const omniboxId = 'pr-uv',
errorPrefix = 'failure',
// For the hacky URL test further below, use the URL page's EXACT title.
website = Object.freeze({
path: 'example.com',
title: 'Example Domain',
});
await page.goto('http://localhost:8080/ultraviolet');
const generatedUrl = await page.evaluate(
generateUrl,
omniboxId,
website.path,
errorPrefix
);
const testResults = await page.evaluate(
async (generatedUrl, pageTitle) => {
const results = [{}, {}];
await new Promise((resolve) => {
const waitForDocument = () => {
if (document.readyState === 'complete') resolve();
else window.addEventListener('load', resolve);
},
// Wait until a service worker is registered before continuing.
// Also check again to make sure the document is loaded.
waitForWorker = async () => {
setTimeout(async () => {
(await navigator.serviceWorker.getRegistrations()).length > 0
? waitForDocument()
: waitForWorker();
}, 1000);
};
waitForWorker();
});
try {
results[0].ultraviolet = generatedUrl;
// Test to see if the document title for example.com has loaded,
// by appending an IFrame to the document and grabbing its content.
const testGeneratedUrlHacky = async (url) => {
const exampleIFrame = document.createElement('iframe');
const waitForDocument = new Promise((resolve) => {
document.documentElement.appendChild(exampleIFrame);
exampleIFrame.addEventListener('load', () => {
resolve(
exampleIFrame.contentWindow.document.title === pageTitle
);
});
});
// Give 10 seconds for the IFrame to load before manually checking.
const timeout = new Promise((resolve) => {
setTimeout(() => {
resolve(
exampleIFrame.contentWindow.document.title === pageTitle
);
}, 10000);
});
exampleIFrame.src = url;
exampleIFrame.style.display = 'none';
return await Promise.race([waitForDocument, timeout]);
};
results[1].uvTestPassed =
!!results[0].ultraviolet.indexOf(errorPrefix) &&
(await testGeneratedUrlHacky(results[0].ultraviolet));
} catch (e) {
results[0].ultraviolet = errorPrefix + ': ' + e.message;
}
return results;
},
generatedUrl,
website.title,
errorPrefix
);
console.log('Ultraviolet test results:', testResults[0]);
const uvTestPassed =
testResults[0].ultraviolet &&
testResults[0].ultraviolet !== 'failure' &&
testResults[1].uvTestPassed;
console.log(
'Ultraviolet test result:',
uvTestPassed ? 'success' : 'failure'
);
return uvTestPassed;
};
// Run tests for Rammerhead and Ultraviolet.
const rammerheadPassed = await testRammerhead();
const ultravioletPassed = await testUltraviolet();
if (rammerheadPassed && ultravioletPassed) {
console.log('Both tests passed.');
process.exitCode = 0;
} else {
console.error('Tests failed.');
process.exitCode = 1;
}
} catch (error) {
console.error('Error in testCommonJSOnPage:', error.message);
process.exitCode = 1;
} finally {
await browser.close();
}
};
testServerResponse();