UNPKG

4.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.serverSecure = exports.serverSecureHTTPHeader = void 0;
4const crypto = require("crypto");
5const debug_ = require("debug");
6const debug = debug_("r2:streamer#http/server-secure");
7const debugHttps = debug_("r2:https");
8const IS_DEV = (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "dev");
9function serverSecureHTTPHeader(server, url) {
10 const info = server.serverInfo();
11 if (server.isSecured() &&
12 info && info.trustKey && info.trustCheck && info.trustCheckIV) {
13 let t1;
14 if (IS_DEV) {
15 t1 = process.hrtime();
16 }
17 const encrypteds = [];
18 const encryptStream = crypto.createCipheriv("aes-256-cbc", info.trustKey, info.trustCheckIV);
19 encryptStream.setAutoPadding(true);
20 const now = Date.now();
21 const jsonStr = `{"url":"${url}","time":${now}}`;
22 const buff1 = encryptStream.update(jsonStr, "utf8");
23 if (buff1) {
24 encrypteds.push(buff1);
25 }
26 const buff2 = encryptStream.final();
27 if (buff2) {
28 encrypteds.push(buff2);
29 }
30 const encrypted = Buffer.concat(encrypteds);
31 const base64 = Buffer.from(encrypted).toString("base64");
32 if (IS_DEV) {
33 const t2 = process.hrtime(t1);
34 const seconds = t2[0];
35 const nanoseconds = t2[1];
36 const milliseconds = nanoseconds / 1e6;
37 debugHttps(`< A > ${seconds}s ${milliseconds}ms [ ${url} ]`);
38 }
39 return { name: "X-" + info.trustCheck, value: base64 };
40 }
41 return undefined;
42}
43exports.serverSecureHTTPHeader = serverSecureHTTPHeader;
44function serverSecure(server, topRouter) {
45 topRouter.use((req, res, next) => {
46 if (!server.isSecured()) {
47 next();
48 return;
49 }
50 if (req.method.toLowerCase() === "options") {
51 next();
52 return;
53 }
54 let doFail = true;
55 const serverData = server.serverInfo();
56 if (serverData && serverData.trustKey &&
57 serverData.trustCheck && serverData.trustCheckIV) {
58 let t1;
59 if (IS_DEV) {
60 t1 = process.hrtime();
61 }
62 let delta = 0;
63 const urlCheck = server.serverUrl() + req.url;
64 const base64Val = req.get("X-" + serverData.trustCheck);
65 if (base64Val) {
66 const decodedVal = Buffer.from(base64Val, "base64");
67 const encrypted = decodedVal;
68 const decrypteds = [];
69 const decryptStream = crypto.createDecipheriv("aes-256-cbc", serverData.trustKey, serverData.trustCheckIV);
70 decryptStream.setAutoPadding(false);
71 const buff1 = decryptStream.update(encrypted);
72 if (buff1) {
73 decrypteds.push(buff1);
74 }
75 const buff2 = decryptStream.final();
76 if (buff2) {
77 decrypteds.push(buff2);
78 }
79 const decrypted = Buffer.concat(decrypteds);
80 const nPaddingBytes = decrypted[decrypted.length - 1];
81 const size = encrypted.length - nPaddingBytes;
82 const decryptedStr = decrypted.slice(0, size).toString("utf8");
83 try {
84 const decryptedJson = JSON.parse(decryptedStr);
85 let url = decryptedJson.url;
86 const time = decryptedJson.time;
87 const now = Date.now();
88 delta = now - time;
89 if (delta <= 3000) {
90 const i = url.lastIndexOf("#");
91 if (i > 0) {
92 url = url.substr(0, i);
93 }
94 if (url === urlCheck) {
95 doFail = false;
96 }
97 }
98 }
99 catch (err) {
100 debug(err);
101 debug(decryptedStr);
102 }
103 }
104 if (IS_DEV) {
105 const t2 = process.hrtime(t1);
106 const seconds = t2[0];
107 const nanoseconds = t2[1];
108 const milliseconds = nanoseconds / 1e6;
109 debugHttps(`< B > (${delta}ms) ${seconds}s ${milliseconds}ms [ ${urlCheck} ]`);
110 }
111 }
112 if (doFail) {
113 debug("############## X-Debug- FAIL ========================== ");
114 debug(req.url);
115 res.status(200);
116 res.end();
117 return;
118 }
119 next();
120 });
121}
122exports.serverSecure = serverSecure;
123//# sourceMappingURL=server-secure.js.map
\No newline at end of file