-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (96 loc) · 3.62 KB
/
app.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
const express = require('express');
const WebSocket = require('ws');
const useragent = require('express-useragent');
const https = require('https');
const http = require('http');
const cookieSession = require('cookie-session');
const proxy = require('express-http-proxy');
const clientHandler = require('./project_modules/client_handler.js')
const sessionHandler = require('./project_modules/session_handler.js')
const linkerInit = require('./project_modules/linker.js')
const fileSaver = require('./project_modules/file_saver.js')
const configLoader = require('./config-loader.js')
const config = configLoader({ home_dir: __dirname })
const smoke_tests = [];
function tearDown() {
//TODO send disconnect messages to all connected clients
process.exit(0)
}
process.on('SIGTERM', tearDown)
process.on('SIGINT', tearDown)
async function checkAndStartServer(port) {
let wss, clientPlugins = [];
app = express();
const file_saver = fileSaver();
app.use(file_saver.router);
clientPlugins.push(file_saver.plugin);
app.use(cookieSession({
secret: config.get('cookie_secret'),
signed: true
}))
app.use(useragent.express());
app.get('/health', function(req, res) {
res.end("healthy. version: 0.1")
})
if(config.get('https_redirect_target')) {
app.use('/', (req, res, next) => {
let fullHost = req.protocol + '://' + req.get('host')
if(fullHost !== config.get('https_redirect_target')) {
console.log(`fullHost (${fullHost}) is not the target host (${config.get('https_redirect_target')}). redirecting`)
res.redirect(301, config.get('https_redirect_target'))
}
next();
})
}
const clients = clientHandler({
plugins: clientPlugins
});
const sessions = sessionHandler();
const linker = linkerInit({ sessions, clients })
app.use(linker)
const proxy_to_frontend = proxy(config.get('frontend_server_address'))
app.get(['/connector/*', '/welcome'], (req, res) => {
http.get(`http://${config.get('frontend_server_address')}/`, (proxy_res) => {
proxy_res.pipe(res)
})
})
app.use('/', proxy_to_frontend)
try {
await Promise.all(smoke_tests.map(async (test) => { return await test() }))
} catch(e) {
console.error(`smoke tests did not pass: ${e.code}. terminating.`)
process.exit(1);
}
if(config.get('tls')) {
console.log('using tls')
const fs = require('fs');
let privateKey = fs.readFileSync(config.get("private_key_file"), 'utf8');
let certificate = fs.readFileSync(config.get("cert_file"), 'utf8');
let credentials = { key: privateKey, cert: certificate };
https.createServer(credentials, app).listen(port, () => {
console.log(`server started, listening on port ${port}`)
console.log(`websocket server on port ${config.get('ws_port')}`)
});
wssTlsServer = https.createServer(credentials)
wss = new WebSocket.Server({ server: wssTlsServer });
wssTlsServer.listen(config.get('ws_port'))
redirectApp = express()
redirectApp.get('*', (req, res) => {
res.redirect(301, config.get('https_redirect_target'))
})
redirectApp.listen(config.get('non_tls_port'), () => {
console.log("redirect app listening on " + config.get('non_tls_port'))
})
} else {
app.listen(port, () => {
console.log(`server started, listening on port ${port}`)
console.log(`websocket server on port ${config.get('ws_port')}`)
})
wss = new WebSocket.Server({ port: config.get('ws_port') });
}
wss.on('connection', async function connection(ws) {
await clients.createNewClient({ ws })
console.log('new websocket connection')
});
}
checkAndStartServer(config.get('listen_port'))