This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: WISP SUPPORT and a couple other improvements
- Loading branch information
1 parent
69579ee
commit c565ff4
Showing
23 changed files
with
245 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#!/usr/bin/env tsx | ||
import { createServer, IncomingMessage } from "node:http"; | ||
import { createServer } from "node:http"; | ||
import { createServer as createViteServer } from "vite"; | ||
import { createBareServer } from "@tomphttp/bare-server-node"; | ||
import express from "express"; | ||
import path from "path"; | ||
import cors from "cors"; | ||
|
@@ -11,7 +10,6 @@ import { Socket } from "node:net"; | |
import wisp from "wisp-server-node"; | ||
const devMode = argv.includes("--dev"); | ||
const usingMasqr = process.env.MASQR || argv.includes("--masqr"); | ||
const noIpLeak = argv.includes("--no-ip-leak"); | ||
const port = | ||
process.env.PORT || | ||
(argv.includes("--port") && argv[argv.indexOf("--port") + 1]) || | ||
|
@@ -26,18 +24,9 @@ default: Run in production mode | |
--dev: Run in development mode | ||
--help, -h: Display this help message | ||
--masqr: Enable masqr | ||
--no-ip-leak: only allow going to asdf.com | ||
`); | ||
process.exit(0); | ||
} | ||
const bare = createBareServer("/bend/", { | ||
blockLocal: false, | ||
maintainer: { | ||
website: "https://z1g-project.vercel.app", | ||
// todo: change this | ||
email: process.env.MAINTAINER_EMAIL || "[email protected]", | ||
}, | ||
}); | ||
const vite = await createViteServer({ | ||
server: { middlewareMode: true }, | ||
}); | ||
|
@@ -68,36 +57,15 @@ if (!devMode) { | |
} | ||
|
||
const server = createServer(); | ||
server.on("request", (request, response) => { | ||
if (bare.shouldRoute(request)) { | ||
if (noIpLeak) { | ||
if ( | ||
request.headers["x-bare-host"] && | ||
request.headers["x-bare-host"] != "asdf.com" | ||
) { | ||
console.log(request.headers); | ||
request.socket.write( | ||
'HTTP/1.1 406 Not Acceptable\n\n"NO LEAKING MY IP!!! GO TO ASDF.COM NOW"', | ||
); | ||
request.socket.end(); | ||
} | ||
} | ||
bare.routeRequest(request, response); | ||
} else { | ||
app(request, response); | ||
} | ||
}); | ||
server.on("request", app); | ||
|
||
server.on("upgrade", (req: IncomingMessage, socket: Socket, head) => { | ||
if (bare.shouldRoute(req)) { | ||
bare.routeUpgrade(req, socket, head); | ||
} else if (req.url.endsWith("/wisp/")) { | ||
server.on("upgrade", (req, socket: Socket, head) => { | ||
if (req.url.endsWith("/wisp/")) { | ||
wisp.routeRequest(req, socket, head); | ||
} | ||
}); | ||
server.listen(port); | ||
console.log(` | ||
\x1b[34;49;1m[Ephemeral] \x1B[32mINFO: Running on port ${port} in ${devMode ? "dev" : "production"} mode | ||
Configured with Masqr: ${masqr} | ||
Configured with IP Leak Protection: ${noIpLeak} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,12 @@ | ||
/*global UVServiceWorker importScripts __uv$config localforage */ | ||
/*global UVServiceWorker importScripts __uv$config */ | ||
importScripts("/epoxy/index.js"); | ||
importScripts("/libcurl/index.js"); | ||
importScripts("/uv/uv.bundle.js"); | ||
importScripts("/uv/uv.config.js"); | ||
importScripts("/uv/uv.sw.js"); | ||
importScripts("/localforage/localforage.min.js"); | ||
localforage.config({ | ||
driver: localforage.INDEXEDDB, | ||
name: "ephemeral", | ||
storeName: "__ephemeral_config", | ||
}); | ||
const setUv = async () => { | ||
try { | ||
const bare = | ||
(await localforage.getItem("proxy.bareServer")) || | ||
location.origin + "/bend/"; | ||
self.__uv$config.bare = bare; | ||
self.uv = new UVServiceWorker(self.__uv$config); | ||
} catch (error) { | ||
console.error( | ||
"\x1b[34;49;1m[Ephemeral] \x1B[31mERROR: Settings for Ultraviolet cannot be set (self.uv) " + | ||
error, | ||
); | ||
} | ||
}; | ||
const uv = new UVServiceWorker(); | ||
self.addEventListener("fetch", (event) => { | ||
if (event.request.url.startsWith(location.origin + __uv$config.prefix)) { | ||
event.respondWith( | ||
(async () => { | ||
await setUv().catch((error) => { | ||
console.error( | ||
"\x1b[34;49;1m[Ephemeral] \x1B[31mERROR: Settings for Ultraviolet cannot be set (event.respondWith) " + | ||
error, | ||
); | ||
}); | ||
return await self.uv.fetch(event); | ||
})(), | ||
); | ||
event.respondWith(uv.fetch(event)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.