2022-09-18 03:27:08 +09:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 13:50:05 +09:00
|
|
|
import * as Redis from 'ioredis';
|
2023-05-29 13:32:19 +09:00
|
|
|
import * as WebSocket from 'ws';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-05-29 13:32:19 +09:00
|
|
|
import type { UsersRepository, AccessToken } from '@/models/index.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { NoteReadService } from '@/core/NoteReadService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
2023-03-08 08:56:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-05 10:21:10 +09:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-05-29 13:32:19 +09:00
|
|
|
import { LocalUser } from '@/models/entities/User';
|
|
|
|
import { AuthenticateService, AuthenticationError } from './AuthenticateService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import MainStreamConnection from './stream/index.js';
|
|
|
|
import { ChannelsService } from './stream/ChannelsService.js';
|
|
|
|
import type * as http from 'node:http';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class StreamingApiServerService {
|
2023-05-29 13:32:19 +09:00
|
|
|
#wss: WebSocket.WebSocketServer;
|
2023-06-02 09:13:41 +09:00
|
|
|
#connections = new Map<WebSocket.WebSocket, number>();
|
|
|
|
#cleanConnectionsIntervalId: NodeJS.Timeout | null = null;
|
2023-05-29 13:32:19 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2023-04-09 17:09:27 +09:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
2023-04-05 10:21:10 +09:00
|
|
|
private cacheService: CacheService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private noteReadService: NoteReadService,
|
|
|
|
private authenticateService: AuthenticateService,
|
|
|
|
private channelsService: ChannelsService,
|
|
|
|
private notificationService: NotificationService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-05-29 13:32:19 +09:00
|
|
|
public attach(server: http.Server): void {
|
|
|
|
this.#wss = new WebSocket.WebSocketServer({
|
|
|
|
noServer: true,
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
server.on('upgrade', async (request, socket, head) => {
|
|
|
|
if (request.url == null) {
|
|
|
|
socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
|
|
socket.destroy();
|
2022-09-18 03:27:08 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
const q = new URL(request.url, `http://${request.headers.host}`).searchParams;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
let user: LocalUser | null = null;
|
|
|
|
let app: AccessToken | null = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
[user, app] = await this.authenticateService.authenticate(q.get('i'));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof AuthenticationError) {
|
|
|
|
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
|
|
|
} else {
|
|
|
|
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
|
|
|
|
}
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
if (user?.isSuspended) {
|
|
|
|
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
const stream = new MainStreamConnection(
|
2022-09-18 03:27:08 +09:00
|
|
|
this.channelsService,
|
|
|
|
this.noteReadService,
|
|
|
|
this.notificationService,
|
2023-04-05 10:21:10 +09:00
|
|
|
this.cacheService,
|
2023-05-29 13:32:19 +09:00
|
|
|
user, app,
|
2022-09-18 03:27:08 +09:00
|
|
|
);
|
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
await stream.init();
|
|
|
|
|
|
|
|
this.#wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
this.#wss.emit('connection', ws, request, {
|
|
|
|
stream, user, app,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
|
|
|
|
stream: MainStreamConnection,
|
|
|
|
user: LocalUser | null;
|
|
|
|
app: AccessToken | null
|
|
|
|
}) => {
|
|
|
|
const { stream, user, app } = ctx;
|
|
|
|
|
|
|
|
const ev = new EventEmitter();
|
|
|
|
|
|
|
|
async function onRedisMessage(_: string, data: string): Promise<void> {
|
|
|
|
const parsed = JSON.parse(data);
|
|
|
|
ev.emit(parsed.channel, parsed.message);
|
|
|
|
}
|
2023-04-05 10:21:10 +09:00
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
this.redisForSub.on('message', onRedisMessage);
|
2023-04-05 10:21:10 +09:00
|
|
|
|
2023-05-29 13:32:19 +09:00
|
|
|
await stream.listen(ev, connection);
|
2023-04-05 10:21:10 +09:00
|
|
|
|
2023-06-02 09:13:41 +09:00
|
|
|
this.#connections.set(connection, Date.now());
|
|
|
|
|
|
|
|
const userUpdateIntervalId = user ? setInterval(() => {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.usersRepository.update(user.id, {
|
|
|
|
lastActiveDate: new Date(),
|
|
|
|
});
|
|
|
|
}, 1000 * 60 * 5) : null;
|
|
|
|
if (user) {
|
|
|
|
this.usersRepository.update(user.id, {
|
|
|
|
lastActiveDate: new Date(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.once('close', () => {
|
|
|
|
ev.removeAllListeners();
|
2023-05-29 13:32:19 +09:00
|
|
|
stream.dispose();
|
2023-04-09 17:09:27 +09:00
|
|
|
this.redisForSub.off('message', onRedisMessage);
|
2023-06-02 09:13:41 +09:00
|
|
|
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
connection.on('message', async (data) => {
|
2023-06-02 09:13:41 +09:00
|
|
|
this.#connections.set(connection, Date.now());
|
2023-05-29 13:32:19 +09:00
|
|
|
if (data.toString() === 'ping') {
|
2022-09-18 03:27:08 +09:00
|
|
|
connection.send('pong');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-06-02 09:13:41 +09:00
|
|
|
|
2023-06-09 12:47:36 +09:00
|
|
|
// 一定期間通信が無いコネクションは実際には切断されている可能性があるため定期的にterminateする
|
2023-06-02 09:13:41 +09:00
|
|
|
this.#cleanConnectionsIntervalId = setInterval(() => {
|
|
|
|
const now = Date.now();
|
|
|
|
for (const [connection, lastActive] of this.#connections.entries()) {
|
|
|
|
if (now - lastActive > 1000 * 60 * 5) {
|
|
|
|
connection.terminate();
|
|
|
|
this.#connections.delete(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 1000 * 60 * 5);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-05-29 13:32:19 +09:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public detach(): Promise<void> {
|
2023-06-02 09:13:41 +09:00
|
|
|
if (this.#cleanConnectionsIntervalId) {
|
|
|
|
clearInterval(this.#cleanConnectionsIntervalId);
|
|
|
|
this.#cleanConnectionsIntervalId = null;
|
|
|
|
}
|
2023-05-29 13:32:19 +09:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.#wss.close(() => resolve());
|
|
|
|
});
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|