-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·44 lines (40 loc) · 1.52 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
const redbird = require('redbird')
const { mappings, cfAccessDomain } = require('./config')
const cookie = require('cookie')
const asyncVerify = require('./lib/asyncVerify')
const getCerts = require('./lib/getCerts')
const getIps = require('./lib/getCfIps')
const ipRangeCheck = require('ip-range-check')
async function run () {
let accessCerts = await getCerts()
let ips = await getIps()
var authResolver = async function (host, url, req) {
const srvip = req.connection.remoteAddress
if (!ipRangeCheck(srvip, ips)) {
console.warn('Rejected IP tried to access origin: ' + srvip)
return
}
const cookieHeader = req.headers.cookie
const cookieToken = cookieHeader ? cookie.parse(cookieHeader).CF_Authorization : null
const headerToken = req.headers['cf-access-jwt-assertion']
const token = headerToken || cookieToken
if (!token) {
return
}
const jwtChecks = await Promise.all(accessCerts.public_certs.map(publicCert => asyncVerify(token, publicCert.cert)))
const passedCheck = jwtChecks.find(jwtPayload => jwtPayload && jwtPayload.iss === `https://${cfAccessDomain}`)
if (passedCheck) {
return mappings[host]
}
}
authResolver.priority = 200
redbird({ port: process.env.LISTEN_PORT, xfwd: true, resolvers: [authResolver] })
console.log(`Web server listening on port ${process.env.LISTEN_PORT}`)
setInterval(async () => {
const newCerts = await getCerts()
accessCerts = newCerts
const newIps = await getIps()
ips = newIps
}, 1000 * 60 * 60)
}
run()