-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
68 lines (61 loc) · 1.97 KB
/
main.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
/* Required Libraries */
const express = require('express');
const path = require('path');
const cors = require('cors');
const fs = require('fs');
const compression = require('compression');
// Load config file
const CONFIG = JSON.parse(fs.readFileSync('config.json', 'utf8'));
const app = express();
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({extended: false}));
// Frontend static files
app.use(express.static('public'));
app.use(cors());
const router = require('./routes');
try {
// Proprietary SC module support
const scRouter = require('./sc/sc-routes');
app.use(scRouter);
console.log('SC Module: Proprietary module loaded, welcome JS! (or Oleg!)');
} catch (e) {
console.warn('SC Module: Failed to load proprietary SC module!\nThis is fine if you\'re running an open-source explorer.');
console.warn(e);
}
app.use(router);
// Handle 404
app.use(function(req, res) {
res.status(404).send('404: Page not Found');
console.error("- IP: " + req.ip);
});
// Handle 500
app.use(function(error, req, res) {
res.status(500).send('500: Internal Server Error');
console.error("- IP: " + req.ip + "\nERROR: " + error);
});
let environment;
// Certificate
let credentials;
try {
credentials = {
key: fs.readFileSync(CONFIG.certificates + 'privkey.pem', 'utf8'),
cert: fs.readFileSync(CONFIG.certificates + 'cert.pem', 'utf8'),
ca: fs.readFileSync(CONFIG.certificates + 'chain.pem', 'utf8')
};
environment = 'public';
} catch (e) {
console.error("ERROR: Certificate could not be loaded, running as 'local' mode...");
environment = "local";
}
if (environment === 'local') {
// HTTP
app.listen(CONFIG.devport, () => {
console.log(`SCPscan running in local (dev) environment on port ` + CONFIG.devport);
});
} else {
// HTTPS
require('https').createServer(credentials, app).listen(443, '0.0.0.0', () => {
console.log("Serving SCPscan with SSL!");
});
}