|
| 1 | +import Fastify from 'fastify'; |
| 2 | +import fastifyMiddie from '@fastify/middie'; |
| 3 | +import fastifyHttpProxy from '@fastify/http-proxy'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { createBareServer } from '@tomphttp/bare-server-node'; |
| 6 | +import createRammerhead from "rammerhead/src/server/index.js"; |
| 7 | +import { createServer } from 'http'; |
| 8 | +import fs from 'fs' |
| 9 | +import YAML from 'yaml'; |
| 10 | +import path from 'path'; |
| 11 | +const __dirname = path.resolve(); |
| 12 | +const settings = YAML.parse(fs.readFileSync(path.join(__dirname, '/config/settings.yml'), 'utf8')); |
| 13 | +import chalk from 'chalk'; |
| 14 | +import compile from './compile.js'; |
| 15 | +//import getLatestRelease from './version.js'; |
| 16 | +let rubyPort = process.argv.find((arg) => arg.startsWith('--ruby-port')).split('=')[1] || 9292; |
| 17 | +let nodePort = process.argv.find((arg) => arg.startsWith('--node-port')).split('=')[1] || 9293; |
| 18 | + |
| 19 | +const latestRelease = 'dev'; |
| 20 | +const bare = createBareServer('/bare/'); |
| 21 | +const rh = createRammerhead(); |
| 22 | +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" ]; |
| 23 | +const rammerheadSession = /^\/[a-z0-9]{32}/; |
| 24 | +function shouldRouteRh(req) { |
| 25 | + const url = new URL(req.url, "http://0.0.0.0"); |
| 26 | + return (rammerheadScopes.includes(url.pathname) || rammerheadSession.test(url.pathname)); |
| 27 | +} |
| 28 | +function routeRhRequest(req, res) { rh.emit("request", req, res) } |
| 29 | +function routeRhUpgrade(req, socket, head) { rh.emit("upgrade", req, socket, head) } |
| 30 | +console.log(chalk.red('Compiling...')) |
| 31 | +compile(); |
| 32 | + |
| 33 | +const proxyHandler = (handler, opts) => { |
| 34 | + return createServer().on('request', (req, res) => { |
| 35 | + if (bare.shouldRoute(req)) { |
| 36 | + bare.routeRequest(req, res); |
| 37 | + } |
| 38 | + else if (shouldRouteRh(req)) { |
| 39 | + routeRhRequest(req, res); |
| 40 | + } |
| 41 | + else { |
| 42 | + handler(req, res); |
| 43 | + } |
| 44 | + }) |
| 45 | + .on('upgrade', (req, socket, head) => { |
| 46 | + if (bare.shouldRoute(req)) { |
| 47 | + bare.routeUpgrade(req, socket, head); |
| 48 | + } |
| 49 | + else if (shouldRouteRh(req)) { |
| 50 | + routeRhUpgrade(req, socket, head); |
| 51 | + } |
| 52 | + }); |
| 53 | +}; |
| 54 | + |
| 55 | +const app = Fastify({ logger: false, serverFactory: proxyHandler }) |
| 56 | +await app |
| 57 | + .register(fastifyHttpProxy, { |
| 58 | + upstream: 'http://localhost:9292', |
| 59 | + prefix: '/', |
| 60 | + http2: false, |
| 61 | + replyOptions: { |
| 62 | + rewriteRequestHeaders: (originalReq, headers) => { |
| 63 | + headers['host'] = originalReq.headers['host']; |
| 64 | + headers['origin'] = originalReq.headers['origin']; |
| 65 | + return headers; |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + .register(fastifyHttpProxy, { |
| 70 | + upstream: 'https://rawcdn.githack.com', |
| 71 | + prefix: '/gms/', |
| 72 | + http2: false, |
| 73 | + }) |
| 74 | + .register(fastifyMiddie) |
| 75 | +app.get('/search=:query', async (req, res) => { |
| 76 | + const { query } = req.params; |
| 77 | + try { |
| 78 | + const resp = await fetch(`https://search.brave.com/api/suggest?q=${query}&format=json`).then((res) => res.json()); |
| 79 | + res.send(resp); |
| 80 | + } |
| 81 | + catch (err) { |
| 82 | + reply.code(500).send({ error: "Internal Server Error" }); |
| 83 | + } |
| 84 | +}); |
| 85 | +app.get('/version', async (req, res) => { |
| 86 | + res.send({ version: latestRelease }); |
| 87 | +}); |
| 88 | + |
| 89 | +app.listen({ port: nodePort, host: '0.0.0.0' }); |
| 90 | +console.log(chalk.green(`Server listening on port ${chalk.red(nodePort)}`)); |
0 commit comments