summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/request.ts
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2019-07-28 09:49:02 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2019-07-28 09:49:02 +0900
commit14736620ec1e0040c55c83cfc163049b36f7af23 (patch)
tree2f00431dceae92da1b8da6a40ac66d3364282570 /src/remote/activitypub/request.ts
parent「削除して編集」機能を追加 (#5182) (diff)
downloadsharkey-14736620ec1e0040c55c83cfc163049b36f7af23.tar.gz
sharkey-14736620ec1e0040c55c83cfc163049b36f7af23.tar.bz2
sharkey-14736620ec1e0040c55c83cfc163049b36f7af23.zip
HTTPリクエストのKeep-AliveとPrxoy対応など (#5226)
* DriveのKeep-Alive, Proxy と APのProxy対応 * request系でKeep-Aliveするように * fix lookup-dns-cache.d.ts * remove debug output
Diffstat (limited to 'src/remote/activitypub/request.ts')
-rw-r--r--src/remote/activitypub/request.ts49
1 files changed, 12 insertions, 37 deletions
diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts
index 6d18e53281..a48bc1e3f3 100644
--- a/src/remote/activitypub/request.ts
+++ b/src/remote/activitypub/request.ts
@@ -1,8 +1,7 @@
-import { request } from 'https';
+import * as https from 'https';
import { sign } from 'http-signature';
import * as crypto from 'crypto';
-import { lookup, IRunOptions } from 'lookup-dns-cache';
-import * as promiseAny from 'promise-any';
+import * as cache from 'lookup-dns-cache';
import config from '../../config';
import { ILocalUser } from '../../models/entities/user';
@@ -12,9 +11,16 @@ import { UserKeypairs, Instances } from '../../models';
import { fetchMeta } from '../../misc/fetch-meta';
import { toPuny } from '../../misc/convert-host';
import { ensure } from '../../prelude/ensure';
+import * as httpsProxyAgent from 'https-proxy-agent';
export const logger = apLogger.createSubLogger('deliver');
+const agent = config.proxy
+ ? new httpsProxyAgent(config.proxy)
+ : new https.Agent({
+ lookup: cache.lookup,
+ });
+
export default async (user: ILocalUser, url: string, object: any) => {
const timeout = 10 * 1000;
@@ -47,24 +53,20 @@ export default async (user: ILocalUser, url: string, object: any) => {
sha256.update(data);
const hash = sha256.digest('base64');
- const addr = await resolveAddr(hostname);
- if (!addr) return;
-
const keypair = await UserKeypairs.findOne({
userId: user.id
}).then(ensure);
await new Promise((resolve, reject) => {
- const req = request({
+ const req = https.request({
+ agent,
protocol,
- hostname: addr,
- setHost: false,
+ hostname,
port,
method: 'POST',
path: pathname + search,
timeout,
headers: {
- 'Host': host,
'User-Agent': config.userAgent,
'Content-Type': 'application/activity+json',
'Digest': `SHA-256=${hash}`
@@ -110,30 +112,3 @@ export default async (user: ILocalUser, url: string, object: any) => {
});
//#endregion
};
-
-/**
- * Resolve host (with cached, asynchrony)
- */
-async function resolveAddr(domain: string) {
- const af = config.outgoingAddressFamily || 'ipv4';
- const useV4 = af == 'ipv4' || af == 'dual';
- const useV6 = af == 'ipv6' || af == 'dual';
-
- const promises = [];
-
- if (!useV4 && !useV6) throw 'No usable address family available';
- if (useV4) promises.push(resolveAddrInner(domain, { family: 4 }));
- if (useV6) promises.push(resolveAddrInner(domain, { family: 6 }));
-
- // v4/v6で先に取得できた方を採用する
- return await promiseAny(promises);
-}
-
-function resolveAddrInner(domain: string, options: IRunOptions = {}): Promise<string> {
- return new Promise((res, rej) => {
- lookup(domain, options, (error, address) => {
- if (error) return rej(error);
- return res(Array.isArray(address) ? address[0] : address);
- });
- });
-}