Skip to content

Commit

Permalink
feat: generator 코드 분리 및 rtmp 서버 용 스트림, 세션 키 교환 api 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
i3kae committed Nov 16, 2024
1 parent 2d974a9 commit 2619196
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
35 changes: 32 additions & 3 deletions backend/mainServer/src/host/host.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Post, Req, Res, HttpException, HttpStatus, Body } from '@nestjs/common';
import { Controller, Post, Get, Req, Res, HttpException, HttpStatus, Body, Query } from '@nestjs/common';
import { HostService } from './host.service.js';
import { hostKeyPairDto } from './dto/hostKeyPairDto.js';
import { Request, Response } from 'express';
Expand All @@ -7,7 +7,12 @@ import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
@Controller('host')
@ApiTags('Host API')
export class HostController {
constructor(private readonly hostService: HostService) {}
_inMemory;
constructor(private readonly hostService: HostService) {
this._inMemory = {
'web22':'web22_session'
};
}

@Post('/key')
@ApiOperation({ summary: 'Host Stream, Session Key Generate API', description: 'Host용 스트림키와 세션키를 생성합니다.' })
Expand All @@ -25,7 +30,31 @@ export class HostController {
}

const [streamKey, sessionKey] = await this.hostService.generateStreamKey(requestDto);
res.status(HttpStatus.OK).json({ 'stream-key': streamKey, 'session-key':sessionKey });
this._inMemory[streamKey] = sessionKey;
console.log(this._inMemory);
res.status(HttpStatus.OK).json({ 'streamKey': streamKey, 'sessionKey':sessionKey });
} catch (error) {
if ((error as { status: number }).status === 400) {
res.status(HttpStatus.BAD_REQUEST).json({
error: (error as { response: Response }).response
});
}
else {
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
error: 'Server logic error',
});
}
}
}

@Get('/session')
@ApiOperation({ summary: 'Session Key To Session Key API', description: 'Host의 Stream Key를 통해 Session Key를 찾습니다.' })
@ApiCreatedResponse({ description: '스트림 키를 통해 세션키를 전달 받습니다.' })
async findSession(@Query('streamKey') streamKey: string, @Req() req: Request, @Res() res: Response) {
try {
if (!(streamKey in this._inMemory))
throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST);
res.status(HttpStatus.OK).json({'session-key':this._inMemory[streamKey] });
} catch (error) {
if ((error as { status: number }).status === 400) {
res.status(HttpStatus.BAD_REQUEST).json({
Expand Down
4 changes: 2 additions & 2 deletions backend/mainServer/src/util/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'crypto';

export function randomKey(uuid: string, salt: string) {
export function randomKey(uuid: string, salt: string, length: number = 20) {
const hash = crypto.createHmac('sha256', salt).update(uuid).digest('hex');
return hash;
return hash.substring(0, length);
}

0 comments on commit 2619196

Please sign in to comment.