forked from PrismarineJS/prismarine-web-client
-
Notifications
You must be signed in to change notification settings - Fork 58
/
server.js
73 lines (67 loc) · 2.11 KB
/
server.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
#!/usr/bin/env node
const express = require('express')
const netApi = require('net-browserify')
const compression = require('compression')
const path = require('path')
const cors = require('cors')
const https = require('https')
const fs = require('fs')
let siModule
try {
siModule = require('systeminformation')
} catch (err) { }
// Create our app
const app = express()
const isProd = process.argv.includes('--prod')
app.use(compression())
app.use(cors())
app.use(netApi({ allowOrigin: '*' }))
if (!isProd) {
app.use('/sounds', express.static(path.join(__dirname, './generated/sounds/')))
}
// patch config
app.get('/config.json', (req, res, next) => {
// read original file config
let config = {}
try {
config = require('./config.json')
} catch {
try {
config = require('./dist/config.json')
} catch { }
}
res.json({
...config,
'defaultProxy': '', // use current url (this server)
})
})
if (isProd) {
// add headers to enable shared array buffer
app.use((req, res, next) => {
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
next()
})
app.use(express.static(path.join(__dirname, './dist')))
}
const numArg = process.argv.find(x => x.match(/^\d+$/))
const port = (require.main === module ? numArg : undefined) || 8080
// Start the server
const server =
app.listen(port, async function () {
console.log('Proxy server listening on port ' + server.address().port)
if (siModule && isProd) {
const _interfaces = await siModule.networkInterfaces()
const interfaces = Array.isArray(_interfaces) ? _interfaces : [_interfaces]
let netInterface = interfaces.find(int => int.default)
if (!netInterface) {
netInterface = interfaces.find(int => !int.virtual) ?? interfaces[0]
console.warn('Failed to get the default network interface, searching for fallback')
}
if (netInterface) {
const address = netInterface.ip4
console.log(`You can access the server on http://localhost:${port} or http://${address}:${port}`)
}
}
})
module.exports = { app }