-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (48 loc) · 1.64 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
const cors = require('cors')
const proxy = require('express-http-proxy')
const app = require('express')()
const crypto = require('crypto')
const port = process.env.PORT || 3334
console.log(`To make http requests behind CORS, send the request to:
http://localhost:${port}/proxy
Put the server URL (e. g. \`http://example.com/\`) in the X-Client-Gateway-Server header\n`)
let clientGatewayToken = process.env.CLIENT_GATEWAY_TOKEN
if (!clientGatewayToken) {
crypto.randomBytes(48, (err, buffer) => {
clientGatewayToken = buffer.toString('hex')
console.log('Use the header X-Client-Gateway-Token with the following value:')
console.log(` ${clientGatewayToken}\n`)
})
} else {
console.log('Use the header X-Client-Gateway-Token with the contents of this environment variable:')
console.log(' CLIENT_GATEWAY_TOKEN\n')
}
app.use(cors())
app.use('/proxy', proxy(req => {
const server = req.headers['x-client-gateway-server']
if (!server) {
throw new Error('Missing header: X-Client-Gateway-Server')
}
return server
}, {
filter(req, res) {
const token = req.headers['x-client-gateway-token']
if (!token) {
console.log('Missing header: X-Client-Gateway-Token')
}
if (token !== clientGatewayToken) {
console.log('Invalid token in header: X-Client-Gateway-Token')
}
return token === clientGatewayToken
},
proxyReqOptDecorator(proxyReqOpts, srcReq) {
delete proxyReqOpts.headers['x-client-gateway-token']
delete proxyReqOpts.headers['x-client-gateway-server']
return proxyReqOpts
}
}))
app.listen(port, () => {
setTimeout(() => {
console.log(`Listening on localhost:${port}`)
}, 200)
})