More puny

This commit is contained in:
syuilo
2019-04-10 00:59:41 +09:00
parent 72a5f7b1e2
commit 236d72685d
14 changed files with 52 additions and 55 deletions

View File

@ -8,14 +8,13 @@ import { resolveImage } from './image';
import { IRemoteUser, User } from '../../../models/entities/user';
import { fromHtml } from '../../../mfm/fromHtml';
import { ITag, extractHashtags } from './tag';
import { toUnicode } from 'punycode';
import { unique, concat, difference } from '../../../prelude/array';
import { extractPollFromQuestion } from './question';
import vote from '../../../services/note/polls/vote';
import { apLogger } from '../logger';
import { DriveFile } from '../../../models/entities/drive-file';
import { deliverQuestionUpdate } from '../../../services/note/polls/update';
import { extractDbHost } from '../../../misc/convert-host';
import { extractDbHost, toPuny } from '../../../misc/convert-host';
import { Notes, Emojis, Polls } from '../../../models';
import { Note } from '../../../models/entities/note';
import { IObject, INote } from '../type';
@ -246,8 +245,8 @@ export async function resolveNote(value: string | IObject, resolver?: Resolver):
return await createNote(uri, resolver);
}
export async function extractEmojis(tags: ITag[], host_: string) {
const host = toUnicode(host_.toLowerCase());
export async function extractEmojis(tags: ITag[], host: string) {
host = toPuny(host);
if (!tags) return [];

View File

@ -1,5 +1,4 @@
import * as promiseLimit from 'promise-limit';
import { toUnicode } from 'punycode';
import config from '../../../config';
import Resolver from '../resolver';
@ -33,7 +32,7 @@ const logger = apLogger;
* @param uri Fetch target URI
*/
function validatePerson(x: any, uri: string) {
const expectHost = toUnicode(new URL(uri).hostname.toLowerCase());
const expectHost = toPuny(new URL(uri).hostname);
if (x == null) {
return new Error('invalid person: object is null');
@ -63,7 +62,7 @@ function validatePerson(x: any, uri: string) {
return new Error('invalid person: id is not a string');
}
const idHost = toUnicode(new URL(x.id).hostname.toLowerCase());
const idHost = toPuny(new URL(x.id).hostname);
if (idHost !== expectHost) {
return new Error('invalid person: id has different host');
}
@ -72,7 +71,7 @@ function validatePerson(x: any, uri: string) {
return new Error('invalid person: publicKey.id is not a string');
}
const publicKeyIdHost = toUnicode(new URL(x.publicKey.id).hostname.toLowerCase());
const publicKeyIdHost = toPuny(new URL(x.publicKey.id).hostname);
if (publicKeyIdHost !== expectHost) {
return new Error('invalid person: publicKey.id has different host');
}

View File

@ -4,7 +4,6 @@ import { URL } from 'url';
import * as crypto from 'crypto';
import { lookup, IRunOptions } from 'lookup-dns-cache';
import * as promiseAny from 'promise-any';
import { toUnicode } from 'punycode';
import config from '../../config';
import { ILocalUser } from '../../models/entities/user';
@ -12,6 +11,7 @@ import { publishApLogStream } from '../../services/stream';
import { apLogger } from './logger';
import { UserKeypairs } from '../../models';
import fetchMeta from '../../misc/fetch-meta';
import { toPuny } from '../../misc/convert-host';
export const logger = apLogger.createSubLogger('deliver');
@ -25,7 +25,7 @@ export default async (user: ILocalUser, url: string, object: any) => {
// ブロックしてたら中断
// TODO: いちいちデータベースにアクセスするのはコスト高そうなのでどっかにキャッシュしておく
const meta = await fetchMeta();
if (meta.blockedHosts.includes(toUnicode(host))) return;
if (meta.blockedHosts.includes(toPuny(host))) return;
const data = JSON.stringify(object);

View File

@ -1,4 +1,3 @@
import { toUnicode, toASCII } from 'punycode';
import webFinger from './webfinger';
import config from '../config';
import { createPerson, updatePerson } from './activitypub/models/person';
@ -7,31 +6,27 @@ import { remoteLogger } from './logger';
import chalk from 'chalk';
import { User, IRemoteUser } from '../models/entities/user';
import { Users } from '../models';
import { toPuny } from '../misc/convert-host';
const logger = remoteLogger.createSubLogger('resolve-user');
export async function resolveUser(username: string, _host: string, option?: any, resync = false): Promise<User> {
export async function resolveUser(username: string, host: string, option?: any, resync = false): Promise<User> {
const usernameLower = username.toLowerCase();
host = toPuny(host);
if (_host == null) {
if (host == null) {
logger.info(`return local user: ${usernameLower}`);
return await Users.findOne({ usernameLower, host: null });
}
const configHostAscii = toASCII(config.host).toLowerCase();
const configHost = toUnicode(configHostAscii);
const hostAscii = toASCII(_host).toLowerCase();
const host = toUnicode(hostAscii);
if (configHost == host) {
if (config.host == host) {
logger.info(`return local user: ${usernameLower}`);
return await Users.findOne({ usernameLower, host: null });
}
const user = await Users.findOne({ usernameLower, host }, option);
const acctLower = `${usernameLower}@${hostAscii}`;
const acctLower = `${usernameLower}@${host}`;
if (user == null) {
const self = await resolveSelf(acctLower);
@ -51,7 +46,7 @@ export async function resolveUser(username: string, _host: string, option?: any,
// validate uri
const uri = new URL(self.href);
if (uri.hostname !== hostAscii) {
if (uri.hostname !== host) {
throw new Error(`Invalied uri`);
}