-
Notifications
You must be signed in to change notification settings - Fork 77
/
http-server.js
179 lines (157 loc) · 4.82 KB
/
http-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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var async = require('async-collection')
var mkdirp = require('mkdirp')
var http = require('http')
var path = require('path')
var pump = require('pump')
var net = require('net')
var fs = require('fs')
var os = require('os')
var selfsigned = require('selfsigned')
var getPort = require('get-port')
var CONFIG_DIR = path.join(os.homedir(), '.config/bankai')
var CERT_NAME = 'cert.pem'
var KEY_NAME = 'key.pem'
var CERT_LOCATION = path.join(CONFIG_DIR, CERT_NAME)
var KEY_LOCATION = path.join(CONFIG_DIR, KEY_NAME)
exports.createServer = createDevServer
function createDevServer (connectionHandler) {
var httpPort, httpsPort
var createSecureServer
try {
createSecureServer = require('http2').createSecureServer
} catch (e) {
createSecureServer = require('https').createServer
}
return {
listen: listen
}
function listen (port, onlisten) {
net.createServer(tcpConnection).listen(port, onNetListen)
function onNetListen () {
getPort({ port: 8080 }).then(function (port) {
httpPort = port
var httpServer = http.createServer(httpConnection)
.listen(port, onHttpListen)
httpServer.keepAliveTimeout = 0
httpServer.timeout = 0
})
.catch(function (err) {
throw err
})
}
function onHttpListen () {
createKeys(function (err, keys) {
if (err) throw err
var cert = keys.cert
var key = keys.key
getPort({ port: 4443 }).then(function (port) {
httpsPort = port
var serverOpts = { cert, key, allowHTTP1: true }
var http2Server = createSecureServer(serverOpts, connectionHandler)
http2Server.keepAliveTimeout = 0
http2Server.timeout = 0
http2Server.listen(httpsPort, function () {
if (onlisten) onlisten()
})
})
.catch(function (err) {
throw err
})
})
}
}
function tcpConnection (conn) {
conn.once('data', function (buf) {
// A TLS handshake record starts with byte 22.
var address = buf[0] === 22 ? httpsPort : httpPort
var proxy = net.createConnection(address, function () {
proxy.write(buf)
pump(conn, proxy, conn, function (err) {
if (err) return false // TODO: log error to the logger part
})
})
})
}
function httpConnection (req, res) {
var host = req.headers['host']
var location = 'https://' + host + req.url
var agent = req.headers['user-agent']
// We don't want to force an HTTPS connection if we are already
// encrypted or we are being forwarded through a proxy that may be
// taking care of it.
var encrypted = req.headers['x-forwarded-proto'] || req.connection.encrypted
// We want to force HTTPS connections, but using curl(1) or wget(1) from
// the command line can be convenient to quickly check output.
if (/^(curl|wget)/i.test(agent) || encrypted) {
return connectionHandler(req, res)
} else {
res.writeHead(301, { location: location })
res.end(`Redirecting to ${location}`)
}
}
}
// Read keys from ~/.config/bankai, or create new ones if they don't exist.
function createKeys (cb) {
mkdirp(CONFIG_DIR, function (err) {
if (err) return cb(err)
fs.readdir(CONFIG_DIR, function (err, files) {
if (err) return cb(err)
var keys = {}
// check if both files exist
if (files.indexOf(KEY_NAME) !== -1 && files.indexOf(CERT_NAME) !== -1) {
return async.parallel([
function (done) {
fs.readFile(CERT_LOCATION, function (err, buf) {
if (err) return done(err)
keys.cert = buf
done()
})
},
function (done) {
fs.readFile(KEY_LOCATION, function (err, buf) {
if (err) return done(err)
keys.key = buf
done()
})
}
], function (err) {
if (err) return cb(err)
cb(null, keys)
})
}
var opts = {
days: 2048,
algorithm: 'sha256',
extensions: [
{
name: 'subjectAltName',
altNames: [
{
type: 2, // DNSName
value: 'localhost'
}
]
}
]
}
selfsigned.generate([{ name: 'commonName', value: 'localhost' }], opts, function (err, keys) {
if (err) return cb(err)
keys = {
key: keys.private,
cert: keys.cert
}
async.parallel([
function (done) {
fs.writeFile(KEY_LOCATION, keys.key, done)
},
function (done) {
fs.writeFile(CERT_LOCATION, keys.cert, done)
}
], function (err) {
if (err) return cb(err)
cb(null, keys)
})
})
})
})
}