forked from NebulaServices/Noctura
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
123 lines (104 loc) · 3.08 KB
/
index.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
import Fastify from 'fastify';
import { createServer } from 'http';
import { fileURLToPath } from 'url';
import { join } from 'path';
import { createBareServer } from '@tomphttp/bare-server-node';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
import fastifyStatic from "@fastify/static";
import createRammerhead from 'rammerhead/src/server/index.js';
import { chromium } from 'playwright';
// airplane
// if anyone can figure out how to unfuck fastify not working on some things that would be great, ideally we want to use it over express whenever we can.
const bare = createBareServer("/bare/");
const rh = createRammerhead();
// used when forwarding the script
// rammerhead hell
const rammerheadScopes = [
'/rammerhead.js',
'/hammerhead.js',
'/transport-worker.js',
'/task.js',
'/iframe-task.js',
'/worker-hammerhead.js',
'/messaging',
'/sessionexists',
'/deletesession',
'/newsession',
'/editsession',
'/needpassword',
'/syncLocalStorage',
'/api/shuffleDict',
'/mainport'
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
function shouldRouteRh(req) {
const url = new URL(req.url, 'http://0.0.0.0');
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
}
function routeRhRequest(req, res) {
rh.emit('request', req, res);
}
function routeRhUpgrade(req, socket, head) {
rh.emit('upgrade', req, socket, head);
}
const serverFactory = (handler, opts) => {
return createServer()
.on("request", (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else if (shouldRouteRh(req)) {
routeRhRequest(req, res);
} else {
handler(req, res)
}
})
.on("upgrade", (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else if (shouldRouteRh(req)) {
routeRhUpgrade(req, socket, head);
}
});
}
const fastify = Fastify({ logger: true, serverFactory });
fastify.register(import("@fastify/compress"));
fastify.register(fastifyStatic, {
root: join(fileURLToPath(import.meta.url), "../dist"),
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: uvPath,
prefix: "/uv/",
decorateReply: false
});
fastify.get("/search=:query", async (req, res) => {
const { query } = req.params;
try {
const response = await fetch(`http://api.duckduckgo.com/ac?q=${query}&format=json`)
.then((res) => res.json());
res.send(response);
} catch {
reply.code(500).send({ error: 'Internal Server Error' });
}
});
let promise = chromium.launch().then(browser => {
return browser.newPage();
}).then(page => (page.setViewportSize({ width: 1440, height: 796 }), page));
fastify.get("/gen-scrot", async (req, res) => {
const { url } = req.query;
let page = await promise;
try {
await page.goto(decodeURIComponent(url));
let buffer = await page.screenshot();
return res.status(200).headers({ 'content-type': 'image/png' }).send(buffer);
} catch(e) {
console.log(e);
res.status(500).send({ error: 'Internal Server Error' });
}
});
fastify.listen({
port: 8080,
});