2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-05-29 11:54:49 +09:00
|
|
|
import * as Bull from 'bullmq';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { InstancesRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-12-04 10:16:03 +09:00
|
|
|
import { ApRequestService } from '@/core/activitypub/ApRequestService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { FetchInstanceMetadataService } from '@/core/FetchInstanceMetadataService.js';
|
2023-04-06 11:14:43 +09:00
|
|
|
import { MemorySingleCache } from '@/misc/cache.js';
|
2023-08-16 17:51:28 +09:00
|
|
|
import type { MiInstance } from '@/models/entities/Instance.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import InstanceChart from '@/core/chart/charts/instance.js';
|
|
|
|
import ApRequestChart from '@/core/chart/charts/ap-request.js';
|
|
|
|
import FederationChart from '@/core/chart/charts/federation.js';
|
|
|
|
import { StatusError } from '@/misc/status-error.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2023-01-03 08:48:00 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type { DeliverJobData } from '../types.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DeliverProcessorService {
|
2022-09-19 03:11:50 +09:00
|
|
|
private logger: Logger;
|
2023-08-16 17:51:28 +09:00
|
|
|
private suspendedHostsCache: MemorySingleCache<MiInstance[]>;
|
2022-09-19 03:11:50 +09:00
|
|
|
private latest: string | null;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private federatedInstanceService: FederatedInstanceService,
|
|
|
|
private fetchInstanceMetadataService: FetchInstanceMetadataService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private instanceChart: InstanceChart,
|
|
|
|
private apRequestChart: ApRequestChart,
|
|
|
|
private federationChart: FederationChart,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('deliver');
|
2023-08-16 17:51:28 +09:00
|
|
|
this.suspendedHostsCache = new MemorySingleCache<MiInstance[]>(1000 * 60 * 60);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async process(job: Bull.Job<DeliverJobData>): Promise<string> {
|
|
|
|
const { host } = new URL(job.data.to);
|
|
|
|
|
|
|
|
// ブロックしてたら中断
|
|
|
|
const meta = await this.metaService.fetch();
|
2023-01-13 18:21:07 +09:00
|
|
|
if (this.utilityService.isBlockedHost(meta.blockedHosts, this.utilityService.toPuny(host))) {
|
2022-09-18 03:27:08 +09:00
|
|
|
return 'skip (blocked)';
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSuspendedなら中断
|
2023-04-04 17:32:09 +09:00
|
|
|
let suspendedHosts = this.suspendedHostsCache.get();
|
2022-09-18 03:27:08 +09:00
|
|
|
if (suspendedHosts == null) {
|
|
|
|
suspendedHosts = await this.instancesRepository.find({
|
|
|
|
where: {
|
|
|
|
isSuspended: true,
|
|
|
|
},
|
|
|
|
});
|
2023-04-04 17:32:09 +09:00
|
|
|
this.suspendedHostsCache.set(suspendedHosts);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
if (suspendedHosts.map(x => x.host).includes(this.utilityService.toPuny(host))) {
|
|
|
|
return 'skip (suspended)';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.apRequestService.signedPost(job.data.user, job.data.to, job.data.content);
|
|
|
|
|
|
|
|
// Update stats
|
2023-01-03 09:32:36 +09:00
|
|
|
this.federatedInstanceService.fetch(host).then(i => {
|
|
|
|
if (i.isNotResponding) {
|
2023-04-22 19:59:08 +09:00
|
|
|
this.federatedInstanceService.update(i.id, {
|
2023-01-03 09:32:36 +09:00
|
|
|
isNotResponding: false,
|
|
|
|
});
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
this.fetchInstanceMetadataService.fetchInstanceMetadata(i);
|
|
|
|
this.apRequestChart.deliverSucc();
|
|
|
|
this.federationChart.deliverd(i.host, true);
|
2023-03-24 19:08:08 +09:00
|
|
|
|
|
|
|
if (meta.enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.requestSent(i.host, true);
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return 'Success';
|
|
|
|
} catch (res) {
|
2023-01-03 08:48:00 +09:00
|
|
|
// Update stats
|
2023-01-03 09:32:36 +09:00
|
|
|
this.federatedInstanceService.fetch(host).then(i => {
|
|
|
|
if (!i.isNotResponding) {
|
2023-04-22 19:59:08 +09:00
|
|
|
this.federatedInstanceService.update(i.id, {
|
2023-01-03 09:32:36 +09:00
|
|
|
isNotResponding: true,
|
|
|
|
});
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
this.apRequestChart.deliverFail();
|
|
|
|
this.federationChart.deliverd(i.host, false);
|
2023-03-24 19:08:08 +09:00
|
|
|
|
|
|
|
if (meta.enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.requestSent(i.host, false);
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (res instanceof StatusError) {
|
2023-01-03 08:48:00 +09:00
|
|
|
// 4xx
|
2022-09-18 03:27:08 +09:00
|
|
|
if (res.isClientError) {
|
2023-03-13 09:46:53 +09:00
|
|
|
// 相手が閉鎖していることを明示しているため、配送停止する
|
2023-03-14 19:11:31 +09:00
|
|
|
if (job.data.isSharedInbox && res.statusCode === 410) {
|
2023-03-13 09:46:53 +09:00
|
|
|
this.federatedInstanceService.fetch(host).then(i => {
|
2023-04-22 19:59:08 +09:00
|
|
|
this.federatedInstanceService.update(i.id, {
|
2023-03-13 09:46:53 +09:00
|
|
|
isSuspended: true,
|
|
|
|
});
|
|
|
|
});
|
2023-05-29 11:54:49 +09:00
|
|
|
throw new Bull.UnrecoverableError(`${host} is gone`);
|
2023-03-13 09:46:53 +09:00
|
|
|
}
|
2023-05-29 11:54:49 +09:00
|
|
|
throw new Bull.UnrecoverableError(`${res.statusCode} ${res.statusMessage}`);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5xx etc.
|
2023-05-29 11:54:49 +09:00
|
|
|
throw new Error(`${res.statusCode} ${res.statusMessage}`);
|
2022-09-18 03:27:08 +09:00
|
|
|
} else {
|
2023-01-03 08:48:00 +09:00
|
|
|
// DNS error, socket error, timeout ...
|
2022-09-18 03:27:08 +09:00
|
|
|
throw res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|