-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (134 loc) · 5.24 KB
/
server.js
File metadata and controls
147 lines (134 loc) · 5.24 KB
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
// demos/https/server.js — minimal HTTPS server.
//
// Generates a self-signed cert at startup (RSA-2048, ~6 s on a G3),
// then listens for HTTPS requests. Three routes:
//
// GET / → small HTML status page with TLS + request info
// GET /info → JSON dump of request headers + TLS metadata
// GET /echo?msg= → echo back the query
//
// Usage:
// ./node demos/https/server.js [port] # port defaults to 8443
//
// Browser test: open https://<host>:8443/ — accept the self-signed cert
// warning. CLI test: see demos/https/client.js.
var https = require('https');
var tls = require('tls');
var url = require('url');
var port = parseInt(process.argv[2], 10) || 8443;
console.error('starting demos/https server on :' + port);
console.error('generating self-signed RSA-2048 cert (slow on G3 ~6 s)...');
var t0 = Date.now();
var pair = tls.generateSelfSigned('ionpower-https-demo', 30);
console.error('cert generated in ' + (Date.now() - t0) + ' ms');
var startedAt = new Date().toISOString();
function htmlPage(reqInfo, tlsInfo) {
return [
'<!doctype html>',
'<html><head>',
'<meta charset="utf-8">',
'<title>ionpower-node HTTPS demo</title>',
'<style>',
' body{font-family:system-ui,sans-serif;margin:2em;max-width:60em;background:#fafafa;color:#222}',
' h1{font-size:1.4em}',
' table{border-collapse:collapse;margin:1em 0}',
' td,th{padding:0.3em 0.6em;border:1px solid #ccc;text-align:left;vertical-align:top}',
' th{background:#eee}',
' code{background:#eee;padding:0 0.3em;border-radius:3px}',
' .ok{color:#080}',
'</style>',
'</head><body>',
'<h1><span class="ok">🔒</span> ionpower-node HTTPS demo</h1>',
'<p>Served from <code>demos/https/server.js</code>',
' on <code>' + process.version + '</code>',
' (OpenSSL ' + (process.versions.openssl || '?') + ').</p>',
'<h2>TLS connection</h2>',
'<table>',
' <tr><th>Protocol</th><td><code>' + tlsInfo.protocol + '</code></td></tr>',
' <tr><th>Cipher</th><td><code>' + (tlsInfo.cipher && tlsInfo.cipher.name) + '</code></td></tr>',
'</table>',
'<h2>Request</h2>',
'<table>',
' <tr><th>Method</th><td><code>' + reqInfo.method + '</code></td></tr>',
' <tr><th>URL</th><td><code>' + reqInfo.url + '</code></td></tr>',
' <tr><th>Remote</th><td><code>' + reqInfo.remote + '</code></td></tr>',
'</table>',
'<h2>Try</h2>',
'<ul>',
' <li><a href="/info">GET /info</a> — JSON dump of TLS + request</li>',
' <li><a href="/echo?msg=hello">GET /echo?msg=hello</a> — echo a query</li>',
'</ul>',
'<p><small>Self-signed cert; expect a browser warning. Started at ' + startedAt + '.</small></p>',
'</body></html>'
].join('\n');
}
var server = https.createServer({
cert: pair.certPem,
key: pair.keyPem
}, function (req, res) {
var u = url.parse(req.url, true);
var sock = req.socket;
var tlsInfo = {
protocol: sock.getProtocol ? sock.getProtocol() : null,
cipher: sock.getCipher ? sock.getCipher() : null
};
var reqInfo = {
method: req.method,
url: req.url,
path: u.pathname,
query: u.query,
headers: req.headers,
remote: (sock.address && sock.address().host) || ''
};
console.error('[' + new Date().toISOString() + '] ' +
req.method + ' ' + req.url + ' (' +
tlsInfo.protocol + ' ' +
((tlsInfo.cipher && tlsInfo.cipher.name) || '?') + ')');
if (u.pathname === '/' || u.pathname === '') {
var body = htmlPage(reqInfo, tlsInfo);
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': Buffer.byteLength(body)
});
res.end(body);
return;
}
if (u.pathname === '/info') {
var payload = JSON.stringify({
tls: tlsInfo,
request: reqInfo,
server: {
runtime: process.version,
openssl: process.versions.openssl,
started: startedAt
}
}, null, 2) + '\n';
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
});
res.end(payload);
return;
}
if (u.pathname === '/echo') {
var msg = (u.query && u.query.msg) || '';
var out = String(msg);
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Length': Buffer.byteLength(out)
});
res.end(out);
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('not found\n');
});
server.on('clientError', function (e, sock) {
console.error('clientError:', e && e.message ? e.message : e);
if (sock && sock.destroy) sock.destroy();
});
server.listen(port, '0.0.0.0', function () {
var addr = server.address();
console.error('listening on https://' + addr.host + ':' + addr.port + '/');
console.error('try: ./node demos/https/client.js https://127.0.0.1:' + addr.port + '/info');
});