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