Skip to content

Commit

Permalink
Merge pull request #20 from unicam-complex-system/intrusion-detection
Browse files Browse the repository at this point in the history
Intrusion detection
  • Loading branch information
Glydric authored Feb 6, 2024
2 parents fc12258 + 8319bf7 commit d1aec30
Show file tree
Hide file tree
Showing 39 changed files with 862 additions and 1,182 deletions.
10 changes: 6 additions & 4 deletions Backend/.env-example
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#The SSL_CERTIFICATE and PRIVATE_KEY refer to the variables used for https encryption, you can specify your own if
#you have generated a cert from a CA. Otherwise you can live it as it is.

#SSL_CERTIFICATE=/path-to/server.crt
#PRIVATE_KEY=/path-to/server.key
LISTEN_PORT=8080
MONGO_PROTOCOL=mongodb
MONGO_HOST=localhost:27017
MONGO_HOST=127.0.0.1:27017
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=123123
JWT_SECRET=RIpEW4QJCzEovH8wNsxv16aMiyGn0yNk
CSD_USER=root
CSD_PASSWORD=123123
TELEGRAM_TOKEN=TOKEN_FROM_TELEGRAM
TELEGRAM_TOKEN=TELEGRAM_TOKEN_FROM_TELEGRAM
BCRYPT_SALT=$2b$10$0H7uZ2aSzUll9k96X7EvHu
OPENVIDU_URL=http://localhost:4443/
OPENVIDU_SECRET=MY_SECRET
NVR_IP_ADDRESS=rtsp://192.168.1.41:80
1 change: 0 additions & 1 deletion Backend/.env-openvidu

This file was deleted.

10 changes: 10 additions & 0 deletions Backend/cleaner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
rm -r ./static/stream/*
mkdir -p ./static/stream/cam0
mkdir -p ./static/stream/cam1
mkdir -p ./static/stream/cam2
mkdir -p ./static/stream/cam3
mkdir -p ./static/stream/cam4
mkdir -p ./static/stream/cam5
mkdir -p ./static/stream/cam6
mkdir -p ./static/stream/cam7
58 changes: 58 additions & 0 deletions Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"private": true,
"license": "GPL",
"scripts": {
"docker:start": "npm ci && nest start",
"css:start": "chmod +x cleaner.sh && ./cleaner.sh && npm ci && nest start",
"docker:dev": "npm run docker:start -- --watch",
"docker:test": "npm ci && jest",
"build": "nest build",
"start": "nest start",
"start:linux": "./cleaner.sh && nest start",
"start:dev": "nest start --watch",
"start:debug": "npm start -- --debug --watch",
"start:prod": "node dist/main",
Expand All @@ -30,6 +31,7 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-socket.io": "^10.2.10",
"@nestjs/serve-static": "^4.0.0",
"@nestjs/swagger": "^7.1.16",
"@nestjs/websockets": "^10.2.10",
"bcrypt": "^5.1.1",
Expand All @@ -38,7 +40,6 @@
"dotenv": "^16.3.1",
"mongodb": "^6.3.0",
"node-telegram-bot-api": "^0.64.0",
"openvidu-node-client": "^2.29.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"ws": "^8.14.2"
Expand Down
7 changes: 5 additions & 2 deletions Backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { LoginController } from './app/login.controller';
import { CameraStreamGateway } from './cameraStream/cameraStream.gateway';
import { LoginService } from './login/login.service';
import { MediaServerController } from './app/mediaServer/mediaServer.controller';
import { CSSOpenVidu } from './cameraStream/open-vidu.service';
import { SharedController } from './app/shared/shared.controller';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
imports: [
Expand All @@ -25,6 +26,9 @@ import { SharedController } from './app/shared/shared.controller';
// FIXME Error: Payload as string is not allowed with the following sign options: expiresIn
// signOptions: { expiresIn: "60s" },
}),
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'static'),
}),
],
controllers: [
MachineLearningController,
Expand All @@ -38,7 +42,6 @@ import { SharedController } from './app/shared/shared.controller';
TelegramService,
CameraStreamGateway,
LoginService,
CSSOpenVidu,
],
exports: [DatabaseService, TelegramService],
})
Expand Down
4 changes: 2 additions & 2 deletions Backend/src/app/machineLearning/machineLearning.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ export class MachineLearningController {
) {
const date = new Date();

console.log('Adding data to database');
// console.log('Adding data to database');
await this.database.addData(new DataType(cameraId, date, null, file));
console.log('Sending intrusion detection notification');
// console.log('Sending intrusion detection notification');
await this.telegramApi.sendIntrusionDetectionNotification(
cameraId,
date,
Expand Down
4 changes: 1 addition & 3 deletions Backend/src/cameraStream/cameraStream.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CameraStreamGateway } from './cameraStream.gateway';
import { JwtModule } from '@nestjs/jwt';

import { CSSOpenVidu } from './open-vidu.service';
import { DatabaseService } from '../database/database.service';

describe('WebStreamGateway', () => {
let gateway: CameraStreamGateway;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CameraStreamGateway, CSSOpenVidu, DatabaseService],
providers: [CameraStreamGateway, DatabaseService],
imports: [
JwtModule.register({
global: true,
Expand Down
63 changes: 11 additions & 52 deletions Backend/src/cameraStream/cameraStream.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import {
UseGuards,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { CSSOpenVidu } from './open-vidu.service';
import { ConnectionProperties, ConnectionType } from 'openvidu-node-client';
import { DatabaseService } from '../database/database.service';
import { transcode } from './transcode';

@Catch(WsException, HttpException)
export class WsExceptionFilter implements WsExceptionFilter {
Expand Down Expand Up @@ -49,32 +48,24 @@ export class CameraStreamGateway implements OnGatewayConnection {

constructor(
private readonly jwtService: JwtService,
private readonly openvidu: CSSOpenVidu,
private readonly database: DatabaseService,
) {}

async afterInit() {
try {
const session = await this.openvidu.instance.createSession();
this.sessionId = session.sessionId;
const nvr = await this.database.getNVRData();

nvr.channels.forEach((id: number) => {
const connectionProperties: ConnectionProperties = {
type: ConnectionType.IPCAM,
rtspUri: `${nvr.ip}/ch${id}_0.264`,
adaptativeBitrate: true,
onlyPlayWithSubscribers: true,
networkCache: 1000,
data: id.toString(),
};
console.log(connectionProperties);
/* To be used with NVR */
const cameras = nvr.channels.map((id: number) => ({
id: id.toString(),
rtspUri: `${nvr.ip}/ch${id}_0.264`,
}));

session
.createConnection(connectionProperties)
.then((connection: unknown) => console.log(connection))
.catch((error) => console.error(error));
});
Promise.all(
cameras.map((camera) => {
transcode(camera, this.io);
}),
);
} catch (error) {
console.error(error);
}
Expand All @@ -85,10 +76,6 @@ export class CameraStreamGateway implements OnGatewayConnection {
new AuthGuard(this.jwtService).checkToken(client.handshake.headers);

console.log(`Welcome new client .... ${client.id}`);
client.emit(
'session_delivery',
JSON.stringify({ sessionId: this.sessionId }),
);
} catch (e) {
client.disconnect();
return;
Expand All @@ -110,34 +97,6 @@ export class CameraStreamGateway implements OnGatewayConnection {
}
}

@SubscribeMessage('token_request')
private async createToken(
@ConnectedSocket() client: Socket,
@MessageBody() data: string,
) {
try {
const message = JSON.parse(data) as { sessionId: string };
const session = this.openvidu.instance.activeSessions.find(
(s) => s.sessionId === message.sessionId,
);

if (!session) {
client.emit(
'error',
JSON.stringify({ message: 'Error getting the session' }),
);
} else {
const connection = await session.createConnection();
client.emit(
'token_delivery',
JSON.stringify({ token: connection.token }),
);
}
} catch (e) {
return e.message;
}
}

broadcastEvent(event: any) {
console.log(event);
this.io.to('clients').emit('participantLeft', JSON.stringify(event));
Expand Down
14 changes: 0 additions & 14 deletions Backend/src/cameraStream/open-vidu.service.ts

This file was deleted.

Loading

0 comments on commit d1aec30

Please sign in to comment.