2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 04:50:05 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2023-05-29 04:32:19 +00:00
|
|
|
import * as WebSocket from 'ws';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { UsersRepository, MiAccessToken } from '@/models/_.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { NoteReadService } from '@/core/NoteReadService.js';
|
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
2023-03-07 23:56:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-05 01:21:10 +00:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import { MiLocalUser } from '@/models/User.js';
|
2023-10-03 11:26:11 +00:00
|
|
|
import { UserService } from '@/core/UserService.js';
|
2023-10-27 09:34:02 +00:00
|
|
|
import { ChannelFollowingService } from '@/core/ChannelFollowingService.js';
|
2023-05-29 04:32:19 +00:00
|
|
|
import { AuthenticateService, AuthenticationError } from './AuthenticateService.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import MainStreamConnection from './stream/Connection.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { ChannelsService } from './stream/ChannelsService.js';
|
|
|
|
import type * as http from 'node:http';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class StreamingApiServerService {
|
2023-05-29 04:32:19 +00:00
|
|
|
#wss: WebSocket.WebSocketServer;
|
2023-06-02 00:13:41 +00:00
|
|
|
#connections = new Map<WebSocket.WebSocket, number>();
|
|
|
|
#cleanConnectionsIntervalId: NodeJS.Timeout | null = null;
|
2023-05-29 04:32:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
2023-04-09 08:09:27 +00:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private noteReadService: NoteReadService,
|
|
|
|
private authenticateService: AuthenticateService,
|
|
|
|
private channelsService: ChannelsService,
|
|
|
|
private notificationService: NotificationService,
|
2023-10-03 11:26:11 +00:00
|
|
|
private usersService: UserService,
|
2023-10-27 09:34:02 +00:00
|
|
|
private channelFollowingService: ChannelFollowingService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-05-29 04:32:19 +00:00
|
|
|
public attach(server: http.Server): void {
|
|
|
|
this.#wss = new WebSocket.WebSocketServer({
|
|
|
|
noServer: true,
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
|
2023-05-29 04:32:19 +00: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-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
const q = new URL(request.url, `http://${request.headers.host}`).searchParams;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-08-16 08:51:28 +00:00
|
|
|
let user: MiLocalUser | null = null;
|
|
|
|
let app: MiAccessToken | null = null;
|
2023-05-29 04:32:19 +00:00
|
|
|
|
2023-06-28 04:37:13 +00:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc6750.html#section-2.1
|
|
|
|
// Note that the standard WHATWG WebSocket API does not support setting any headers,
|
|
|
|
// but non-browser apps may still be able to set it.
|
|
|
|
const token = request.headers.authorization?.startsWith('Bearer ')
|
|
|
|
? request.headers.authorization.slice(7)
|
|
|
|
: q.get('i');
|
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
try {
|
2023-06-28 04:37:13 +00:00
|
|
|
[user, app] = await this.authenticateService.authenticate(token);
|
2023-12-27 06:08:59 +00:00
|
|
|
|
|
|
|
if (app !== null && !app.permission.some(p => p === 'read:account')) {
|
|
|
|
throw new AuthenticationError('Your app does not have necessary permissions to use websocket API.');
|
|
|
|
}
|
2023-05-29 04:32:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof AuthenticationError) {
|
2023-06-28 04:37:13 +00:00
|
|
|
socket.write([
|
|
|
|
'HTTP/1.1 401 Unauthorized',
|
|
|
|
'WWW-Authenticate: Bearer realm="Misskey", error="invalid_token", error_description="Failed to authenticate"',
|
|
|
|
].join('\r\n') + '\r\n\r\n');
|
2023-05-29 04:32:19 +00:00
|
|
|
} else {
|
|
|
|
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
|
|
|
|
}
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
if (user?.isSuspended) {
|
|
|
|
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
const stream = new MainStreamConnection(
|
2022-09-17 18:27:08 +00:00
|
|
|
this.channelsService,
|
|
|
|
this.noteReadService,
|
|
|
|
this.notificationService,
|
2023-04-05 01:21:10 +00:00
|
|
|
this.cacheService,
|
2023-10-27 09:34:02 +00:00
|
|
|
this.channelFollowingService,
|
2023-05-29 04:32:19 +00:00
|
|
|
user, app,
|
2022-09-17 18:27:08 +00:00
|
|
|
);
|
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
await stream.init();
|
|
|
|
|
|
|
|
this.#wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
this.#wss.emit('connection', ws, request, {
|
|
|
|
stream, user, app,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-03 22:49:13 +00:00
|
|
|
const globalEv = new EventEmitter();
|
|
|
|
|
|
|
|
this.redisForSub.on('message', (_: string, data: string) => {
|
|
|
|
const parsed = JSON.parse(data);
|
|
|
|
globalEv.emit('message', parsed);
|
|
|
|
});
|
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
|
|
|
|
stream: MainStreamConnection,
|
2023-08-16 08:51:28 +00:00
|
|
|
user: MiLocalUser | null;
|
|
|
|
app: MiAccessToken | null
|
2023-05-29 04:32:19 +00:00
|
|
|
}) => {
|
|
|
|
const { stream, user, app } = ctx;
|
|
|
|
|
|
|
|
const ev = new EventEmitter();
|
|
|
|
|
2023-07-03 22:49:13 +00:00
|
|
|
function onRedisMessage(data: any): void {
|
|
|
|
ev.emit(data.channel, data.message);
|
2023-05-29 04:32:19 +00:00
|
|
|
}
|
2023-04-05 01:21:10 +00:00
|
|
|
|
2023-07-03 22:49:13 +00:00
|
|
|
globalEv.on('message', onRedisMessage);
|
2023-04-05 01:21:10 +00:00
|
|
|
|
2023-05-29 04:32:19 +00:00
|
|
|
await stream.listen(ev, connection);
|
2023-04-05 01:21:10 +00:00
|
|
|
|
2023-06-02 00:13:41 +00:00
|
|
|
this.#connections.set(connection, Date.now());
|
|
|
|
|
|
|
|
const userUpdateIntervalId = user ? setInterval(() => {
|
2023-10-03 11:26:11 +00:00
|
|
|
this.usersService.updateLastActiveDate(user);
|
2022-09-17 18:27:08 +00:00
|
|
|
}, 1000 * 60 * 5) : null;
|
|
|
|
if (user) {
|
2023-10-03 11:26:11 +00:00
|
|
|
this.usersService.updateLastActiveDate(user);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connection.once('close', () => {
|
|
|
|
ev.removeAllListeners();
|
2023-05-29 04:32:19 +00:00
|
|
|
stream.dispose();
|
2023-07-03 22:49:13 +00:00
|
|
|
globalEv.off('message', onRedisMessage);
|
2023-06-09 07:11:28 +00:00
|
|
|
this.#connections.delete(connection);
|
2023-06-02 00:13:41 +00:00
|
|
|
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
|
2023-06-09 08:07:57 +00:00
|
|
|
connection.on('pong', () => {
|
2023-06-02 00:13:41 +00:00
|
|
|
this.#connections.set(connection, Date.now());
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
});
|
2023-06-02 00:13:41 +00:00
|
|
|
|
2023-06-09 03:47:36 +00:00
|
|
|
// 一定期間通信が無いコネクションは実際には切断されている可能性があるため定期的にterminateする
|
2023-06-02 00:13:41 +00:00
|
|
|
this.#cleanConnectionsIntervalId = setInterval(() => {
|
|
|
|
const now = Date.now();
|
|
|
|
for (const [connection, lastActive] of this.#connections.entries()) {
|
2023-06-09 08:07:57 +00:00
|
|
|
if (now - lastActive > 1000 * 60 * 2) {
|
2023-06-02 00:13:41 +00:00
|
|
|
connection.terminate();
|
|
|
|
this.#connections.delete(connection);
|
2023-06-09 08:07:57 +00:00
|
|
|
} else {
|
|
|
|
connection.ping();
|
2023-06-02 00:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-09 08:07:57 +00:00
|
|
|
}, 1000 * 60);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-05-29 04:32:19 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public detach(): Promise<void> {
|
2023-06-02 00:13:41 +00:00
|
|
|
if (this.#cleanConnectionsIntervalId) {
|
|
|
|
clearInterval(this.#cleanConnectionsIntervalId);
|
|
|
|
this.#cleanConnectionsIntervalId = null;
|
|
|
|
}
|
2023-05-29 04:32:19 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.#wss.close(() => resolve());
|
|
|
|
});
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|