-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (45 loc) · 1.24 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
const proxy = require('http-proxy')
const fs = require('fs')
const url = require('url')
const path = require('path')
function getRemoteTarget(bind) {
if (bind.port) {
return `http://localhost:${bind.port}`
}
const {
port
} = url.parse(bind)
return `http://localhost:${port}`
}
exports.setup = function (mage) {
mage.on('readyToStart', () => {
const logger = mage.logger.context('https')
const port = 8443
let ssl
const {
httpsProxy: isHttpsProxyEnabled,
bind
} = mage.core.config.get('server.clientHost')
if (!isHttpsProxyEnabled) {
return
}
const target = getRemoteTarget(bind)
try {
ssl = {
key: fs.readFileSync(path.join(__dirname, './certs/localhost.key')),
cert: fs.readFileSync(path.join(__dirname, './certs/localhost.cer'))
}
} catch (error) {
logger.emergency('Failed to load certificate files', error)
mage.quit(-1)
}
logger.debug('Starting HTTPS proxy')
proxy
.createServer({ target, ssl, ws: true, proxyTimeout: 60 * 1000 })
.on('error', (error) => logger.error('Proxy error', error))
.listen(port, () => logger.notice('HTTPS proxy server started', {
port,
target
}))
})
}