WebFingerリクエストで Proxy, Keep-Alive などをサポート (#4658)

* no webfinger.js

* Fix: エラーメッセージがくどい
This commit is contained in:
MeiMei 2019-04-10 12:28:59 +09:00 committed by syuilo
parent 83b7010d6a
commit 03a3c56a54
4 changed files with 34 additions and 78 deletions

View file

@ -250,7 +250,6 @@
"vuex": "3.1.0", "vuex": "3.1.0",
"vuex-persistedstate": "2.5.4", "vuex-persistedstate": "2.5.4",
"web-push": "3.3.3", "web-push": "3.3.3",
"webfinger.js": "2.7.0",
"webpack": "4.28.4", "webpack": "4.28.4",
"webpack-cli": "3.2.3", "webpack-cli": "3.2.3",
"websocket": "1.0.28", "websocket": "1.0.28",

View file

@ -1,65 +0,0 @@
declare module 'webfinger.js' {
interface IWebFingerConstructorConfig {
tls_only?: boolean;
webfist_fallback?: boolean;
uri_fallback?: boolean;
request_timeout?: number;
}
type JRDProperties = { [type: string]: string };
interface IJRDLink {
rel: string;
type?: string;
href?: string;
template?: string;
titles?: { [lang: string]: string };
properties?: JRDProperties;
}
interface IJRD {
subject?: string;
expires?: Date;
aliases?: string[];
properties?: JRDProperties;
links?: IJRDLink[];
}
interface IIDXLinks {
'avatar': IJRDLink[];
'remotestorage': IJRDLink[];
'blog': IJRDLink[];
'vcard': IJRDLink[];
'updates': IJRDLink[];
'share': IJRDLink[];
'profile': IJRDLink[];
'webfist': IJRDLink[];
'camlistore': IJRDLink[];
[type: string]: IJRDLink[];
}
interface IIDXProperties {
'name': string;
[type: string]: string;
}
interface IIDX {
links: IIDXLinks;
properties: IIDXProperties;
}
interface ILookupCallbackResult {
object: IJRD;
json: string;
idx: IIDX;
}
type LookupCallback = (err: Error | string, result?: ILookupCallbackResult) => void;
export class WebFinger {
constructor(config?: IWebFingerConstructorConfig);
public lookup(address: string, cb: LookupCallback): NodeJS.Timeout;
public lookupLink(address: string, rel: string, cb: IJRDLink): void;
}
}

View file

@ -79,8 +79,8 @@ export default async (username: string, _host: string, option?: any, resync?: bo
async function resolveSelf(acctLower: string) { async function resolveSelf(acctLower: string) {
logger.info(`WebFinger for ${chalk.yellow(acctLower)}`); logger.info(`WebFinger for ${chalk.yellow(acctLower)}`);
const finger = await webFinger(acctLower).catch(e => { const finger = await webFinger(acctLower).catch(e => {
logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${e.message} (${e.status})`); logger.error(`Failed to WebFinger for ${chalk.yellow(acctLower)}: ${ e.statusCode || e.message }`);
throw e; throw new Error(`Failed to WebFinger for ${acctLower}: ${ e.statusCode || e.message }`);
}); });
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self'); const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
if (!self) { if (!self) {

View file

@ -1,6 +1,7 @@
import { WebFinger } from 'webfinger.js'; import config from '../config';
import * as request from 'request-promise-native';
const webFinger = new WebFinger({ }); import { URL } from 'url';
import { query as urlQuery } from '../prelude/url';
type ILink = { type ILink = {
href: string; href: string;
@ -12,12 +13,33 @@ type IWebFinger = {
subject: string; subject: string;
}; };
export default async function resolve(query: any): Promise<IWebFinger> { export default async function(query: string): Promise<IWebFinger> {
return await new Promise((res, rej) => webFinger.lookup(query, (error: Error | string, result: any) => { const url = genUrl(query);
if (error) {
return rej(error); return await request({
url,
proxy: config.proxy,
timeout: 10 * 1000,
forever: true,
headers: {
'User-Agent': config.userAgent,
Accept: 'application/jrd+json, application/json'
},
json: true
});
} }
res(result.object); function genUrl(query: string) {
})) as IWebFinger; if (query.match(/^https?:\/\//)) {
const u = new URL(query);
return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
}
const m = query.match(/^([^@]+)@(.*)/);
if (m) {
const hostname = m[2];
return `https://${hostname}/.well-known/webfinger?` + urlQuery({ resource: `acct:${query}` });
}
throw new Error(`Invalied query (${query})`);
} }