-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
150 lines (131 loc) · 3.99 KB
/
index.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
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
145
146
147
148
149
150
const express = require("express");
const fs = require("fs");
const fetch = require("isomorphic-unfetch");
require("dotenv").config();
const PORT = Number(process.env.PORT) || 8080;
const IPDATA_KEY = String(process.env.IPDATA_KEY);
const LINKS_CONF = process.env.LINKS_CONF || "Missing LINKS_CONF";
const BASE_URL = process.env.BASE_URL || "https://cool.link";
const TITLE = process.env.TITLE || "My Awesome Links";
const DESCRIPTION =
process.env.DESCRIPTION || "A collection of links that I like";
const GITHUB_URL =
process.env.GITHUB_URL || "https://github.com/statico/statico-link-list";
const CUSTOM_LINKS = {
ip: "Your public IPv4 address",
geoip: "Information about your IP using ipdata.co",
useragent: "Your current user agent",
};
const app = express();
app.set("json spaces", 2);
const ip = (req) => req.headers["x-forwarded-for"] ?? req.socket.remoteAddress;
app.get("/ip", (req, res) => {
res.set("Content-Type", "text/plain");
res.send(ip(req) + "\n");
});
app.get("/geoip", async (req, res) => {
if (IPDATA_KEY) {
const data = await fetch(
`https://api.ipdata.co/${ip(req)}?api-key=${IPDATA_KEY}`
);
const obj = await data.json();
res.json(obj);
} else {
res.send("IPDATA_KEY not configured");
}
});
app.get("/useragent", (req, res) => {
res.set("Content-Type", "text/plain");
res.send(req.headers["user-agent"] + "\n");
});
app.get("/:key", (req, res) => {
const conf = fs.readFileSync(LINKS_CONF, "utf8");
const lines = conf.split(/\n+/g).filter((x) => x);
const key = req.params.key.replace(/\W/g, "").toLowerCase();
for (const line of lines) {
const [path, url] = line.split(/\s+/, 2);
if (path.substr(1) === key) {
res.set("Referrer-Policy", "unsafe-url");
res.set("X-Robots-Tag", "noindex, nofollow");
res.redirect(302, url);
return;
}
}
res.status(404).send("Not found");
});
app.get("/", (req, res) => {
let out = `<!doctype html>
<html>
<head>
<title>${TITLE}</title>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<div class="my-4">
<h1 class="display-4">${TITLE}</h1>
<h2 class="lead">
${DESCRIPTION}
<a href="${GITHUB_URL}" class="text-muted text-decoration-none">(Source)</a>
</h2>
</div>
<div class="table-responsive">
<table class="table table-bordered">
`;
for (const key in CUSTOM_LINKS) {
const short = `${BASE_URL}/${key}`;
out += `
<tr>
<td class="text-nowrap">
<a href="/${key}" rel="noreferrer,noopener" target="_blank">${short}</a>
</td>
<td class="text-truncate">
${CUSTOM_LINKS[key]}
</td>
</tr>
`;
}
const conf = fs.readFileSync(LINKS_CONF, "utf8");
const lines = conf.split(/\n+/g).filter((x) => x);
for (const line of lines) {
const [path, url, ...more] = line.split(/\s+/);
const short = `${BASE_URL}${path}`;
if (/priv/.test(more.join(" "))) continue;
if (CUSTOM_LINKS[path.substr(1)]) continue;
out += `
<tr>
<td class="text-nowrap">
<a
href="${path}"
rel="noreferrer,noopener"
target="_blank"
>${short}</a>
</td>
<td>
<a
href="${url}"
rel="noreferrer,noopener"
target="_blank"
class="link-preview"
>${url}</a>
</td>
</tr>
`;
}
out += `
</table>
</div>
</div>
${process.env.FOOTER_HTML || ""}
</body>
</html>
`;
res.send(out);
});
app.listen(PORT, () => {
console.info(`Listening on http://127.0.0.1:${PORT}`);
});