admin side scan connection
This commit is contained in:
parent
b29cdae088
commit
83ab0c4ea7
7 changed files with 156 additions and 6 deletions
4
src/enums/socketEnum.ts
Normal file
4
src/enums/socketEnum.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export enum SocketConnectionTypes {
|
||||
scanner = "/scanner",
|
||||
pscanner = "/public_scanner",
|
||||
}
|
|
@ -45,7 +45,6 @@ export default async function authenticateSocket(socket: Socket, next: Function)
|
|||
permissions: decoded.permissions,
|
||||
isWebApiRequest: decoded?.sub == "webapi_access_token",
|
||||
});
|
||||
socket.join("home");
|
||||
|
||||
next();
|
||||
} catch (err) {
|
||||
|
|
|
@ -3,6 +3,8 @@ import { PermissionObject, PermissionType, PermissionSection, PermissionModule }
|
|||
import PermissionHelper from "../helpers/permissionHelper";
|
||||
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
|
||||
import { SocketMap } from "../storage/socketMap";
|
||||
import { SocketConnectionTypes } from "../enums/socketEnum";
|
||||
import SocketServer from ".";
|
||||
|
||||
export type EventResponseType = {
|
||||
answer: any;
|
||||
|
@ -23,7 +25,8 @@ type PermissionPass =
|
|||
section: PermissionSection;
|
||||
module?: PermissionModule;
|
||||
}
|
||||
| "admin";
|
||||
| "admin"
|
||||
| "noPermissionsRequired";
|
||||
|
||||
export let handleEvent = (
|
||||
permissions: PermissionPass,
|
||||
|
@ -37,7 +40,7 @@ export let handleEvent = (
|
|||
if (!socketData.isOwner && !socketData.permissions.admin) {
|
||||
throw new ForbiddenRequestException(`missing admin permission`);
|
||||
}
|
||||
} else {
|
||||
} else if (permissions != "noPermissionsRequired") {
|
||||
if (
|
||||
!socketData.isOwner &&
|
||||
!PermissionHelper.can(socketData.permissions, permissions.type, permissions.section, permissions.module)
|
||||
|
@ -58,6 +61,25 @@ export let handleEvent = (
|
|||
};
|
||||
};
|
||||
|
||||
export let emitEvent = (
|
||||
event: EventResponseType & { namespace?: SocketConnectionTypes },
|
||||
socket: Socket,
|
||||
io: Server
|
||||
) => {
|
||||
try {
|
||||
const { answer, type, room, namespace } = event;
|
||||
if (room === undefined || room == "") {
|
||||
socket.emit(type, answer);
|
||||
} else if (namespace === undefined) {
|
||||
socket.to(room).emit(type, answer);
|
||||
} else {
|
||||
io.of(namespace).emit(type, answer);
|
||||
}
|
||||
} catch (e) {
|
||||
socket.emit("error", e.message);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
socket.on(
|
||||
"event",
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
import helmet from "helmet";
|
||||
import { Server as httpServerType } from "http";
|
||||
import { Server } from "socket.io";
|
||||
import { instrument } from "@socket.io/admin-ui";
|
||||
|
||||
import authenticateSocket from "../middleware/authenticateSocket";
|
||||
import checkSocketExists from "../middleware/checkSocketExists";
|
||||
import { SocketConnectionTypes } from "../enums/socketEnum";
|
||||
|
||||
import base from "./base";
|
||||
import scanner from "./scanner";
|
||||
import pScanner from "./pScanner";
|
||||
|
||||
export default abstract class SocketServer {
|
||||
private static io: Server;
|
||||
|
@ -17,17 +23,31 @@ export default abstract class SocketServer {
|
|||
},
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV) {
|
||||
instrument(this.io, {
|
||||
auth: false,
|
||||
mode: "development",
|
||||
});
|
||||
}
|
||||
|
||||
this.io.engine.use(helmet());
|
||||
|
||||
this.io
|
||||
.of("/scanner")
|
||||
.of(SocketConnectionTypes.scanner)
|
||||
.use(authenticateSocket)
|
||||
.on("connection", (socket) => {
|
||||
console.log("socket connection: ", socket.id);
|
||||
socket.use((packet, next) => authenticateSocket(socket, next));
|
||||
socket.use((packet, next) => checkSocketExists(socket, packet, next));
|
||||
|
||||
base(this.io, socket);
|
||||
scanner(this.io, socket);
|
||||
});
|
||||
|
||||
this.io.of(SocketConnectionTypes.pscanner).on("connection", (socket) => {
|
||||
console.log("socket connection: ", socket.id);
|
||||
|
||||
base(this.io, socket);
|
||||
pScanner(this.io, socket);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,63 @@
|
|||
import { Server, Socket } from "socket.io";
|
||||
import { emitEvent, handleEvent } from "../handleEvent";
|
||||
import { SocketConnectionTypes } from "../../enums/socketEnum";
|
||||
|
||||
export default (io: Server, socket: Socket) => {
|
||||
socket.on(
|
||||
"session:create",
|
||||
handleEvent(
|
||||
"noPermissionsRequired",
|
||||
async (room: string) => {
|
||||
socket.join(room);
|
||||
return {
|
||||
type: "status-session:create",
|
||||
answer: { status: "success" },
|
||||
};
|
||||
},
|
||||
socket
|
||||
)
|
||||
);
|
||||
|
||||
socket.on(
|
||||
"session:close",
|
||||
handleEvent(
|
||||
"noPermissionsRequired",
|
||||
async () => {
|
||||
const rooms = Array.from(socket.rooms).filter((r) => r !== socket.id);
|
||||
const room = rooms[0];
|
||||
socket.leave(room);
|
||||
emitEvent(
|
||||
{
|
||||
type: "package-host_leave",
|
||||
answer: "host_leave",
|
||||
room: room,
|
||||
namespace: SocketConnectionTypes.pscanner,
|
||||
},
|
||||
socket,
|
||||
io
|
||||
);
|
||||
return {
|
||||
type: "status-session:close",
|
||||
answer: { status: "success" },
|
||||
};
|
||||
},
|
||||
socket
|
||||
)
|
||||
);
|
||||
|
||||
socket.on("disconnecting", () => {
|
||||
// tell public client, that host left - connection will be closed
|
||||
const rooms = Array.from(socket.rooms).filter((r) => r !== socket.id);
|
||||
const room = rooms[0];
|
||||
io.of(SocketConnectionTypes.pscanner).in(room).disconnectSockets();
|
||||
emitEvent(
|
||||
{
|
||||
type: "package-host_leave",
|
||||
answer: "host_leave",
|
||||
room: room,
|
||||
namespace: SocketConnectionTypes.pscanner,
|
||||
},
|
||||
socket,
|
||||
io
|
||||
);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue