Updates
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
FROM node:20-bookworm
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
curl zip unzip ca-certificates docker.io rsync openssh-client \
|
||||
zlib1g wget python3 python3-pip make build-essential \
|
||||
mariadb-client xz-utils
|
||||
|
||||
RUN update-ca-certificates
|
||||
|
||||
RUN curl -fsSL https://bun.sh/install | bash -s "bun-v1.2.0"
|
||||
ENV PATH="/root/.bun/bin:${PATH}"
|
||||
|
||||
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/entrypoint.sh
|
||||
|
||||
ENTRYPOINT ["entrypoint.sh"]
|
||||
@@ -0,0 +1,34 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import { _n } from "@/client-exports";
|
||||
import sendData from "../../../utils/send-data";
|
||||
import getQueue from "@/package-shared/functions/backend/queues/get-queue";
|
||||
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "@/package-shared/types/dsql";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
};
|
||||
|
||||
export default async function checkQueue({ ws }: Param) {
|
||||
try {
|
||||
const user = ws.data.user;
|
||||
const queue = (await getQueue({
|
||||
userId: user.id,
|
||||
single: true,
|
||||
})) as DSQL_DATASQUIREL_PROCESS_QUEUE | undefined;
|
||||
|
||||
sendData(ws, {
|
||||
event: "server:queue",
|
||||
data: {
|
||||
queue,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
sendData(ws, {
|
||||
event: "server:queue",
|
||||
data: {
|
||||
queue: undefined,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import { _n } from "@/client-exports";
|
||||
import sendData from "../../../utils/send-data";
|
||||
import { WebSocketDataType } from "@/types";
|
||||
import deleteQueue from "@/package-shared/functions/backend/queues/delete-queue";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
data?: WebSocketDataType;
|
||||
};
|
||||
|
||||
export default async function webSocketDeleteQueue({ ws, data }: Param) {
|
||||
try {
|
||||
const user = ws.data.user;
|
||||
|
||||
await deleteQueue({
|
||||
queueId: _n(data?.data?.queue?.id),
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
await Bun.sleep(2000);
|
||||
|
||||
sendData(ws, {
|
||||
event: "server:queue-deleted",
|
||||
});
|
||||
} catch (error: any) {}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import { _n } from "@/client-exports";
|
||||
import sendData from "@WS/utils/send-data";
|
||||
import addQueue from "@/package-shared/functions/backend/queues/add-queue";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
};
|
||||
|
||||
export default async function webSocketSendDummyQueue({ ws }: Param) {
|
||||
try {
|
||||
console.log("Sending Dummy Queue ...");
|
||||
|
||||
const user = ws.data.user;
|
||||
const dummyQueue = await addQueue({
|
||||
queue: {
|
||||
job_type: "dummy",
|
||||
user_id: user.id,
|
||||
title: "Running Dummy Queue ...",
|
||||
},
|
||||
userId: user.id,
|
||||
dummy: true,
|
||||
});
|
||||
|
||||
await Bun.sleep(2000);
|
||||
|
||||
sendData(ws, {
|
||||
event: "server:dev:queue",
|
||||
});
|
||||
} catch (error: any) {
|
||||
sendData(ws, {
|
||||
event: "server:dev:queue",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { type ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
};
|
||||
|
||||
export default async function socketClose({ ws }: Param) {
|
||||
const user = ws.data.user;
|
||||
console.log(`Web Closed by ${user.first_name}`);
|
||||
|
||||
const userSessionIndex = global.ACTIVE_USERS.findIndex(
|
||||
(session) => session.id == user.id
|
||||
);
|
||||
|
||||
if (userSessionIndex >= 0) {
|
||||
global.ACTIVE_USERS.splice(userSessionIndex, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import type { DATASQUIREL_LoggedInUser } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
|
||||
type Param = {
|
||||
req: Request;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
user: DATASQUIREL_LoggedInUser | null;
|
||||
};
|
||||
|
||||
export default async function socketInit({
|
||||
req,
|
||||
debug,
|
||||
}: Param): Promise<Return> {
|
||||
const cookieString = req.headers.get("Cookie") || undefined;
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::socketInit:cookieString", cookieString);
|
||||
}
|
||||
|
||||
if (!cookieString)
|
||||
return {
|
||||
user: null,
|
||||
};
|
||||
|
||||
const user = datasquirel.user.userAuth({
|
||||
cookieString,
|
||||
database: process.env.DB_NAME || "",
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::socketInit:user", user);
|
||||
}
|
||||
|
||||
return { user: user.payload };
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import { EJSON } from "@/client-exports";
|
||||
import checkQueue from "./events/client-requests/check-queue";
|
||||
import webSocketSendDummyQueue from "./events/client-requests/send-dummy-queue";
|
||||
import webSocketDeleteQueue from "./events/client-requests/delete-queue";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
message: string | Buffer;
|
||||
};
|
||||
|
||||
export type WebSocketMessageParam = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
message?: string | Buffer;
|
||||
data?: WebSocketDataType;
|
||||
};
|
||||
|
||||
export default async function socketMessage({ ws, message }: Param) {
|
||||
const user = ws.data.user;
|
||||
const data = EJSON.parse(message.toString()) as
|
||||
| WebSocketDataType
|
||||
| undefined;
|
||||
|
||||
const websocketMessageParams: WebSocketMessageParam = {
|
||||
ws,
|
||||
data,
|
||||
message,
|
||||
};
|
||||
|
||||
switch (data?.event) {
|
||||
/**
|
||||
* Check Queue
|
||||
*/
|
||||
case "client:check-queue":
|
||||
checkQueue({ ws });
|
||||
break;
|
||||
/**
|
||||
* Send Dummy Queue
|
||||
*/
|
||||
case "client:dev:queue":
|
||||
webSocketSendDummyQueue({ ws });
|
||||
break;
|
||||
/**
|
||||
* Delete Queue
|
||||
*/
|
||||
case "client:delete-queue":
|
||||
webSocketDeleteQueue({ ws, data });
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
};
|
||||
|
||||
export default async function socketOpen({ ws }: Param) {
|
||||
const user = ws.data.user;
|
||||
console.log(`Web Socket Opened by ${user.first_name}`);
|
||||
global.ACTIVE_USERS.push({ ...user, ws });
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { type ServerWebSocket, type Subprocess } from "bun";
|
||||
import socketInit from "@WS/functions/socket-init";
|
||||
import type { DATASQUIREL_LoggedInUser } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import socketOpen from "@WS/functions/socket-open";
|
||||
import socketClose from "@WS/functions/socket-close";
|
||||
import socketMessage from "@WS/functions/socket-message";
|
||||
import setupDSQLDb from "@/utils/setup-db";
|
||||
import debugLog from "@/package-shared/utils/logging/debug-log";
|
||||
|
||||
setupDSQLDb();
|
||||
|
||||
export type WebSocketData = {
|
||||
user: DATASQUIREL_LoggedInUser;
|
||||
};
|
||||
|
||||
declare global {
|
||||
var ACTIVE_USERS: (DATASQUIREL_LoggedInUser & {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
})[];
|
||||
}
|
||||
|
||||
global.ACTIVE_USERS = [];
|
||||
|
||||
const server = Bun.serve<WebSocketData>({
|
||||
async fetch(req, server) {
|
||||
const { user } = await socketInit({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return new Response("Unauthorized!");
|
||||
}
|
||||
|
||||
const success = server.upgrade(req, {
|
||||
data: { user },
|
||||
});
|
||||
|
||||
if (success) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return new Response("Web Socket Connection Failed!");
|
||||
},
|
||||
websocket: {
|
||||
async message(ws, message) {
|
||||
socketMessage({ ws, message });
|
||||
},
|
||||
async open(ws) {
|
||||
socketOpen({ ws });
|
||||
},
|
||||
async close(ws, code, message) {
|
||||
socketClose({ ws });
|
||||
},
|
||||
idleTimeout: 600,
|
||||
},
|
||||
port: process.env.DSQL_WEBSOCKET_PORT,
|
||||
});
|
||||
|
||||
console.log(`Websocket Listening on http://${server.hostname}:${server.port}`);
|
||||
@@ -0,0 +1,50 @@
|
||||
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
|
||||
import { execSync, type ExecSyncOptions } from "child_process";
|
||||
|
||||
type Param = {
|
||||
filePath: string;
|
||||
debug?: boolean;
|
||||
server?: any;
|
||||
options?: ExecSyncOptions;
|
||||
};
|
||||
|
||||
export default function bunExecSSH({
|
||||
filePath,
|
||||
debug,
|
||||
server,
|
||||
options,
|
||||
}: Param): string | undefined {
|
||||
try {
|
||||
let cmdPrefix = grabCoderankSSHPrefix();
|
||||
|
||||
let finalCmd = `${cmdPrefix}`;
|
||||
|
||||
if (server) {
|
||||
finalCmd += ` ${server.username}@${server.ip}`;
|
||||
}
|
||||
|
||||
finalCmd += ` bun < ${filePath}`;
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::", finalCmd);
|
||||
}
|
||||
|
||||
const str = execSync(finalCmd, {
|
||||
stdio: "pipe",
|
||||
...options,
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::", str);
|
||||
}
|
||||
|
||||
return str.trim();
|
||||
} catch (error: any) {
|
||||
if (debug) {
|
||||
console.log(`DEBUG::: Raw SSh Error: ${error.message}`);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Client, type ConnectConfig } from "ssh2";
|
||||
|
||||
type Param = {
|
||||
config: ConnectConfig;
|
||||
};
|
||||
|
||||
export default async function connectSSH({
|
||||
config,
|
||||
}: Param): Promise<Client | undefined> {
|
||||
return new Promise((resolve) => {
|
||||
const ssh = new Client();
|
||||
|
||||
ssh.on("ready", () => {
|
||||
resolve(ssh);
|
||||
}).connect(config);
|
||||
|
||||
ssh.on("error", (err) => {
|
||||
console.log(`SHH connect ERROR: ${err.message}`);
|
||||
resolve(undefined);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
|
||||
import { exec, execSync, type ExecSyncOptions } from "child_process";
|
||||
import debugLog from "@moduletrace/datasquirel/dist/package-shared/utils/logging/debug-log";
|
||||
|
||||
type Param = {
|
||||
cmd: string;
|
||||
debug?: boolean;
|
||||
server?: any;
|
||||
options?: ExecSyncOptions;
|
||||
detached?: boolean;
|
||||
};
|
||||
|
||||
export default function execRawSSH({
|
||||
cmd,
|
||||
debug,
|
||||
server,
|
||||
options,
|
||||
detached,
|
||||
}: Param): string | undefined {
|
||||
function debugFn(log: any, label?: string) {
|
||||
debugLog({
|
||||
log: log,
|
||||
label: label,
|
||||
title: "execRawSSH",
|
||||
addTime: true,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
let cmdPrefix = grabCoderankSSHPrefix();
|
||||
|
||||
let finalCmd = `${cmdPrefix}`;
|
||||
|
||||
if (server) {
|
||||
finalCmd += ` ${server.username}@${server.ip}`;
|
||||
}
|
||||
|
||||
finalCmd += ` << 'CDREXEC' \n\
|
||||
${cmd}\n\
|
||||
CDREXEC`;
|
||||
|
||||
if (debug) {
|
||||
debugFn(finalCmd, "finalCmd");
|
||||
}
|
||||
|
||||
const str = detached
|
||||
? ""
|
||||
: execSync(finalCmd, {
|
||||
stdio: "pipe",
|
||||
...options,
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
if (detached) {
|
||||
exec(finalCmd);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
debugFn(str, "commandResult");
|
||||
}
|
||||
|
||||
return str.trim();
|
||||
} catch (error: any) {
|
||||
if (debug) {
|
||||
debugFn(error.message, "Raw SSh Error");
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Client } from "ssh2";
|
||||
|
||||
type Param = {
|
||||
ssh: Client;
|
||||
cmd: string;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
export default async function execSSH({
|
||||
ssh,
|
||||
cmd,
|
||||
debug,
|
||||
}: Param): Promise<string | undefined> {
|
||||
if (!ssh) {
|
||||
console.log(`No SSH object passed into execSSH function`);
|
||||
|
||||
return undefined;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
let finalData = "";
|
||||
let stdErrData = "";
|
||||
|
||||
ssh.exec(cmd, (err, stream) => {
|
||||
if (err) throw err;
|
||||
|
||||
stream
|
||||
.on("close", (code?: number, signal?: string) => {
|
||||
if (code || signal) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(finalData);
|
||||
}
|
||||
})
|
||||
.on("data", (data: any) => {
|
||||
finalData += data;
|
||||
})
|
||||
.stderr.on("data", (data) => {
|
||||
stdErrData += data;
|
||||
})
|
||||
.on("close", () => {
|
||||
if (stdErrData.match(/./)) {
|
||||
if (debug) {
|
||||
console.log("SSH Error code:", stdErrData);
|
||||
}
|
||||
|
||||
resolve(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function grabCoderankSSHPrefix() {
|
||||
return `ssh -i ${process.env.CODERANK_APP_DIR}/ssh/coderank -o StrictHostKeyChecking=no -C -c aes128-ctr`;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendData(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
data: WebSocketDataType
|
||||
) {
|
||||
ws.send(String(EJSON.stringify(data)));
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendError(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message?: String
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:error",
|
||||
message: message,
|
||||
} as WebSocketDataType)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "..";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendMessage(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:message",
|
||||
message: message,
|
||||
} as WebSocketDataType)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendReady(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message?: String
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:ready",
|
||||
message: message,
|
||||
} as WebSocketDataType)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendSuccess(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:success",
|
||||
message: message,
|
||||
} as WebSocketDataType)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import type { WebSocketData } from "@WS/.";
|
||||
import type { WebSocketDataType } from "@/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendUpdate(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:update",
|
||||
message: message,
|
||||
} as WebSocketDataType)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd /app
|
||||
|
||||
if [[ -z "$NODE_ENV" ]]; then
|
||||
echo "NODE_ENV is not set. Defaulting to development."
|
||||
NODE_ENV="development"
|
||||
fi
|
||||
|
||||
if [[ "$NODE_ENV" == "production" ]]; then
|
||||
bun websocket:start
|
||||
else
|
||||
bun websocket:dev
|
||||
fi
|
||||
Reference in New Issue
Block a user