-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathrun.js
More file actions
111 lines (96 loc) · 3.02 KB
/
Copy pathrun.js
File metadata and controls
111 lines (96 loc) · 3.02 KB
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
/* Copyright (c) 2019-2020 Digital Dream Labs. See LICENSE file for details. */
"use strict";
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const fs = require("fs");
const cors = require("cors");
const https = require('https');
const fsPromise = require("../utils/fsPromise");
const { getNetworkIp } = require("../utils/ip");
const { filePath } = require("./common.js");
const app = express();
let port = undefined;
let networkIp = getNetworkIp();
let serverIp = undefined;
app.set("view engine", "ejs");
app.use("/static", express.static(path.join(__dirname, "../site")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.get("/", (req, res) => {
res.render(path.join(__dirname, "../templates/main.ejs"), {
networkIp: networkIp,
serverIp: serverIp,
port: port,
});
});
app.post("/firmware", async (req, res) => {
try {
const env = req.body.env;
const loc = filePath.FIRMWARE_FOLDER + "/" + env;
if (!(await fsPromise.exists(loc))) {
return res.json({ message: "Store doesn't exists" });
} else {
const result = await fsPromise.readdir(loc);
return res.json({ message: result });
}
} catch (err) {
console.log(err);
}
});
const key = fs.readFileSync('key.pem');
const cert = fs.readFileSync('cert.pem');
const server = https.createServer({key: key, cert: cert }, app);
const startServer = (portReq, ipReq) => {
port = portReq === undefined ? 8000 : portReq;
serverIp = ipReq === undefined ? "0.0.0.0" : ipReq;
app
server.listen(port, serverIp, () => {
console.log(`Server running on ip ${serverIp} and port ${port}`);
let ipToShow = serverIp;
if ("0.0.0.0" == serverIp || serverIp.startsWith("127")) {
ipToShow = "localhost";
} else {
console.log(
`WARN: Server is running on an ip that might not be accessible to bot.`
);
}
console.log(`Server running. Go to http://${ipToShow}:${port} to use it`);
})
.on("error", (err) => {
switch (err.code) {
case "EADDRNOTAVAIL":
console.log(`Unable to bind to IP ${serverIp} on this device`);
return;
case "EACCES":
console.log(
`Permission denied to bind to IP ${serverIp} on PORT ${port} on this device.`
);
return;
case "EADDRINUSE":
console.log(
`Permission denied to bind to IP ${serverIp} on PORT ${port} on this device. The address is already in use.`
);
return;
default:
console.log(err);
return;
}
});
};
module.exports = (portReq, ipReq) => {
try {
if (
fs.existsSync(filePath.SETTINGS_FILE) &&
fs.existsSync(filePath.INVENTORY_FILE)
) {
startServer(portReq, ipReq);
} else {
console.log("Seems like you have missed this step 'configure'!");
console.log("E.g. 'vector-web-setup configure'");
}
} catch (err) {
console.log(err);
}
};