forked from DogeNetwork/dogeub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (104 loc) · 3.54 KB
/
server.js
File metadata and controls
121 lines (104 loc) · 3.54 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
import dotenv from "dotenv";
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import fastifyCookie from "@fastify/cookie";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createServer } from "node:http";
import { logging, server as wisp } from "@mercuryworkshop/wisp-js/server";
import { createBareServer } from "@tomphttp/bare-server-node";
import { MasqrMiddleware } from "./masqr.js";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const port = process.env.PORT || 2345;
const server = createServer();
const bare = process.env.BARE !== "false" ? createBareServer("/seal/") : null;
logging.set_level(logging.NONE);
Object.assign(wisp.options, {
dns_method: "resolve",
dns_servers: ["1.1.1.3", "1.0.0.3"],
dns_result_order: "ipv4first"
});
server.on("upgrade", (req, sock, head) =>
bare?.shouldRoute(req)
? bare.routeUpgrade(req, sock, head)
: req.url.endsWith("/wisp/")
? wisp.routeRequest(req, sock, head)
: sock.end()
);
const app = Fastify({
serverFactory: h => (
server.on("request", (req, res) =>
bare?.shouldRoute(req) ? bare.routeRequest(req, res) : h(req, res)
),
server
),
logger: false,
keepAliveTimeout: 30000,
connectionTimeout: 60000,
forceCloseConnections: true
});
await app.register(fastifyCookie);
app.register(fastifyStatic, {
root: join(__dirname, "dist"),
prefix: "/",
decorateReply: true,
etag: true,
lastModified: true,
cacheControl: true,
setHeaders(res, path) {
if (path.endsWith(".html")) {
res.setHeader("Cache-Control", "no-cache, must-revalidate");
} else if (/\.[a-f0-9]{8,}\./.test(path)) {
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
} else {
res.setHeader("Cache-Control", "public, max-age=3600");
}
}
});
if (process.env.MASQR === "true")
app.addHook("onRequest", MasqrMiddleware);
const proxy = (url, type = "application/javascript") => async (req, reply) => {
try {
const res = await fetch(url(req));
if (!res.ok) return reply.code(res.status).send();
const hop = [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailer",
"transfer-encoding",
"upgrade"
];
for (const [k, v] of res.headers) {
if (!hop.includes(k.toLowerCase())) reply.header(k, v);
}
if (res.headers.getSetCookie) {
const cookies = res.headers.getSetCookie();
if (cookies.length) reply.header("set-cookie", cookies);
}
if (!res.headers.get("content-type")) reply.type(type);
return reply.send(res.body);
} catch {
return reply.code(500).send();
}
};
app.get("/assets/img/*", proxy(req => `https://dogeub-assets.pages.dev/img/${req.params["*"]}`, ""));
app.get("/js/script.js", proxy(() => "https://byod.privatedns.org/js/script.js"));
app.get("/ds", (req, res) => res.redirect("https://discord.gg/ZBef7HnAeg"));
app.get("/return", async (req, reply) =>
req.query?.q
? fetch(`https://duckduckgo.com/ac/?q=${encodeURIComponent(req.query.q)}`)
.then(r => r.json())
.catch(() => reply.code(500).send({ error: "request failed" }))
: reply.code(401).send({ error: "query parameter?" })
);
app.setNotFoundHandler((req, reply) =>
req.raw.method === "GET" && req.headers.accept?.includes("text/html")
? reply.sendFile("index.html")
: reply.code(404).send({ error: "Not Found" })
);
app.listen({ port }).then(() => console.log(`Server running on ${port}`));