From 33a9783ae5cf18e913cfe3f0223ed89aa94e63b4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 9 Apr 2019 23:59:32 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=89=E3=83=A1=E3=82=A4=E3=83=B3=E3=81=AF?= =?UTF-8?q?=E5=B8=B8=E3=81=ABPunycode=E3=81=A7=E4=BF=9D=E5=AD=98=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/load.ts | 7 ++++--- src/migrate.ts | 15 ++++++++------- src/misc/acct/render.ts | 2 +- src/misc/convert-host.ts | 15 +++++---------- src/models/repositories/user.ts | 2 +- src/queue/processors/db/import-following.ts | 4 ++-- src/queue/processors/db/import-user-lists.ts | 4 ++-- src/remote/activitypub/models/person.ts | 3 ++- src/server/api/private/signup.ts | 3 ++- src/server/api/stream/channels/hybrid-timeline.ts | 2 +- src/services/register-or-fetch-instance-doc.ts | 3 +++ 11 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/config/load.ts b/src/config/load.ts index 50ae47d9e..4d174d0e2 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 * as pkg from '../../package.json'; +import { toPuny } from '../misc/convert-host'; /** * Path of configuration directory @@ -27,12 +28,12 @@ export default function load() { const url = validateUrl(config.url); - config.url = normalizeUrl(config.url); + config.url = toPuny(normalizeUrl(config.url)); config.port = config.port || parseInt(process.env.PORT, 10); - mixin.host = url.host; - mixin.hostname = url.hostname; + mixin.host = toPuny(url.host); + mixin.hostname = toPuny(url.hostname); mixin.scheme = url.protocol.replace(/:$/, ''); mixin.wsScheme = mixin.scheme.replace('http', 'ws'); mixin.wsUrl = `${mixin.wsScheme}://${mixin.host}`; diff --git a/src/migrate.ts b/src/migrate.ts index 46df8429d..b9d6eb339 100644 --- a/src/migrate.ts +++ b/src/migrate.ts @@ -25,6 +25,7 @@ import { UserPublickey } from './models/entities/user-publickey'; import { UserKeypair } from './models/entities/user-keypair'; import { extractPublic } from './crypto_key'; import { Emoji } from './models/entities/emoji'; +import { toPuny } from './misc/convert-host'; const u = (config as any).mongodb.user ? encodeURIComponent((config as any).mongodb.user) : null; const p = (config as any).mongodb.pass ? encodeURIComponent((config as any).mongodb.pass) : null; @@ -87,7 +88,7 @@ async function main() { createdAt: user.createdAt || new Date(), username: user.username, usernameLower: user.username.toLowerCase(), - host: user.host, + host: toPuny(user.host), token: generateUserToken(), password: user.password, isAdmin: user.isAdmin, @@ -133,10 +134,10 @@ async function main() { followeeId: following.followeeId.toHexString(), // 非正規化 - followerHost: following._follower ? following._follower.host : null, + followerHost: following._follower ? toPuny(following._follower.host) : null, followerInbox: following._follower ? following._follower.inbox : null, followerSharedInbox: following._follower ? following._follower.sharedInbox : null, - followeeHost: following._followee ? following._followee.host : null, + followeeHost: following._followee ? toPuny(following._followee.host) : null, followeeInbox: following._followee ? following._followee.inbox : null, followeeSharedInbox: following._followee ? following._followee.sharedInbo : null }); @@ -159,7 +160,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -191,7 +192,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -210,7 +211,7 @@ async function main() { await DriveFiles.save({ id: file._id.toHexString(), userId: user._id.toHexString(), - userHost: user.host, + userHost: toPuny(user.host), createdAt: file.uploadDate || new Date(), md5: file.md5, name: file.filename, @@ -313,7 +314,7 @@ async function main() { aliases: emoji.aliases, url: emoji.url, uri: emoji.uri, - host: emoji.host, + host: toPuny(emoji.host), name: emoji.name }); } diff --git a/src/misc/acct/render.ts b/src/misc/acct/render.ts index 67e063fcb..094eceffe 100644 --- a/src/misc/acct/render.ts +++ b/src/misc/acct/render.ts @@ -1,5 +1,5 @@ import Acct from './type'; export default (user: Acct) => { - return user.host === null ? user.username : `${user.username}@${user.host}`; + return user.host == null ? user.username : `${user.username}@${user.host}`; }; diff --git a/src/misc/convert-host.ts b/src/misc/convert-host.ts index 8f2f1c7ab..f7feebd55 100644 --- a/src/misc/convert-host.ts +++ b/src/misc/convert-host.ts @@ -1,27 +1,22 @@ import config from '../config'; -import { toUnicode, toASCII } from 'punycode'; +import { toASCII } from 'punycode'; import { URL } from 'url'; export function getFullApAccount(username: string, host: string) { - return host ? `${username}@${toApHost(host)}` : `${username}@${toApHost(config.host)}`; + return host ? `${username}@${toPuny(host)}` : `${username}@${toPuny(config.host)}`; } export function isSelfHost(host: string) { if (host == null) return true; - return toApHost(config.host) === toApHost(host); + return toPuny(config.host) === toPuny(host); } export function extractDbHost(uri: string) { const url = new URL(uri); - return toDbHost(url.hostname); + return toPuny(url.hostname); } -export function toDbHost(host: string) { - if (host == null) return null; - return toUnicode(host.toLowerCase()); -} - -export function toApHost(host: string) { +export function toPuny(host: string) { if (host == null) return null; return toASCII(host.toLowerCase()); } diff --git a/src/models/repositories/user.ts b/src/models/repositories/user.ts index f1a51c1c5..3939e3142 100644 --- a/src/models/repositories/user.ts +++ b/src/models/repositories/user.ts @@ -166,7 +166,7 @@ export class UserRepository extends Repository { } public isLocalUser(user: User): user is ILocalUser { - return user.host === null; + return user.host == null; } public isRemoteUser(user: User): user is IRemoteUser { diff --git a/src/queue/processors/db/import-following.ts b/src/queue/processors/db/import-following.ts index 07a163949..aae24b22d 100644 --- a/src/queue/processors/db/import-following.ts +++ b/src/queue/processors/db/import-following.ts @@ -5,7 +5,7 @@ import follow from '../../../services/following/create'; import parseAcct from '../../../misc/acct/parse'; import { resolveUser } from '../../../remote/resolve-user'; import { downloadTextFile } from '../../../misc/download-text-file'; -import { isSelfHost, toDbHost } from '../../../misc/convert-host'; +import { isSelfHost, toPuny } from '../../../misc/convert-host'; import { Users, DriveFiles } from '../../../models'; const logger = queueLogger.createSubLogger('import-following'); @@ -35,7 +35,7 @@ export async function importFollowing(job: Bull.Job, done: any): Promise { host: null, usernameLower: username.toLowerCase() }) : await Users.findOne({ - host: toDbHost(host), + host: toPuny(host), usernameLower: username.toLowerCase() }); diff --git a/src/queue/processors/db/import-user-lists.ts b/src/queue/processors/db/import-user-lists.ts index ff896c1be..c7273ea6b 100644 --- a/src/queue/processors/db/import-user-lists.ts +++ b/src/queue/processors/db/import-user-lists.ts @@ -5,7 +5,7 @@ import parseAcct from '../../../misc/acct/parse'; import { resolveUser } from '../../../remote/resolve-user'; import { pushUserToUserList } from '../../../services/user-list/push'; import { downloadTextFile } from '../../../misc/download-text-file'; -import { isSelfHost, toDbHost } from '../../../misc/convert-host'; +import { isSelfHost, toPuny } from '../../../misc/convert-host'; import { DriveFiles, Users, UserLists, UserListJoinings } from '../../../models'; import { genId } from '../../../misc/gen-id'; @@ -47,7 +47,7 @@ export async function importUserLists(job: Bull.Job, done: any): Promise { host: null, usernameLower: username.toLowerCase() }) : await Users.findOne({ - host: toDbHost(host), + host: toPuny(host), usernameLower: username.toLowerCase() }); diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index fbc329e86..2362455e0 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -24,6 +24,7 @@ import { UserServiceLinking } from '../../../models/entities/user-service-linkin import { instanceChart, usersChart } from '../../../services/chart'; import { UserPublickey } from '../../../models/entities/user-publickey'; import { isDuplicateKeyValueError } from '../../../misc/is-duplicate-key-value-error'; +import { toPuny } from '../../../misc/convert-host'; const logger = apLogger; /** @@ -124,7 +125,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise { const body = ctx.request.body as any; @@ -103,7 +104,7 @@ export default async (ctx: Koa.BaseContext) => { createdAt: new Date(), username: username, usernameLower: username.toLowerCase(), - host: host, + host: toPuny(host), token: secret, password: hash, isAdmin: config.autoAdmin && usersCount === 0, diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts index 3f09dd839..30643aeda 100644 --- a/src/server/api/stream/channels/hybrid-timeline.ts +++ b/src/server/api/stream/channels/hybrid-timeline.ts @@ -24,7 +24,7 @@ export default class extends Channel { if (!( this.user.id === note.userId || this.following.includes(note.userId) || - note.user.host === null + note.user.host == null )) return; if (['followers', 'specified'].includes(note.visibility)) { diff --git a/src/services/register-or-fetch-instance-doc.ts b/src/services/register-or-fetch-instance-doc.ts index c96c8a1e3..459f538e9 100644 --- a/src/services/register-or-fetch-instance-doc.ts +++ b/src/services/register-or-fetch-instance-doc.ts @@ -2,10 +2,13 @@ import { Instance } from '../models/entities/instance'; import { Instances } from '../models'; import { federationChart } from './chart'; import { genId } from '../misc/gen-id'; +import { toPuny } from '../misc/convert-host'; export async function registerOrFetchInstanceDoc(host: string): Promise { if (host == null) return null; + host = toPuny(host); + const index = await Instances.findOne({ host }); if (index == null) {