Holy-Unblocker/test.js
2024-07-10 18:44:49 -07:00

196 lines
5.7 KiB
JavaScript

const axios = require("axios");
const puppeteer = require("puppeteer");
async function testEndpoint(url) {
try {
const response = await axios.get(url);
return response.status === 200;
} catch (error) {
console.error(`Error while testing ${url}:`, error.message);
return false;
}
}
async function testGeneratedUrl(url, headers) {
try {
console.log(`Testing generated URL: ${url}`);
// Make the Axios request with custom headers
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;
}
}
async function testServerResponse() {
const endpoints = [
"http://localhost:8080/",
"http://localhost:8080/?pathtonowhere",
"http://localhost:8080/?browse",
"http://localhost:8080/?rh",
"http://localhost:8080/?q",
];
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.exit(1);
}
}
async function testCommonJSOnPage() {
const browser = await puppeteer.launch({
args: [
"--enable-features=NetworkService",
"--enable-features=ServiceWorker",
"--enable-features=InsecureOrigins",
],
headless: false,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
try {
// Function to extract headers from the page context
async function getHeaders() {
const headers = {};
// Example: Extract headers dynamically from the page
headers['User-Agent'] = await page.evaluate(() => navigator.userAgent);
headers['Referer'] = await page.evaluate(() => window.location.href);
return headers;
}
// Function to test Rammerhead with "?rh"
async function testRammerhead() {
await page.goto("http://localhost:8080/?rh");
const testResults = await page.evaluate(async () => {
const results = {};
await new Promise((resolve) => {
if (document.readyState === "complete") {
resolve();
} else {
window.addEventListener("load", resolve);
}
});
if (window.goProx) {
try {
const rammerheadUrl = await window.goProx.rammerhead(
"example.com",
false
);
console.log("Generated Rammerhead URL:", rammerheadUrl);
results.rammerhead = rammerheadUrl ? rammerheadUrl : "failure";
} catch (e) {
results.rammerhead = "failure: " + e.message;
}
} else {
results.goProx = "not defined";
}
return results;
});
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;
}
async function testUltraviolet() {
await page.goto("http://localhost:8080/?q");
// Wait for the document's readyState to be complete
await page.waitForFunction(() => document.readyState === "complete");
// Evaluate the function to register the service worker
await page.evaluate(async () => {
const stockSW = "/uv/sw.js";
const swAllowedHostnames = ["localhost", "127.0.0.1"];
async function registerSW() {
if (!navigator.serviceWorker) {
if (
location.protocol !== "https:" &&
!swAllowedHostnames.includes(location.hostname)
)
throw new Error(
"Service workers cannot be registered without https."
);
throw new Error("Your browser doesn't support service workers.");
}
await navigator.serviceWorker.register(stockSW);
let wispUrl =
(location.protocol === "https:" ? "wss" : "ws") +
"://" +
location.host +
"/wisp/";
await BareMux.SetTransport("EpxMod.EpoxyClient", { wisp: wispUrl });
// When testing proxy support, clear service workers from 8080 (or whatever current port you are using)
// navigator.serviceWorker.register(stockSW).then(register => register.unregister().then(bool => console.log("Unregistered: " + bool)));
}
await registerSW();
});
// Test for the existence of /assets/js/register-sw.js
const swTestPassed = await testEndpoint("http://localhost:8080/assets/js/register-sw.js");
console.log(
`Service Worker registration test result: ${
swTestPassed ? "success" : "failure"
}`
);
return swTestPassed;
}
// Run tests for Rammerhead and Ultraviolet
const rammerheadPassed = await testRammerhead();
const ultravioletPassed = await testUltraviolet();
if (rammerheadPassed && ultravioletPassed) {
console.log("Both tests passed.");
process.exit(0);
} else {
console.error("Tests failed.");
process.exit(1);
}
} catch (error) {
console.error("Error in testCommonJSOnPage:", error.message);
process.exit(1);
} finally {
await browser.close();
}
}
testServerResponse();