-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
123 lines (101 loc) · 2.78 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
const http = require("http");
const url = require("url");
const QRCode = require("qrcode");
require("dotenv").config();
const BASE_QR_CODE_URL = "/generate-qr";
const QR_CODE_SERVER_USER = process.env.QR_CODE_SERVER_USER ?? "admin";
const QR_CODE_SERVER_PASSWORD = process.env.QR_CODE_SERVER_PASSWORD ?? "admin";
const qrCodeRoutes = {
[BASE_QR_CODE_URL]: "",
};
function checkCredentials(req) {
const auth = req.headers["authorization"];
if (!auth) {
return false;
}
const [username, password] = Buffer.from(auth.split(" ")[1], "base64")
.toString()
.split(":");
return (
username === QR_CODE_SERVER_USER && password === QR_CODE_SERVER_PASSWORD
);
}
function generateQRCodeOptionsRoute(res) {
res.writeHead(200, { "Content-Type": "text/html" });
const html = `
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Route options</title>
</head>
<body>
<ul>
${Object.keys(qrCodeRoutes)
.map(function (route) {
return `<li><a href="${route}">${route}</a></li>`;
})
.join("")}
</ul>
</body>
</html>
`;
res.end(html);
}
function registerQRCodeRoute(client) {
client.on("qr", (qr) => {
let route = BASE_QR_CODE_URL;
if (!!client?.authStrategy?.clientId) {
route = `${route}/${new String(client.authStrategy.clientId).match(/\d+/g)}`;
}
qrCodeRoutes[route] = qr;
console.log("REGISTERED ROUTE || ", route);
});
}
function raise404(res) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
}
function qrCodeRoute(route, res) {
const qrCode = qrCodeRoutes[route];
QRCode.toDataURL(qrCode, { errorCorrectionLevel: "H" }, function (err, url) {
if (err) {
console.error(err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Error generating QR code");
} else {
res.writeHead(200, { "Content-Type": "image/png" });
res.end(Buffer.from(url.split(",")[1], "base64"));
}
});
}
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url, true);
if (!checkCredentials(req)) {
res.writeHead(401, { "WWW-Authenticate": 'Basic realm="MyServer"' });
res.end("Access Denied");
return;
}
try {
if (reqUrl.path.includes(BASE_QR_CODE_URL)) {
qrCodeRoute(reqUrl.path, res);
} else if (reqUrl.path === "/") {
generateQRCodeOptionsRoute(res);
} else {
raise404(res);
}
} catch (e) {
raise404(res);
}
});
function runQRCodeServer() {
const port = process.env?.QR_CODE_SERVER_PORT ?? 3000;
server.listen(port, () => {
console.log(`QR code server running`);
});
}
module.exports = {
runQRCodeServer,
registerQRCodeRoute,
};