From 1f167e11df2535819a9d595f5302438fa52625b5 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 13 Aug 2021 22:57:46 +0200 Subject: :construction: fix server bundle --- api/src/middlewares/TestClient.ts | 21 ++++++++++++++------- api/src/routes/users/@me/devices.ts | 10 ++++++++++ bundle/.vscode/launch.json | 18 ++++++++++++++++++ bundle/package-lock.json | 22 +++++++++------------- bundle/package.json | 2 +- bundle/src/Server.ts | 13 ++++++++++--- fosscord-server.code-workspace | 29 +++++++++++++++++++++++++++++ gateway/src/Server.ts | 2 ++ gateway/src/listener/listener.ts | 13 +++++++------ util/src/util/Config.ts | 8 ++++---- util/src/util/Event.ts | 7 ++++++- 11 files changed, 110 insertions(+), 35 deletions(-) create mode 100644 api/src/routes/users/@me/devices.ts create mode 100644 bundle/.vscode/launch.json create mode 100644 fosscord-server.code-workspace diff --git a/api/src/middlewares/TestClient.ts b/api/src/middlewares/TestClient.ts index f93d0b3a..75fdf650 100644 --- a/api/src/middlewares/TestClient.ts +++ b/api/src/middlewares/TestClient.ts @@ -8,6 +8,20 @@ export default function TestClient(app: Application) { const assetCache = new Map(); const indexHTML = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" }); + var html = indexHTML; + const CDN_ENDPOINT = (Config.get().cdn.endpointClient || Config.get()?.cdn.endpoint || process.env.CDN || "").replace( + /(https?)?(:\/\/?)/g, + "" + ); + const GATEWAY_ENDPOINT = Config.get().gateway.endpointClient || Config.get()?.gateway.endpoint || process.env.GATEWAY || ""; + + if (CDN_ENDPOINT) { + html = html.replace(/CDN_HOST: .+/, `CDN_HOST: \`${CDN_ENDPOINT}\`,`); + } + if (GATEWAY_ENDPOINT) { + html = html.replace(/GATEWAY_ENDPOINT: .+/, `GATEWAY_ENDPOINT: \`${GATEWAY_ENDPOINT}\`,`); + } + app.use("/assets", express.static(path.join(__dirname, "..", "assets"))); app.get("/assets/:file", async (req: Request, res: Response) => { @@ -52,13 +66,6 @@ export default function TestClient(app: Application) { app.get("*", (req: Request, res: Response) => { res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); res.set("content-type", "text/html"); - var html = indexHTML; - const CDN_ENDPOINT = (Config.get()?.cdn.endpoint || process.env.CDN || "").replace(/(https?)?(:\/\/?)/g, ""); - const GATEWAY_ENDPOINT = Config.get()?.gateway.endpoint || process.env.GATEWAY || ""; - - if (CDN_ENDPOINT && Config.get().cdn.endpointClient) html = html.replace(/CDN_HOST: .+/, `CDN_HOST: "${CDN_ENDPOINT}",`); - if (GATEWAY_ENDPOINT && Config.get().gateway.endpointClient) - html = html.replace(/GATEWAY_ENDPOINT: .+/, `GATEWAY_ENDPOINT: "${GATEWAY_ENDPOINT}",`); res.send(html); }); diff --git a/api/src/routes/users/@me/devices.ts b/api/src/routes/users/@me/devices.ts new file mode 100644 index 00000000..b16ef783 --- /dev/null +++ b/api/src/routes/users/@me/devices.ts @@ -0,0 +1,10 @@ +import { Router, Response, Request } from "express"; + +const router = Router(); + +router.post("/", (req: Request, res: Response) => { + // TODO: + res.sendStatus(204); +}); + +export default router; diff --git a/bundle/.vscode/launch.json b/bundle/.vscode/launch.json new file mode 100644 index 00000000..0b5d2287 --- /dev/null +++ b/bundle/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "sourceMaps": true, + "type": "node", + "request": "launch", + "name": "Launch server bundle", + "program": "${workspaceFolder}/dist/start.js", + "preLaunchTask": "tsc: build - tsconfig.json", + "outFiles": ["${workspaceFolder}/dist/**/*.js"], + "envFile": "${workspaceFolder}/.env" + } + ] +} diff --git a/bundle/package-lock.json b/bundle/package-lock.json index 218c1a55..a0c8f4e6 100644 --- a/bundle/package-lock.json +++ b/bundle/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "@fosscord/server", "version": "1.0.0", + "hasInstallScript": true, "license": "AGPLV3", "dependencies": { "@fosscord/api": "file:../api", @@ -28,7 +29,6 @@ "../api": { "name": "@fosscord/api", "version": "1.0.0", - "hasInstallScript": true, "license": "ISC", "dependencies": { "@fosscord/util": "file:../util", @@ -85,7 +85,6 @@ "../cdn": { "name": "@fosscord/cdn", "version": "1.0.0", - "hasInstallScript": true, "license": "ISC", "dependencies": { "@fosscord/util": "file:../util", @@ -121,7 +120,6 @@ "../gateway": { "name": "@fosscord/gateway", "version": "1.0.0", - "hasInstallScript": true, "license": "ISC", "dependencies": { "@fosscord/util": "file:../util", @@ -152,7 +150,13 @@ "version": "1.3.55", "hasInstallScript": true, "license": "GPLV3", - "dependencies": { + "devDependencies": { + "@types/amqplib": "^0.8.1", + "@types/jsonwebtoken": "^8.5.0", + "@types/mongodb": "^3.6.9", + "@types/mongoose-autopopulate": "^0.10.1", + "@types/mongoose-lean-virtuals": "^0.5.1", + "@types/node": "^14.17.9", "ajv": "^8.5.0", "amqplib": "^0.8.0", "dot-prop": "^6.0.1", @@ -161,15 +165,7 @@ "missing-native-js-functions": "^1.2.2", "mongodb": "^3.6.9", "mongoose": "^5.13.7", - "mongoose-autopopulate": "^0.12.3" - }, - "devDependencies": { - "@types/amqplib": "^0.8.1", - "@types/jsonwebtoken": "^8.5.0", - "@types/mongodb": "^3.6.9", - "@types/mongoose-autopopulate": "^0.10.1", - "@types/mongoose-lean-virtuals": "^0.5.1", - "@types/node": "^14.17.9", + "mongoose-autopopulate": "^0.12.3", "typescript": "^4.1.3" } }, diff --git a/bundle/package.json b/bundle/package.json index 56ef9591..be584631 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -4,7 +4,7 @@ "description": "", "main": "src/start.js", "scripts": { - "postinstall": "cd ../api/ && npm i && cd ../cdn/ && npm i && cd ../gateway/ && npm i && ", + "postinstall": "cd ../api/ && npm i && cd ../cdn/ && npm i && cd ../gateway/ && npm i", "build": "npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle", "build:bundle": "tsc -b .", "build:api": "cd ../api/ && npm run build", diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts index 68966bf4..6951c1c4 100644 --- a/bundle/src/Server.ts +++ b/bundle/src/Server.ts @@ -9,9 +9,10 @@ import express from "express"; import { Config } from "../../util/dist"; const app = express(); -const server = http.createServer(app); +const server = http.createServer(); const port = Number(process.env.PORT) || 8080; const production = true; +server.on("request", app); // @ts-ignore const api = new APIServer({ server, port, production, app }); @@ -22,8 +23,14 @@ const gateway = new GatewayServer({ server, port, production }); async function main() { await Config.set({ - cdn: { endpointClientKeepDefault: true, endpoint: `http://localhost:${port}` }, - gateway: { endpointClientKeepDefault: true, endpoint: `ws://localhost:${port}` }, + cdn: { + endpointClient: "${location.host}", + endpoint: `http://localhost:${port}`, + }, + gateway: { + endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}', + endpoint: `ws://localhost:${port}`, + }, }); await api.start(); diff --git a/fosscord-server.code-workspace b/fosscord-server.code-workspace new file mode 100644 index 00000000..645dcd76 --- /dev/null +++ b/fosscord-server.code-workspace @@ -0,0 +1,29 @@ +{ + "folders": [ + { + "path": "api" + }, + { + "path": "dashboard" + }, + { + "path": "bundle" + }, + { + "path": "cdn" + }, + { + "path": "gateway" + }, + { + "path": "rtc" + }, + { + "path": "util" + }, + { + "path": "webrtc" + } + ], + "settings": {} +} \ No newline at end of file diff --git a/gateway/src/Server.ts b/gateway/src/Server.ts index c2bc9c47..a50c24a6 100644 --- a/gateway/src/Server.ts +++ b/gateway/src/Server.ts @@ -23,6 +23,8 @@ export class Server { }); this.server.on("upgrade", (request, socket, head) => { + console.log("socket requests upgrade", request.url); + // @ts-ignore this.ws.handleUpgrade(request, socket, head, (socket) => { this.ws.emit("connection", socket, request); }); diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts index 708bfe5c..51082586 100644 --- a/gateway/src/listener/listener.ts +++ b/gateway/src/listener/listener.ts @@ -58,13 +58,14 @@ export async function setupListener(this: WebSocket) { this.permissions[guild] = x; this.listeners; this.events[guild] = await listenEvent(guild, consumer, opts); - for (const channel of guild_channels) { + + for (const channel of guild_channels.filter((c) => c.guild_id === guild)) { if (x.overwriteChannel(channel.permission_overwrites).has("VIEW_CHANNEL")) { this.events[channel.id] = await listenEvent(channel.id, consumer, opts); } } }) - .catch((e) => {}); + .catch((e) => console.log("couldn't get permission for guild " + guild, e)); } this.once("close", () => { @@ -74,14 +75,14 @@ export async function setupListener(this: WebSocket) { } // TODO: only subscribe for events that are in the connection intents -function consume(this: WebSocket, opts: EventOpts) { +async function consume(this: WebSocket, opts: EventOpts) { const { data, event } = opts; const id = data.id as string; const permission = this.permissions[id] || new Permissions("ADMINISTRATOR"); // default permission for dm const consumer = consume.bind(this); const listenOpts = opts as ListenEventOpts; - console.log("event", event); + // console.log("event", event); // subscription managment switch (event) { @@ -94,14 +95,14 @@ function consume(this: WebSocket, opts: EventOpts) { if (!permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) return; // TODO: check if user has permission to channel case "GUILD_CREATE": - listenEvent(id, consumer, listenOpts); + this.events[id] = await listenEvent(id, consumer, listenOpts); break; case "CHANNEL_UPDATE": const exists = this.events[id]; // @ts-ignore if (permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) { if (exists) break; - listenEvent(id, consumer, listenOpts); + this.events[id] = await listenEvent(id, consumer, listenOpts); } else { if (!exists) return; // return -> do not send channel update events for hidden channels opts.cancel(id); diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index a23d724d..09a42564 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -43,11 +43,11 @@ export interface KafkaBroker { export interface DefaultOptions { gateway: { - endpointClientKeepDefault?: boolean; + endpointClient: string | null; endpoint: string | null; }; cdn: { - endpointClientKeepDefault?: boolean; + endpointClient: string | null; endpoint: string | null; }; general: { @@ -150,11 +150,11 @@ export interface DefaultOptions { export const DefaultOptions: DefaultOptions = { gateway: { - endpointClientKeepDefault: false, + endpointClient: null, endpoint: null, }, cdn: { - endpointClientKeepDefault: false, + endpointClient: null, endpoint: null, }, general: { diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts index 13dd797a..c088e8b3 100644 --- a/util/src/util/Event.ts +++ b/util/src/util/Event.ts @@ -16,6 +16,7 @@ export async function emitEvent(payload: Omit) { const successful = RabbitMQ.channel?.publish(id, "", Buffer.from(`${data}`), { type: payload.event }); if (!successful) throw new Error("failed to send event"); } else { + console.log("emit event", id); events.emit(id, payload); } } @@ -44,7 +45,11 @@ export async function listenEvent(event: string, callback: (event: EventOpts) => // @ts-ignore return rabbitListen(opts?.channel || RabbitMQ.channel, event, callback, { acknowledge: opts?.acknowledge }); } else { - const cancel = () => events.removeListener(event, callback); + const cancel = () => { + console.log("cancel event", event); + events.removeListener(event, callback); + }; + console.log("listen event", event); events.addListener(event, (opts) => callback({ ...opts, cancel })); return cancel; -- cgit 1.4.1