This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
119 lines (101 loc) · 3.21 KB
/
server.js
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
const defaultAvatar = '';
const Koa = require('koa');
const Router = require('@koa/router');
const CORS = require('@koa/cors');
const koaViews = require('koa-views');
const koaStatic = require('koa-static');
const path = require('path');
const axios = require('axios');
// const compress = require('koa-compress');
const { default: sslify, xForwardedProtoResolver } = require('koa-sslify');
const app = new Koa();
app.use(
sslify({
resolver: xForwardedProtoResolver,
}),
);
app.use(CORS());
// app.use(compress());
app.use(koaStatic(path.join(__dirname, 'dist')));
app.use(
koaViews(path.join(__dirname, 'dist'), {
extension: 'ejs',
}),
);
const getNameByAddress = async (address) => {
try {
const name = (await axios.get(`https://rss3.domains/address/${address}`)).data;
return name?.rnsName.replace('.rss3', '') || name?.dasName || name?.ensName || '';
} catch (e) {
console.log(e);
return '';
}
};
const getName = async (host, url) => {
if (host.split('.').length > 2) {
return host.split('.').slice(0, -2).join('.');
} else {
return /^\/(.+?)\//.exec(url + '/')?.[1];
}
};
const getAddress = async (name) => {
try {
return (await axios.get(`https://rss3.domains/name/${name}`)).data.address;
} catch (e) {
console.log(e);
return '';
}
};
const getPersona = async (address) => {
try {
return (await axios.get(`https://prenode.rss3.dev/${address}`)).data;
} catch (e) {
console.log(e);
return null;
}
};
const router = new Router();
const injectMetadata = async (ctx) => {
// extract name
const host = ctx.host;
const aon = await getName(host, ctx.url);
let address = '';
// get address
if (/^0x[a-fA-F0-9]{40}$/.test(aon)) {
// try whether can use name
const name = await getNameByAddress(aon);
if (name) {
// redirect to name
const fullUrl = ctx.request.href;
const rootDomain = host.split('.').slice(-2).join('.');
const newURL = fullUrl
.replace(`${rootDomain}/${aon}`, `${name}.${rootDomain}`)
.replace('http://', 'https://');
console.log(newURL);
ctx.redirect(newURL);
return;
} else {
// no name found, use address
address = aon;
}
} else {
address = await getAddress(aon);
}
// request for persona
let persona = {};
if (address) {
persona = await getPersona(address);
}
// embed default data (so page don't need to request again)
await ctx.render('index', {
user: JSON.stringify(persona).replace(/\\/g, '\\\\'),
title: (persona?.profile?.name ? persona?.profile?.name + "'s " : '') + 'Cheers.Bio',
avatar: persona?.profile?.avatar?.[0]?.replace('ipfs://', 'https://ipfs.io/ipfs/') || defaultAvatar,
bio: persona?.profile?.bio?.replace(/\n/g, ' ') || 'Cheers.Bio',
url: ctx.request.href.replace('http://', 'https://'),
});
};
router.get('/(.*)', injectMetadata);
app.use(router.routes()).use(router.allowedMethods());
console.log('Server start...');
app.listen(process.env.PORT || 8080);