From 5b039a1beea20d300d1cb2579674383ac763176d Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Tue, 4 Sep 2018 17:44:21 +0900 Subject: [PATCH] Add User-Agent header (#2602) --- src/config/load.ts | 2 ++ src/config/types.ts | 1 + src/remote/activitypub/request.ts | 1 + src/remote/activitypub/resolver.ts | 3 ++- src/services/drive/upload-from-url.ts | 7 ++++++- 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/config/load.ts b/src/config/load.ts index 8929cf8d3e..3a1bac3201 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -7,6 +7,7 @@ import { URL } from 'url'; import * as yaml from 'js-yaml'; import { Source, Mixin } from './types'; import isUrl = require('is-url'); +const pkg = require('../../package.json'); /** * Path of configuration directory @@ -43,6 +44,7 @@ export default function load() { mixin.stats_url = `${mixin.scheme}://${mixin.host}/stats`; mixin.status_url = `${mixin.scheme}://${mixin.host}/status`; mixin.drive_url = `${mixin.scheme}://${mixin.host}/files`; + mixin.user_agent = `Misskey/${pkg.version} (${config.url})`; if (config.localDriveCapacityMb == null) config.localDriveCapacityMb = 256; if (config.remoteDriveCapacityMb == null) config.remoteDriveCapacityMb = 8; diff --git a/src/config/types.ts b/src/config/types.ts index a1dc9a5bd4..003185accd 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -114,6 +114,7 @@ export type Mixin = { status_url: string; dev_url: string; drive_url: string; + user_agent: string; }; export type Config = Source & Mixin; diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index d739d08e15..07f0ecca8b 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -27,6 +27,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso method: 'POST', path: pathname + search, headers: { + 'User-Agent': config.user_agent, 'Content-Type': 'application/activity+json', 'Digest': `SHA-256=${hash}` } diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 0b053ca774..9bbe474d35 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -1,7 +1,7 @@ import * as request from 'request-promise-native'; import * as debug from 'debug'; import { IObject } from './type'; -//import config from '../../config'; +import config from '../../config'; const log = debug('misskey:activitypub:resolver'); @@ -51,6 +51,7 @@ export default class Resolver { const object = await request({ url: value, headers: { + 'User-Agent': config.user_agent, Accept: 'application/activity+json, application/ld+json' }, json: true diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts index 4e297d3bb1..0cf21ea5a2 100644 --- a/src/services/drive/upload-from-url.ts +++ b/src/services/drive/upload-from-url.ts @@ -34,7 +34,12 @@ export default async (url: string, user: IUser, folderId: mongodb.ObjectID = nul // write content at URL to temp file await new Promise((res, rej) => { const writable = fs.createWriteStream(path); - request(url) + request({ + url, + headers: { + 'User-Agent': config.user_agent + } + }) .on('error', rej) .on('end', () => { writable.close();