-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpanel.js
158 lines (137 loc) · 5.15 KB
/
panel.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
151
152
153
154
155
156
157
158
import { createServer } from "http";
import readConfig from "./util/read-config.js";
import exchangeCodeToToken from "./auth/exchange-code.js";
import logging from "./util/logging.js";
import validateAccessToken from "./auth/validate-token.js";
import serveStatic from "./static/serve-static.js";
import DB_METHODS from "./database/methods.js";
import readPayload from "./util/read-payload.js";
import { parseCookies, parsePath, parseQuery } from "./util/urls-and-cookies.js";
import fetch from "node-fetch";
import { equal } from "assert";
const PANEL_CONFIG = readConfig();
const IS_PANEL_ORIGIN_SECURE = new URL(PANEL_CONFIG.PANEL_ORIGIN).protocol === "https:";
createServer((req, res) => {
const method = req.method;
const requestedURL = req.url;
const path = parsePath(req.url);
const queries = parseQuery(req.url);
const cookies = parseCookies(req.headers);
const sendError = () => {
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("500 Internal Server Error");
};
const sendForbidden = () => {
res.statusCode = 403;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("403 Forbidden");
};
const redirectToLoginPage = () => {
const loginPage = `${PANEL_CONFIG.KEYCLOAK_ORIGIN}/realms/${PANEL_CONFIG.KEYCLOAK_REALM}/protocol/openid-connect/auth?client_id=${PANEL_CONFIG.KEYCLOAK_CLIENT_ID}&scope=openid%20profile&redirect_uri=${PANEL_CONFIG.PANEL_ORIGIN}&response_type=code`;
res.statusCode = 302;
res.setHeader("Location", loginPage);
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end(`Go to ${loginPage}`);
};
validateAccessToken(cookies.access_token)
.then(() => {
if (path[0] === "grafana") {
fetch(new URL(requestedURL, PANEL_CONFIG.GRAFANA_ORIGIN).href, {
method,
headers: JSON.parse(JSON.stringify(req.headers)),
body: method === "GET" ? undefined : req
})
.then((grafanaResponse) => {
res.statusCode = grafanaResponse.status;
res.statusMessage = grafanaResponse.statusText;
Array.from(grafanaResponse.headers.entries()).forEach(([name, value]) => res.setHeader(name, value));
grafanaResponse.body.pipe(res);
})
.catch(() => sendError());
return;
}
if (path[0] === "params-panel") {
if (path[1] === "api" && path[2] === "params" && path[3] === "list") {
if (method !== "GET") {
res.statusCode = 405;
res.end("405 Method Not Allowed");
return;
}
return DB_METHODS.listAllParams()
.then((params) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(JSON.stringify(params));
})
.catch((e) => {
logging(e);
sendError();
});
}
if (path[1] === "api" && path[2] === "params" && path[3] === "set") {
if (method !== "POST") {
res.statusCode = 405;
res.end("405 Method Not Allowed");
return;
}
return readPayload(req)
.then((readBuffer) => {
try {
return Promise.resolve(JSON.parse(readBuffer.toString()));
} catch (e) {
return Promise.reject(new Error("Malformed JSON"));
}
})
.then((payload) => DB_METHODS.setParam(payload))
.then((updatedNumber) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(JSON.stringify({ updatedNumber }));
})
.catch((e) => {
logging(e);
sendError();
});
}
if (path[1] === "api" && path[2] === "logs" && path[3] === "list") {
if (method !== "GET") {
res.statusCode = 405;
res.end("405 Method Not Allowed");
return;
}
return DB_METHODS.getLogs(parseInt(queries.skip), parseInt(queries.limit))
.then((logs) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(JSON.stringify(logs));
})
.catch((e) => {
logging(e);
sendError();
});
}
}
serveStatic(req, res);
})
.catch(() => {
if ("session_state" in queries && queries.code) {
exchangeCodeToToken(queries.code)
.then((token) => {
res.setHeader(
"Set-Cookie",
`access_token=${encodeURIComponent(token)}; Expires=${new Date(
Date.now() + PANEL_CONFIG.PANEL_COOKIE_TTL_SECONDS * 1000
).toUTCString()}; ${IS_PANEL_ORIGIN_SECURE ? "Secure; " : ""}HttpOnly; SameSite=Strict`
);
serveStatic(req, res);
})
.catch((e) => {
console.warn(e);
sendForbidden();
});
return;
}
redirectToLoginPage();
});
}).listen(80);