Merge pull request #75 from RadiusProxy/adBlocking

Feat: Add Adblocking functionality
This commit is contained in:
MotorTruck1221 2025-06-14 23:08:55 -06:00 committed by GitHub
commit 975050c76e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 62 additions and 9 deletions

View file

@ -18,7 +18,7 @@ const viteWispServer = (): Plugin => {
name: "vite-wisp-server", name: "vite-wisp-server",
configureServer(server) { configureServer(server) {
server.httpServer?.on("upgrade", (req, socket, head) => { server.httpServer?.on("upgrade", (req, socket, head) => {
req.url.startsWith("/wisp") ? wisp.routeRequest(req, socket, head) : undefined; req.url.startsWith("/wisp") || req.url.startsWith("/adblock") ? wisp.routeRequest(req, socket, head) : undefined;
}); });
} }
}; };

View file

@ -23,7 +23,8 @@ const serverFactory: FastifyServerFactory = (
handler(req, res); handler(req, res);
}) })
.on("upgrade", (req, socket, head) => { .on("upgrade", (req, socket, head) => {
if (req.url?.endsWith("/wisp/")) { if (req.url?.endsWith("/wisp/") || req.url?.endsWith("/adblock/")) {
console.log(req.url);
wisp.routeRequest(req, socket as Socket, head); wisp.routeRequest(req, socket as Socket, head);
} }
}); });

View file

@ -7,6 +7,7 @@
const init = async () => { const init = async () => {
settings.searchEngine(); settings.searchEngine();
settings.proxy(); settings.proxy();
settings.adBlock();
await sw.wispServer(); await sw.wispServer();
} }

View file

@ -2,12 +2,13 @@
import type { DropdownOptions } from "@utils/types"; import type { DropdownOptions } from "@utils/types";
interface Props { interface Props {
id: string; id: string;
classes?: string;
options: DropdownOptions[]; options: DropdownOptions[];
} }
const { id, options } = Astro.props; const { id, options, classes } = Astro.props;
--- ---
<select id=`dropdownBox-${id}` class="cursor-pointer flex h-10 w-[180px] items-center justify-between text-(--foreground) background-(--background) rounded-lg border border-(--border) px-3 py-2"> <select id=`dropdownBox-${id}` class=`${classes} cursor-pointer flex h-10 w-[180px] items-center justify-between text-(--foreground) background-(--background) rounded-lg border border-(--border) px-3 py-2`>
{options.map((el) => {options.map((el) =>
<option class="w-full bg-(--accent) rounded-sm p-1" value={el.value}>{el.name}</option> <option class="w-full bg-(--accent) rounded-sm p-1" value={el.value}>{el.name}</option>
)} )}

View file

@ -37,6 +37,15 @@ Object.keys(SearchEngines).forEach((k) =>
<div> <div>
<p> Wisp Server </p> <p> Wisp Server </p>
<Input id="wispServerSwitcher" placeholder="Wisp server URL (EX: wss://radiusproxy.app/wisp/" /> <Input id="wispServerSwitcher" placeholder="Wisp server URL (EX: wss://radiusproxy.app/wisp/" />
<div class="mt-2 hidden" id="adBlocking">
<p> Ad Blocking </p>
<Dropdown id="adBlocking" options={
[
{ name: 'Enabled', value: 'enabled', default: true },
{ name: 'Disabled', value: 'disabled' }
]
} />
</div>
</div> </div>
<div class="mt-2 mb-2 hidden" id="wispServerInfo"> <div class="mt-2 mb-2 hidden" id="wispServerInfo">
<p class="text-blue-500" id="wispServerInfo-inner"> Checking URL... </p> <p class="text-blue-500" id="wispServerInfo-inner"> Checking URL... </p>
@ -90,9 +99,10 @@ Object.keys(SearchEngines).forEach((k) =>
const wispServerInfoInner = document.getElementById("wispServerInfo-inner") as HTMLParagraphElement; const wispServerInfoInner = document.getElementById("wispServerInfo-inner") as HTMLParagraphElement;
const wispServerSave = document.getElementById("wispServerSave") as HTMLButtonElement; const wispServerSave = document.getElementById("wispServerSave") as HTMLButtonElement;
const wispServerReset = document.getElementById("wispServerReset") as HTMLButtonElement; const wispServerReset = document.getElementById("wispServerReset") as HTMLButtonElement;
const adBlocking = document.getElementById("adBlocking") as HTMLDivElement;
wispServerSwitcher.value = opts.storageManager.getVal("wispServer"); wispServerSwitcher.value = opts.storageManager.getVal("wispServer");
const resetVal = `${(location.protocol === "https:" ? "wss://" : "ws://")}${location.host}/wisp/`
const reset = (hide: boolean = true) => { const reset = (hide: boolean = true) => {
if (hide) wispServerInfo.classList.add("hidden"); if (hide) wispServerInfo.classList.add("hidden");
wispServerInfoInner.innerText = "Checking URL..." wispServerInfoInner.innerText = "Checking URL..."
@ -100,6 +110,26 @@ Object.keys(SearchEngines).forEach((k) =>
wispServerInfoInner.classList.remove("text-green-500"); wispServerInfoInner.classList.remove("text-green-500");
}; };
const adBlockingFunc = () => {
const adBlockingDropdown = document.getElementById("dropdownBox-adBlocking") as HTMLSelectElement;
adBlockingDropdown.addEventListener("change", () => {
opts.settings.adBlock(adBlockingDropdown.value === "enabled" ? true : false);
});
adBlockingDropdown.value = opts.storageManager.getVal("adBlock") === "true" ? "enabled" : "disabled";
if (wispServerSwitcher.value === resetVal) {
adBlocking.classList.remove("hidden");
opts.settings.adBlock(true);
adBlockingDropdown.value = "enabled";
}
else {
adBlocking.classList.add("hidden");
opts.settings.adBlock(false);
}
}
adBlockingFunc();
wispServerSave.addEventListener("click", async () => { wispServerSave.addEventListener("click", async () => {
const server = wispServerSwitcher.value; const server = wispServerSwitcher.value;
wispServerInfo.classList.remove("hidden"); wispServerInfo.classList.remove("hidden");
@ -114,7 +144,9 @@ Object.keys(SearchEngines).forEach((k) =>
reset(false); reset(false);
wispServerInfoInner.innerText = "Wisp Server Set!"; wispServerInfoInner.innerText = "Wisp Server Set!";
wispServerInfoInner.classList.add("text-green-500"); wispServerInfoInner.classList.add("text-green-500");
//adBlocking.classList.contains("hidden") ? "" : adBlocking.classList.add("hidden");
await opts.sw.wispServer(wispServerSwitcher.value, true); await opts.sw.wispServer(wispServerSwitcher.value, true);
adBlockingFunc();
} }
// We reset this after 4 seconds (any errors OR success) // We reset this after 4 seconds (any errors OR success)
@ -125,9 +157,10 @@ Object.keys(SearchEngines).forEach((k) =>
wispServerInfo.classList.remove("hidden"); wispServerInfo.classList.remove("hidden");
wispServerInfoInner.innerText = "Wisp Server Reset!"; wispServerInfoInner.innerText = "Wisp Server Reset!";
wispServerInfoInner.classList.add("text-green-500"); wispServerInfoInner.classList.add("text-green-500");
await opts.sw.wispServer(`${(location.protocol === "https:" ? "wss://" : "ws://")}${location.host}/wisp/`, true); await opts.sw.wispServer(resetVal, true);
wispServerSwitcher.value = opts.storageManager.getVal("wispServer"); wispServerSwitcher.value = opts.storageManager.getVal("wispServer");
setTimeout(reset, 4000); setTimeout(reset, 4000);
adBlockingFunc();
}); });
} }

View file

@ -60,6 +60,13 @@ class SW {
async setTransport(transport?: "epoxy" | "libcurl", get?: boolean) { async setTransport(transport?: "epoxy" | "libcurl", get?: boolean) {
console.log("Setting transport"); console.log("Setting transport");
const wispServer = (): string => {
const wispServerVal = this.#storageManager.getVal("wispServer");
if (this.#storageManager.getVal("adBlock") === "true") {
return wispServerVal.replace("/wisp/", "/adblock/");
}
return wispServerVal;
}
if (get) return this.#storageManager.getVal("transport"); if (get) return this.#storageManager.getVal("transport");
this.#storageManager.setVal( this.#storageManager.setVal(
"transport", "transport",
@ -68,23 +75,24 @@ class SW {
switch (transport) { switch (transport) {
case "epoxy": { case "epoxy": {
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [ await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") } { wisp: wispServer() }
]); ]);
} }
case "libcurl": { case "libcurl": {
await this.#baremuxConn!.setTransport("/libcurl/index.mjs", [ await this.#baremuxConn!.setTransport("/libcurl/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") } { wisp: wispServer() }
]); ]);
} }
default: { default: {
await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [ await this.#baremuxConn!.setTransport("/epoxy/index.mjs", [
{ wisp: this.#storageManager.getVal("wispServer") } { wisp: wispServer() }
]); ]);
} }
} }
} }
async wispServer(wispServer?: string, set?: true) { async wispServer(wispServer?: string, set?: true) {
console.log(wispServer?.replace("/wisp/", "/adblock/"));
this.#storageManager.setVal( this.#storageManager.setVal(
"wispServer", "wispServer",
wispServer || wispServer ||

View file

@ -122,6 +122,15 @@ class Settings {
} }
} }
adBlock(enabled?: boolean) {
if (enabled === true || enabled === false) {
this.#storageManager.setVal("adBlock", enabled.valueOf().toString());
}
else {
this.#storageManager.setVal("adBlock", "true");
}
}
async *#init() { async *#init() {
yield this.theme(this.#storageManager.getVal("theme") || "default"); yield this.theme(this.#storageManager.getVal("theme") || "default");
} }