2022-09-17 18:27:08 +00:00
|
|
|
import cluster from 'node:cluster';
|
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as http from 'node:http';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-12-03 10:42:05 +00:00
|
|
|
import Fastify from 'fastify';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { IsNull } from 'typeorm';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
|
|
|
import type { UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-18 14:07:41 +00:00
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { envOption } from '@/env.js';
|
|
|
|
import * as Acct from '@/misc/acct.js';
|
|
|
|
import { genIdenticon } from '@/misc/gen-identicon.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-09-18 14:07:41 +00:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { ActivityPubServerService } from './ActivityPubServerService.js';
|
|
|
|
import { NodeinfoServerService } from './NodeinfoServerService.js';
|
|
|
|
import { ApiServerService } from './api/ApiServerService.js';
|
|
|
|
import { StreamingApiServerService } from './api/StreamingApiServerService.js';
|
|
|
|
import { WellKnownServerService } from './WellKnownServerService.js';
|
|
|
|
import { MediaProxyServerService } from './MediaProxyServerService.js';
|
|
|
|
import { FileServerService } from './FileServerService.js';
|
|
|
|
import { ClientServerService } from './web/ClientServerService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ServerService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private logger: Logger;
|
2022-09-18 14:07:41 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private apiServerService: ApiServerService,
|
|
|
|
private streamingApiServerService: StreamingApiServerService,
|
|
|
|
private activityPubServerService: ActivityPubServerService,
|
|
|
|
private wellKnownServerService: WellKnownServerService,
|
|
|
|
private nodeinfoServerService: NodeinfoServerService,
|
|
|
|
private fileServerService: FileServerService,
|
|
|
|
private mediaProxyServerService: MediaProxyServerService,
|
|
|
|
private clientServerService: ClientServerService,
|
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-18 14:07:41 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger = this.loggerService.getLogger('server', 'gray', false);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public launch() {
|
2022-12-03 10:42:05 +00:00
|
|
|
const fastify = Fastify({
|
|
|
|
trustProxy: true,
|
|
|
|
logger: !['production', 'test'].includes(process.env.NODE_ENV ?? ''),
|
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
// HSTS
|
|
|
|
// 6months (15552000sec)
|
|
|
|
if (this.config.url.startsWith('https') && !this.config.disableHsts) {
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.addHook('onRequest', (request, reply, done) => {
|
|
|
|
reply.header('strict-transport-security', 'max-age=15552000; preload');
|
|
|
|
done();
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.register(this.apiServerService.createServer, { prefix: '/api' });
|
|
|
|
fastify.register(this.fileServerService.createServer, { prefix: '/files' });
|
|
|
|
fastify.register(this.mediaProxyServerService.createServer, { prefix: '/proxy' });
|
|
|
|
fastify.register(this.activityPubServerService.createServer);
|
|
|
|
fastify.register(this.nodeinfoServerService.createServer);
|
|
|
|
fastify.register(this.wellKnownServerService.createServer);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.get<{ Params: { acct: string } }>('/avatar/@:acct', async (request, reply) => {
|
|
|
|
const { username, host } = Acct.parse(request.params.acct);
|
2022-09-17 18:27:08 +00:00
|
|
|
const user = await this.usersRepository.findOne({
|
|
|
|
where: {
|
|
|
|
usernameLower: username.toLowerCase(),
|
|
|
|
host: (host == null) || (host === this.config.host) ? IsNull() : host,
|
|
|
|
isSuspended: false,
|
|
|
|
},
|
|
|
|
relations: ['avatar'],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user) {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.redirect(this.userEntityService.getAvatarUrlSync(user));
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.redirect('/static-assets/user-unknown.png');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.get<{ Params: { x: string } }>('/identicon/:x', async (request, reply) => {
|
2022-09-17 18:27:08 +00:00
|
|
|
const [temp, cleanup] = await createTemp();
|
2022-12-03 10:42:05 +00:00
|
|
|
await genIdenticon(request.params.x, fs.createWriteStream(temp));
|
|
|
|
reply.header('Content-Type', 'image/png');
|
|
|
|
return fs.createReadStream(temp).on('close', () => cleanup());
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.get<{ Params: { code: string } }>('/verify-email/:code', async (request, reply) => {
|
2022-09-17 18:27:08 +00:00
|
|
|
const profile = await this.userProfilesRepository.findOneBy({
|
2022-12-03 10:42:05 +00:00
|
|
|
emailVerifyCode: request.params.code,
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (profile != null) {
|
|
|
|
await this.userProfilesRepository.update({ userId: profile.userId }, {
|
|
|
|
emailVerified: true,
|
|
|
|
emailVerifyCode: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.globalEventService.publishMainStream(profile.userId, 'meUpdated', await this.userEntityService.pack(profile.userId, { id: profile.userId }, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true,
|
|
|
|
}));
|
2022-12-03 10:42:05 +00:00
|
|
|
|
|
|
|
reply.code(200);
|
|
|
|
return 'Verify succeeded!';
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(404);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.register(this.clientServerService.createServer);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
this.streamingApiServerService.attachStreamingApi(fastify.server);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
fastify.server.on('error', err => {
|
2022-09-18 14:07:41 +00:00
|
|
|
switch ((err as any).code) {
|
2022-09-17 18:27:08 +00:00
|
|
|
case 'EACCES':
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.error(`You do not have permission to listen on port ${this.config.port}.`);
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
case 'EADDRINUSE':
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.error(`Port ${this.config.port} is already in use by another process.`);
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.error(err);
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cluster.isWorker) {
|
2022-12-03 10:42:05 +00:00
|
|
|
process.send!('listenFailed');
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2022-12-03 10:42:05 +00:00
|
|
|
// disableClustering
|
2022-09-17 18:27:08 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-08 04:40:42 +00:00
|
|
|
fastify.listen({ port: this.config.port, host: '0.0.0.0' });
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|