-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (103 loc) · 2.64 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
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
var _ = require('underscore')
var express = require('express')
var morgan = require('morgan')
var hsts = require('hsts')
var fs = require('fs')
var http = require('http')
var spdy = require('spdy')
var tls = require('tls')
var hostname = require('os').hostname()
var config = require('./config')
var vhost = {}
_.each(config.vhost, host => {
var path = __dirname + '/../' + host
var static
console.log('http://' + host)
try {
fs.accessSync(path + '/index.js')
}
catch (e) {
static = true
}
if (static)
vhost[host] = express.static(path, {maxAge: '1 day'})
else {
try {
vhost[host] = require(path)
}
catch (e) {
console.log(e)
}
}
try {
_.each(require(path + '/alias'), alias => {
console.log('http://' + alias)
vhost[alias] = vhost[host]
})
}
catch (e) {}
})
var letsencrypt = '/etc/letsencrypt/live'
var secureContext = {}
_.each(_.keys(vhost), domain => {
var key
var cert
var ca
try {
key = fs.readFileSync(letsencrypt + '/' + domain + '/privkey.pem')
cert = fs.readFileSync(letsencrypt + '/' + domain + '/cert.pem')
ca = [fs.readFileSync(letsencrypt + '/' + domain + '/chain.pem')]
}
catch (e) {
return
}
console.log('https://' + domain)
secureContext[domain] = {
key: key,
cert: cert,
ca: ca
}
/*
secureContext[domain].ciphers = [
'TLS_AES_256_GCM_SHA384',
'TLS_CHACHA20_POLY1305_SHA256',
'TLS_AES_128_GCM_SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256'
].join(':')
secureContext[domain].honorCipherOrder = true
*/
})
var app = express().
use(morgan(':date[iso] :req[x-forwarded-for] :method :url :status :response-time')).
use(hsts({
maxAge: 31536000,
includeSubDomains: true,
preload: true
})).
use((req, res, next) => {
if (vhost[req.hostname])
return vhost[req.hostname](req, res, next)
next()
})
//exit once a day because of the SSL cert
setTimeout(() => process.exit(), 86400000)
http.createServer(app).listen(80)
if (secureContext[hostname]) {
secureContext[hostname].SNICallback = (domain, done) => {
domain = domain.split('.').slice(-2).join('.') //second level domain w/o subdomains
if (!secureContext[domain])
return done(true)
done(null, tls.createSecureContext(secureContext[domain]))
}
spdy.createServer(secureContext[hostname], app).listen(443)
}