-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (120 loc) · 3.92 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
require("dotenv").config();
const fs = require("fs");
const crypto = require("crypto");
const http = require("http");
const https = require("https");
const shell = require("shelljs");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
function toBool(v) {
if (!v) return null;
let vLower = v.toLowerCase();
if (vLower === "true") return true;
if (vLower === "false") return false;
return null;
}
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
let wol_command = process.env.WOL_COMMAND || "wol";
let password_hashed = toBool(process.env.PASSWORD_HASHED_SHA265);
let password;
if (process.env.PASSWORD) {
password = process.env.PASSWORD;
} else {
try {
password = fs.readFileSync("password_sha256.txt").toString().trim();
} catch (e) {
if (e.code == "ENOENT") {
console.error("File password_sha256.txt missing");
console.error(
"Run the following command to create a hashed password file"
);
console.error("\nnode scripts/create_hash.js");
process.exit(3);
}
console.error(e);
process.exit(1);
}
}
let use_https = toBool(process.env.USE_HTTPS);
let options = {};
if (use_https) {
try {
options = {
key: fs.readFileSync("ssl/localhost.key").toString(),
cert: fs.readFileSync("ssl/localhost.crt").toString(),
ciphers:
"ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384",
honorCipherOrder: true,
secureProtocol: "TLSv1_2_method",
};
} catch (e) {
if (e.code == "ENOENT") {
console.error("SSL certificates not found");
console.error("https won't be used");
console.error(
"Run the following command to create the certificates"
);
console.error("\nsh scripts/keygen.sh");
use_https = false;
} else {
console.error(e);
process.exit(1);
}
}
}
app.post("/wake", (request, response) => {
let req_password = request.body.password;
if (password_hashed) {
const sha256sum = crypto.createHash("sha256");
sha256sum.update(req_password);
req_password = sha256sum.digest("hex");
}
if (req_password === password) {
let ret = shell.exec(wol_command + " " + process.env.MAC_ADDR);
response.send({ success: ret.code });
} else {
response.send({ success: -1 });
}
});
app.get("/check", (request, response) => {
let ret = shell.exec("ping -c 1 -W 1 -q " + process.env.IP_CHECK);
response.send({ success: ret.code });
});
app.get("/", (request, response) => {
response.sendFile("/public/index.html", { root: __dirname });
});
app.get("/robots.txt", (request, response) => {
response.sendFile("/public/robots.txt", { root: __dirname });
});
app.get("/handler.js", (request, response) => {
response.sendFile("/public/handler.js", { root: __dirname });
});
function init_port(port, err) {
if (err) return console.error(err);
if (!shell.which(wol_command)) {
shell.echo(
"Sorry, this script requires the Wake On Lan command: " +
wol_command
);
shell.exit(1);
}
console.log("Server is listening on", port);
}
for (let portStr of process.env.HTTP_PORTS.split(",")) {
let port = parseInt(portStr);
const httpServer = http.createServer(app);
httpServer.listen(port, (err) => {
init_port(port, err);
});
}
if (use_https) {
for (let portStr of process.env.HTTP_PORTS.split(",")) {
let port = parseInt(portStr);
const httpServer = http.createServer(app);
httpServer.listen(port, (err) => {
init_port(port, err);
});
}
}