-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
167 lines (126 loc) · 5.06 KB
/
index.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import express, { Express, Request, Response } from "express";
import * as path from "path";
import "dotenv/config";
const bearer = "Bearer " + process.env.ADMIN_TOKEN;
async function fetchApi(path: string, method = "GET") {
const endpoint = "http://" + process.env.DOCKER_API_HOSTNAME + ":" + process.env.DOCKER_API_PORT + path;
const option = {
headers: {
"Authorization": bearer,
},
method: method
};
const response = await fetch(endpoint, option);
const responseJson = await response.json();
if (response.status != 200) {
const message = responseJson.message || "something is not good";
console.error(message);
}
return responseJson;
}
async function getUrlsUndeletedUnchecked() {
return await fetchApi("/api/urls/undeleted-unchecked");
}
async function getUrlsUndeletedChecked() {
return await fetchApi("/api/urls/undeleted-checked");
}
async function getUrlsDeletedChecked() {
return await fetchApi("/api/urls/deleted-checked");
}
async function getHostnamesBlacklisted() {
return await fetchApi("/api/hostnames/blacklisted");
}
async function getHostnamesWhitelisted() {
return await fetchApi("/api/hostnames/whitelisted");
}
async function setUrlCheckedAdminUndeletedById(id: number) {
return await fetchApi(`/api/url/check/${id}`);
}
async function setUrlCheckedAdminDeletedById(id: number) {
return await fetchApi(`/api/url/delete/${id}`, "DELETE");
}
async function setUrlWhitelist(id: number) {
return await fetchApi(`/api/url/whitelist/${id}`);
}
async function setUrlBlacklist(id: number) {
return await fetchApi(`/api/url/blacklist/${id}`);
}
const app: Express = express();
app.set("view engine", "pug");
const port = process.env.ADMIN_PORT;
app.get("/", async (req: Request, res: Response) => {
const urlsUndeletedUnchecked = await getUrlsUndeletedUnchecked();
const urlsUndeletedChecked = await getUrlsUndeletedChecked();
const urlsDeletedChecked = await getUrlsDeletedChecked();
const hostnamesBlacklisted = await getHostnamesBlacklisted();
const hostnamesWhitelisted = await getHostnamesWhitelisted();
res.render("home", {
title: "2a5 Admin",
urlsUndeletedUnchecked: urlsUndeletedUnchecked,
urlsUndeletedChecked: urlsUndeletedChecked,
urlsDeletedChecked: urlsDeletedChecked,
hostnamesBlacklisted: hostnamesBlacklisted,
hostnamesWhitelisted: hostnamesWhitelisted
});
});
app.get("/api/url/check/:id", async (req: Request, res: Response) => {
const responseJson = await setUrlCheckedAdminUndeletedById(parseInt(req.params.id));
if (responseJson.message == "success") {
res.redirect("/");
} else {
res.render("url-check-error", {
title: "2a5 Admin - Url Check Error",
hostname: responseJson.hostname
});
}
});
app.get("/api/url/delete/:id", async (req: Request, res: Response) => {
await setUrlCheckedAdminDeletedById(parseInt(req.params.id));
res.redirect("/");
});
app.get("/api/url/whitelist/:id", async (req: Request, res: Response) => {
const responseJson = await setUrlWhitelist(parseInt(req.params.id));
if (responseJson.message == "success") {
res.redirect("/");
} else if (responseJson.message == "error - whitelisted already") {
// this is the case, that the hostname is whitelisted already
res.render("whitelist-error-whitelisted-already", {
title: "2a5 Admin - Whitelist Error",
hostname: responseJson.hostname
});
} else if (responseJson.message == "error - blacklisted already") {
// this is the case, that the hostname is blacklisted already
res.render("blacklist-error-blacklisted-already", {
title: "2a5 Admin - blacklist Error",
hostname: responseJson.hostname
});
} else if (responseJson.message == "error - deleted URLs") {
res.render("whitelist-error-urls-deleted", {
title: "2a5 Admin - Whitelist Error",
urls: responseJson.urls,
hostname: responseJson.hostname
});
}
});
app.get("/api/url/blacklist/:id", async (req: Request, res: Response) => {
const responseJson = await setUrlBlacklist(parseInt(req.params.id));
if (responseJson.message === "success") {
res.redirect("/");
} else if (responseJson.message == "error - blacklisted already") {
// this is the case, that the hostname is blacklisted already
res.render("blacklist-error-blacklisted-already", {
title: "2a5 Admin - Blacklist Error",
hostname: responseJson.hostname
});
} else if (responseJson.message == "error - checked URLs") {
res.render("blacklist-error-urls-checked", {
title: "2a5 Admin - Blacklist Error",
urls: responseJson.urls,
hostname: responseJson.hostname
});
}
});
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});
app.use(express.static(path.join(__dirname, "public")));