-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
144 lines (123 loc) · 4.07 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Context, Hono } from "hono";
const app = new Hono();
// Mastodonエイリアス
const MASTODON_ALIASES: Record<string, string> = {
"@[email protected]": "@[email protected]",
};
const getMastodonAliasInfo = (handle: string, instance: string) => {
const alias = MASTODON_ALIASES[`@${handle}@${instance}`];
return alias && alias.match(/^@(?<handle>[^@]+)@(?<instance>[^@]+)$/)?.groups;
};
app.get("/.well-known/webfinger", (c: Context) => {
const { resource } = c.req.query();
if (!resource) {
return c.json({ error: "resource query is required" }, 400);
}
const resourceMatch = resource.match(
/^(acct:(?<handle>[^@]+)@(?<instance>[^@]+)|https?:\/\/(?<instance>[^\/]+)\/(@(?<handle>[^\/]+)|users\/(?<handle>[^\/]+)))$/,
);
if (!resourceMatch?.groups) {
return c.json({ error: "invalid resource format" }, 400);
}
const { handle, instance } = resourceMatch.groups;
const aliasInfo = getMastodonAliasInfo(handle, instance);
if (!aliasInfo) {
return c.json({ error: "Not Found" }, 404);
}
const { handle: targetHandle, instance: targetInstance } = aliasInfo;
return c.json({
subject: `acct:${targetHandle}@${targetInstance}`,
aliases: [
`https://${targetInstance}/@${targetHandle}`,
`https://${targetInstance}/users/${targetHandle}`,
],
links: [
{
rel: "http://webfinger.net/rel/profile-page",
type: "text/html",
href: `https://${targetInstance}/@${targetHandle}`,
},
{
rel: "self",
type: "application/activity+json",
href: `https://${targetInstance}/users/${targetHandle}`,
},
{
rel: "http://ostatus.org/schema/1.0/subscribe",
template: `https://${targetInstance}/authorize_interaction?uri={uri}`,
},
],
});
});
const redirectMastodonUser = (c: Context, handle: string) => {
const instance = c.req.header("Host");
if (!instance) {
return c.json({ error: "Host header is required" }, 400);
}
const aliasInfo = getMastodonAliasInfo(handle, instance);
if (!aliasInfo) {
return c.json({ error: "Not Found" }, 404);
}
const { handle: targetHandle, instance: targetInstance } = aliasInfo;
return c.redirect(`https://${targetInstance}/@${targetHandle}`, 308);
};
app.get("/users/:handle", (c: Context) => {
const { handle } = c.req.param();
return redirectMastodonUser(c, handle);
});
// Bluesky DID
const BLUESKY_DIDS: Record<string, string> = {
"@puzakura.com": "did:plc:bsxc4xeomcekctnqkojxws42",
};
const getBlueskyDidInfo = (handle: string) => {
const did = BLUESKY_DIDS[`@${handle}`];
return did && did.match(/^(?<did>did:plc:[^@]+)$/)?.groups;
};
app.get("/.well-known/atproto-did", (c: Context) => {
const handle = c.req.header("Host");
if (!handle) {
return c.json({ error: "Host header is required" }, 400);
}
const didInfo = getBlueskyDidInfo(handle);
if (!didInfo) {
return c.json({ error: "Not Found" }, 404);
}
const { did } = didInfo;
return c.text(`${did}`);
});
const redirectBlueskyUser = (c: Context) => {
const handle = c.req.header("Host");
if (!handle) {
return c.json({ error: "Host header is required" }, 400);
}
const didInfo = getBlueskyDidInfo(handle);
if (!didInfo) {
return c.json({ error: "Not Found" }, 404);
}
const { did: targetDid } = didInfo;
return c.redirect(`https://bsky.app/profile/${targetDid}`, 308);
};
// Xエイリアス
const X_ALIAS = "puzakura";
const redirectXUser = (c: Context) => {
return c.redirect(`https://x.com/${X_ALIAS}`, 308);
}
app.get("/:path", (c: Context) => {
const { path } = c.req.param();
if (path === "bluesky") {
return redirectBlueskyUser(c);
} else if (path === "x") {
return redirectXUser(c);
}
const pathMatch = path.match(/^@(?<handle>[^@]+)$/);
if (!pathMatch?.groups) {
return c.json({ error: "invalid path format" }, 400);
}
const { handle } = pathMatch.groups;
return redirectMastodonUser(c, handle);
});
app.onError((error: Context, c: Context) => {
console.error(error);
return c.json({ error: "Internal Server Error" }, 500);
});
Deno.serve(app.fetch);