2022-02-27 03:07:39 +01:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import Bull from 'bull';
|
|
|
|
import request from '@/remote/activitypub/request.js';
|
|
|
|
import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc.js';
|
|
|
|
import Logger from '@/services/logger.js';
|
|
|
|
import { Instances } from '@/models/index.js';
|
|
|
|
import { apRequestChart, federationChart, instanceChart } from '@/services/chart/index.js';
|
|
|
|
import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { toPuny } from '@/misc/convert-host.js';
|
|
|
|
import { Cache } from '@/misc/cache.js';
|
|
|
|
import { Instance } from '@/models/entities/instance.js';
|
|
|
|
import { DeliverJobData } from '../types.js';
|
|
|
|
import { StatusError } from '@/misc/fetch.js';
|
2019-03-09 02:18:59 +01:00
|
|
|
|
|
|
|
const logger = new Logger('deliver');
|
2018-04-04 16:12:35 +02:00
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
let latest: string | null = null;
|
2019-03-06 14:55:47 +01:00
|
|
|
|
2021-03-18 02:49:14 +01:00
|
|
|
const suspendedHostsCache = new Cache<Instance[]>(1000 * 60 * 60);
|
|
|
|
|
2021-05-08 11:56:21 +02:00
|
|
|
export default async (job: Bull.Job<DeliverJobData>) => {
|
2019-02-07 15:37:39 +01:00
|
|
|
const { host } = new URL(job.data.to);
|
|
|
|
|
2019-11-06 16:02:18 +01:00
|
|
|
// ブロックしてたら中断
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.blockedHosts.includes(toPuny(host))) {
|
|
|
|
return 'skip (blocked)';
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:56:14 +01:00
|
|
|
// isSuspendedなら中断
|
2021-03-18 02:49:14 +01:00
|
|
|
let suspendedHosts = suspendedHostsCache.get(null);
|
|
|
|
if (suspendedHosts == null) {
|
|
|
|
suspendedHosts = await Instances.find({
|
|
|
|
where: {
|
2021-12-09 15:58:30 +01:00
|
|
|
isSuspended: true,
|
2021-03-18 02:49:14 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
suspendedHostsCache.set(null, suspendedHosts);
|
|
|
|
}
|
2020-01-29 21:56:14 +01:00
|
|
|
if (suspendedHosts.map(x => x.host).includes(toPuny(host))) {
|
|
|
|
return 'skip (suspended)';
|
2019-11-06 16:02:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 11:43:06 +02:00
|
|
|
try {
|
2019-03-09 02:18:59 +01:00
|
|
|
if (latest !== (latest = JSON.stringify(job.data.content, null, 2))) {
|
|
|
|
logger.debug(`delivering ${latest}`);
|
|
|
|
}
|
2019-03-06 14:55:47 +01:00
|
|
|
|
2018-04-05 11:43:06 +02:00
|
|
|
await request(job.data.user, job.data.to, job.data.content);
|
2019-02-07 07:00:44 +01:00
|
|
|
|
|
|
|
// Update stats
|
2019-02-07 15:37:39 +01:00
|
|
|
registerOrFetchInstanceDoc(host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.update(i.id, {
|
|
|
|
latestRequestSentAt: new Date(),
|
|
|
|
latestStatus: 200,
|
|
|
|
lastCommunicatedAt: new Date(),
|
2021-12-09 15:58:30 +01:00
|
|
|
isNotResponding: false,
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
2019-02-08 08:58:57 +01:00
|
|
|
|
2020-07-26 04:04:07 +02:00
|
|
|
fetchInstanceMetadata(i);
|
2019-11-05 14:14:42 +01:00
|
|
|
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.requestSent(i.host, true);
|
2022-02-05 16:13:52 +01:00
|
|
|
apRequestChart.deliverSucc();
|
2022-02-08 15:43:51 +01:00
|
|
|
federationChart.deliverd(i.host, true);
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
2019-03-09 00:57:55 +01:00
|
|
|
|
|
|
|
return 'Success';
|
2018-04-06 23:22:03 +02:00
|
|
|
} catch (res) {
|
2019-02-07 07:00:44 +01:00
|
|
|
// Update stats
|
2019-02-07 15:37:39 +01:00
|
|
|
registerOrFetchInstanceDoc(host).then(i => {
|
2019-04-07 14:50:36 +02:00
|
|
|
Instances.update(i.id, {
|
|
|
|
latestRequestSentAt: new Date(),
|
2021-10-16 10:16:24 +02:00
|
|
|
latestStatus: res instanceof StatusError ? res.statusCode : null,
|
2021-12-09 15:58:30 +01:00
|
|
|
isNotResponding: true,
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
2019-02-08 08:58:57 +01:00
|
|
|
|
|
|
|
instanceChart.requestSent(i.host, false);
|
2022-02-05 16:13:52 +01:00
|
|
|
apRequestChart.deliverFail();
|
2022-02-08 15:43:51 +01:00
|
|
|
federationChart.deliverd(i.host, false);
|
2019-02-07 07:00:44 +01:00
|
|
|
});
|
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
if (res instanceof StatusError) {
|
2019-03-09 00:57:55 +01:00
|
|
|
// 4xx
|
2021-10-16 10:16:24 +02:00
|
|
|
if (res.isClientError) {
|
2018-10-04 18:58:41 +02:00
|
|
|
// HTTPステータスコード4xxはクライアントエラーであり、それはつまり
|
|
|
|
// 何回再送しても成功することはないということなのでエラーにはしないでおく
|
2019-03-09 00:57:55 +01:00
|
|
|
return `${res.statusCode} ${res.statusMessage}`;
|
2018-10-04 18:58:41 +02:00
|
|
|
}
|
2019-03-07 21:22:14 +01:00
|
|
|
|
2019-03-09 00:57:55 +01:00
|
|
|
// 5xx etc.
|
|
|
|
throw `${res.statusCode} ${res.statusMessage}`;
|
2018-04-06 23:22:03 +02:00
|
|
|
} else {
|
2019-03-09 00:57:55 +01:00
|
|
|
// DNS error, socket error, timeout ...
|
|
|
|
throw res;
|
2018-04-06 23:22:03 +02:00
|
|
|
}
|
2018-04-05 11:43:06 +02:00
|
|
|
}
|
2018-04-04 16:22:48 +02:00
|
|
|
};
|