This commit is contained in:
syuilo 2019-03-08 19:45:01 +09:00
parent f89f6f2d4a
commit 565c1afc20

View file

@ -14,7 +14,7 @@ import Instance from '../../models/instance';
export const logger = apLogger.createSubLogger('deliver'); export const logger = apLogger.createSubLogger('deliver');
export default (user: ILocalUser, url: string, object: any) => new Promise(async (resolve, reject) => { export default async (user: ILocalUser, url: string, object: any) => {
logger.info(`--> ${url}`); logger.info(`--> ${url}`);
const timeout = 10 * 1000; const timeout = 10 * 1000;
@ -32,53 +32,57 @@ export default (user: ILocalUser, url: string, object: any) => new Promise(async
sha256.update(data); sha256.update(data);
const hash = sha256.digest('base64'); const hash = sha256.digest('base64');
const addr = await resolveAddr(hostname).catch(e => reject(e)); const addr = await resolveAddr(hostname);
if (!addr) return; if (!addr) return;
const req = request({ const _ = new Promise((resolve, reject) => {
protocol, const req = request({
hostname: addr, protocol,
setHost: false, hostname: addr,
port, setHost: false,
method: 'POST', port,
path: pathname + search, method: 'POST',
timeout, path: pathname + search,
headers: { timeout,
'Host': host, headers: {
'User-Agent': config.userAgent, 'Host': host,
'Content-Type': 'application/activity+json', 'User-Agent': config.userAgent,
'Digest': `SHA-256=${hash}` 'Content-Type': 'application/activity+json',
} 'Digest': `SHA-256=${hash}`
}, res => { }
if (res.statusCode >= 400) { }, res => {
logger.warn(`${url} --> ${res.statusCode}`); if (res.statusCode >= 400) {
reject(res); logger.warn(`${url} --> ${res.statusCode}`);
} else { reject(res);
logger.succ(`${url} --> ${res.statusCode}`); } else {
resolve(); logger.succ(`${url} --> ${res.statusCode}`);
} resolve();
}
});
sign(req, {
authorizationHeaderName: 'Signature',
key: user.keypair,
keyId: `${config.url}/users/${user._id}/publickey`,
headers: ['date', 'host', 'digest']
});
// Signature: Signature ... => Signature: ...
let sig = req.getHeader('Signature').toString();
sig = sig.replace(/^Signature /, '');
req.setHeader('Signature', sig);
req.on('timeout', () => req.abort());
req.on('error', e => {
if (req.aborted) reject('timeout');
reject(e);
});
req.end(data);
}); });
sign(req, { await _;
authorizationHeaderName: 'Signature',
key: user.keypair,
keyId: `${config.url}/users/${user._id}/publickey`,
headers: ['date', 'host', 'digest']
});
// Signature: Signature ... => Signature: ...
let sig = req.getHeader('Signature').toString();
sig = sig.replace(/^Signature /, '');
req.setHeader('Signature', sig);
req.on('timeout', () => req.abort());
req.on('error', e => {
if (req.aborted) reject('timeout');
reject(e);
});
req.end(data);
//#region Log //#region Log
publishApLogStream({ publishApLogStream({
@ -88,7 +92,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise(async
actor: user.username actor: user.username
}); });
//#endregion //#endregion
}); };
/** /**
* Resolve host (with cached, asynchrony) * Resolve host (with cached, asynchrony)