Updates
This commit is contained in:
@@ -1,20 +1,51 @@
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import { NSQLITE_TEST_DB_USERS_PORTS, NSQLiteTables } from "@/src/db/types";
|
||||
import { User } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import { execSync, ExecSyncOptions } from "child_process";
|
||||
import killPort from "./kill-port";
|
||||
|
||||
type Params = {
|
||||
exec_options?: ExecSyncOptions;
|
||||
user: User;
|
||||
};
|
||||
|
||||
export default async function killAllTtydPorts(params?: Params) {
|
||||
const start_port = AppData["DynamicPortStart"];
|
||||
const end_port = start_port + 100;
|
||||
const kill_ports_cmd = `ss -tlnp | awk 'NR>1 {split($4, a, ":"); port=a[length(a)]; if (port >= ${start_port} && port <= ${end_port}) print $6}' | grep -oP 'pid=\\K[0-9]+' | xargs -r kill -9`;
|
||||
|
||||
execSync(kill_ports_cmd, {
|
||||
...params?.exec_options,
|
||||
export default async function killAllTtydPorts({ user }: Params) {
|
||||
const user_ports_res = await BunSQLite.select<
|
||||
NSQLITE_TEST_DB_USERS_PORTS,
|
||||
(typeof NSQLiteTables)[number]
|
||||
>({
|
||||
table: "users_ports",
|
||||
query: { query: { user_id: { value: String(user.id) } } },
|
||||
});
|
||||
|
||||
if (user_ports_res?.payload?.[0]) {
|
||||
for (let i = 0; i < user_ports_res.payload.length; i++) {
|
||||
const user_port = user_ports_res.payload[i];
|
||||
if (!user_port.port) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await killPort({ port: user_port.port });
|
||||
|
||||
await BunSQLite.delete<
|
||||
NSQLITE_TEST_DB_USERS_PORTS,
|
||||
(typeof NSQLiteTables)[number]
|
||||
>({
|
||||
table: "users_ports",
|
||||
query: { query: { port: { value: String(user_port.port) } } },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// const start_port = AppData["DynamicPortStart"];
|
||||
// const end_port = start_port + 100;
|
||||
// const kill_ports_cmd = `ss -tlnp | awk 'NR>1 {split($4, a, ":"); port=a[length(a)]; if (port >= ${start_port} && port <= ${end_port}) print $6}' | grep -oP 'pid=\\K[0-9]+' | xargs -r kill -9`;
|
||||
|
||||
// execSync(kill_ports_cmd, {
|
||||
// ...exec_options,
|
||||
// });
|
||||
|
||||
// sudo ss -tlnp | awk 'NR>1 {split($4, a, ":"); port=a[length(a)]; if (port >= 4700 && port <= 4800) print $6}' | grep -oP 'pid=\K[0-9]+' | xargs -r sudo kill -9
|
||||
|
||||
// ss -tlnp | awk 'NR>1 {split($4, a, ":"); port=a[length(a)]; if (port >= 4700 && port <= 4800) print $6}' | grep -oP 'pid=\K[0-9]+' | xargs -r sudo kill -9
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { WebSocketMessageParam } from "@/src/types";
|
||||
import sendData from "../(utils)/send-data";
|
||||
import sendError from "../(utils)/send-error";
|
||||
import grabConnectedWebsocketUserdata from "../(utils)/grab-connected-websocket-user-data";
|
||||
import killPort from "../(utils)/kill-port";
|
||||
import killAllTtydPorts from "../(utils)/kill-all-ttyd-ports";
|
||||
|
||||
export default async function socketClientKillAllPorts({
|
||||
ws,
|
||||
@@ -11,18 +10,7 @@ export default async function socketClientKillAllPorts({
|
||||
try {
|
||||
const user = ws.data.user;
|
||||
|
||||
const connected_user_data = grabConnectedWebsocketUserdata({ user });
|
||||
|
||||
for (let i = 0; i < connected_user_data.ports.length; i++) {
|
||||
const port = connected_user_data.ports[i];
|
||||
if (port) {
|
||||
await killPort({ port, exec_options: { stdio: "inherit" } });
|
||||
|
||||
connected_user_data.ports = connected_user_data.ports.filter(
|
||||
(p) => p != port,
|
||||
);
|
||||
}
|
||||
}
|
||||
await killAllTtydPorts({ user });
|
||||
|
||||
sendData(ws, {
|
||||
event: "server:killed-all-ports",
|
||||
|
||||
@@ -27,7 +27,5 @@ export default async function socketClose({ ws }: Param) {
|
||||
existing_connected_user_data.child_processes = [];
|
||||
}
|
||||
|
||||
killAllTtydPorts({
|
||||
exec_options: { stdio: "inherit" },
|
||||
});
|
||||
killAllTtydPorts({ user });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user