2019-09-26 23:50:34 +03:00
|
|
|
import * as Router from '@koa/router';
|
2019-02-05 10:42:55 +02:00
|
|
|
|
2021-08-19 15:55:45 +03:00
|
|
|
import config from '@/config/index';
|
2021-11-11 19:02:25 +02:00
|
|
|
import * as Acct from 'misskey-js/built/acct';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { links } from './nodeinfo';
|
2021-08-19 16:04:15 +03:00
|
|
|
import { escapeAttribute, escapeValue } from '@/prelude/xml';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { Users } from '@/models/index';
|
|
|
|
import { User } from '@/models/entities/user';
|
2019-02-05 10:42:55 +02:00
|
|
|
|
|
|
|
// Init router
|
|
|
|
const router = new Router();
|
|
|
|
|
2019-02-20 09:13:43 +02:00
|
|
|
const XRD = (...x: { element: string, value?: string, attributes?: Record<string, string> }[]) =>
|
|
|
|
`<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">${x.map(({ element, value, attributes }) =>
|
|
|
|
`<${
|
|
|
|
Object.entries(typeof attributes === 'object' && attributes || {}).reduce((a, [k, v]) => `${a} ${k}="${escapeAttribute(v)}"`, element)
|
|
|
|
}${
|
|
|
|
typeof value === 'string' ? `>${escapeValue(value)}</${element}` : '/'
|
|
|
|
}>`).reduce((a, c) => a + c, '')}</XRD>`;
|
|
|
|
|
2020-06-27 17:25:06 +03:00
|
|
|
const allPath = '/.well-known/(.*)';
|
2019-02-05 10:42:55 +02:00
|
|
|
const webFingerPath = '/.well-known/webfinger';
|
2019-02-20 09:13:43 +02:00
|
|
|
const jrd = 'application/jrd+json';
|
|
|
|
const xrd = 'application/xrd+xml';
|
2019-02-05 10:42:55 +02:00
|
|
|
|
2020-01-15 21:46:43 +02:00
|
|
|
router.use(allPath, async (ctx, next) => {
|
|
|
|
ctx.set({
|
|
|
|
'Access-Control-Allow-Headers': 'Accept',
|
|
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Expose-Headers': 'Vary',
|
|
|
|
});
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
|
|
|
router.options(allPath, async ctx => {
|
|
|
|
ctx.status = 204;
|
|
|
|
});
|
|
|
|
|
2019-02-05 10:42:55 +02:00
|
|
|
router.get('/.well-known/host-meta', async ctx => {
|
2019-02-20 09:13:43 +02:00
|
|
|
ctx.set('Content-Type', xrd);
|
|
|
|
ctx.body = XRD({ element: 'Link', attributes: {
|
|
|
|
type: xrd,
|
2021-11-13 12:10:14 +02:00
|
|
|
template: `${config.url}${webFingerPath}?resource={uri}`,
|
|
|
|
} });
|
2019-02-05 10:42:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/.well-known/host-meta.json', async ctx => {
|
2019-02-20 09:13:43 +02:00
|
|
|
ctx.set('Content-Type', jrd);
|
2019-02-05 10:42:55 +02:00
|
|
|
ctx.body = {
|
|
|
|
links: [{
|
|
|
|
rel: 'lrdd',
|
2019-02-20 09:13:43 +02:00
|
|
|
type: jrd,
|
2021-11-13 12:10:14 +02:00
|
|
|
template: `${config.url}${webFingerPath}?resource={uri}`,
|
|
|
|
}],
|
2019-02-05 10:42:55 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/.well-known/nodeinfo', async ctx => {
|
|
|
|
ctx.body = { links };
|
|
|
|
});
|
|
|
|
|
2021-05-04 09:05:34 +03:00
|
|
|
/* TODO
|
|
|
|
router.get('/.well-known/change-password', async ctx => {
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2019-02-05 10:42:55 +02:00
|
|
|
router.get(webFingerPath, async ctx => {
|
2019-04-07 15:50:36 +03:00
|
|
|
const fromId = (id: User['id']): Record<string, any> => ({
|
|
|
|
id,
|
2020-01-01 19:47:20 +02:00
|
|
|
host: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
isSuspended: false,
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
|
2019-02-05 10:42:55 +02:00
|
|
|
const generateQuery = (resource: string) =>
|
|
|
|
resource.startsWith(`${config.url.toLowerCase()}/users/`) ?
|
2019-04-12 19:43:22 +03:00
|
|
|
fromId(resource.split('/').pop()!) :
|
2021-11-11 19:02:25 +02:00
|
|
|
fromAcct(Acct.parse(
|
2019-04-12 19:43:22 +03:00
|
|
|
resource.startsWith(`${config.url.toLowerCase()}/@`) ? resource.split('/').pop()! :
|
2019-02-05 10:42:55 +02:00
|
|
|
resource.startsWith('acct:') ? resource.slice('acct:'.length) :
|
|
|
|
resource));
|
|
|
|
|
2021-11-11 19:02:25 +02:00
|
|
|
const fromAcct = (acct: Acct.Acct): Record<string, any> | number =>
|
2019-02-05 10:42:55 +02:00
|
|
|
!acct.host || acct.host === config.host.toLowerCase() ? {
|
|
|
|
usernameLower: acct.username,
|
2020-01-01 19:47:20 +02:00
|
|
|
host: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
isSuspended: false,
|
2019-02-05 10:42:55 +02:00
|
|
|
} : 422;
|
|
|
|
|
|
|
|
if (typeof ctx.query.resource !== 'string') {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = generateQuery(ctx.query.resource.toLowerCase());
|
|
|
|
|
|
|
|
if (typeof query === 'number') {
|
|
|
|
ctx.status = query;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const user = await Users.findOne(query);
|
2019-02-05 10:42:55 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (user == null) {
|
2019-02-05 10:42:55 +02:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:13:43 +02:00
|
|
|
const subject = `acct:${user.username}@${config.host}`;
|
|
|
|
const self = {
|
|
|
|
rel: 'self',
|
|
|
|
type: 'application/activity+json',
|
2021-12-09 16:58:30 +02:00
|
|
|
href: `${config.url}/users/${user.id}`,
|
2019-02-05 10:42:55 +02:00
|
|
|
};
|
2019-02-20 09:13:43 +02:00
|
|
|
const profilePage = {
|
|
|
|
rel: 'http://webfinger.net/rel/profile-page',
|
|
|
|
type: 'text/html',
|
2021-12-09 16:58:30 +02:00
|
|
|
href: `${config.url}/@${user.username}`,
|
2019-02-20 09:13:43 +02:00
|
|
|
};
|
|
|
|
const subscribe = {
|
|
|
|
rel: 'http://ostatus.org/schema/1.0/subscribe',
|
2021-12-09 16:58:30 +02:00
|
|
|
template: `${config.url}/authorize-follow?acct={uri}`,
|
2019-02-20 09:13:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ctx.accepts(jrd, xrd) === xrd) {
|
|
|
|
ctx.body = XRD(
|
|
|
|
{ element: 'Subject', value: subject },
|
|
|
|
{ element: 'Link', attributes: self },
|
|
|
|
{ element: 'Link', attributes: profilePage },
|
|
|
|
{ element: 'Link', attributes: subscribe });
|
|
|
|
ctx.type = xrd;
|
|
|
|
} else {
|
|
|
|
ctx.body = {
|
|
|
|
subject,
|
2021-12-09 16:58:30 +02:00
|
|
|
links: [self, profilePage, subscribe],
|
2019-02-20 09:13:43 +02:00
|
|
|
};
|
|
|
|
ctx.type = jrd;
|
|
|
|
}
|
2019-02-05 10:42:55 +02:00
|
|
|
|
2019-02-20 09:13:43 +02:00
|
|
|
ctx.vary('Accept');
|
2019-02-05 10:42:55 +02:00
|
|
|
ctx.set('Cache-Control', 'public, max-age=180');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return 404 for other .well-known
|
2020-01-15 21:46:43 +02:00
|
|
|
router.all(allPath, async ctx => {
|
2019-02-05 10:42:55 +02:00
|
|
|
ctx.status = 404;
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|