2017-10-20 03:32:20 +02:00
|
|
|
// Flags: --expose-internals
|
|
|
|
|
2017-07-17 19:29:42 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
2017-07-31 20:53:15 +02:00
|
|
|
|
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
|
|
|
|
2017-07-17 19:29:42 +02:00
|
|
|
const assert = require('assert');
|
2017-10-06 19:00:37 +02:00
|
|
|
const fixtures = require('../common/fixtures');
|
2017-07-17 19:29:42 +02:00
|
|
|
const h2 = require('http2');
|
2017-10-20 03:32:20 +02:00
|
|
|
const { kSocket } = require('internal/http2/util');
|
2017-10-06 19:00:37 +02:00
|
|
|
const tls = require('tls');
|
2017-07-17 19:29:42 +02:00
|
|
|
|
|
|
|
function loadKey(keyname) {
|
2017-10-06 19:00:37 +02:00
|
|
|
return fixtures.readKey(keyname, 'binary');
|
2017-07-17 19:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onStream(stream, headers) {
|
2017-10-20 03:32:20 +02:00
|
|
|
const socket = stream.session[kSocket];
|
2018-01-01 20:13:29 +01:00
|
|
|
|
|
|
|
assert(stream.session.encrypted);
|
2018-04-21 05:21:44 +02:00
|
|
|
assert.strictEqual(stream.session.alpnProtocol, 'h2');
|
2018-01-01 20:13:29 +01:00
|
|
|
const originSet = stream.session.originSet;
|
|
|
|
assert(Array.isArray(originSet));
|
|
|
|
assert.strictEqual(originSet[0],
|
|
|
|
`https://${socket.servername}:${socket.remotePort}`);
|
|
|
|
|
2017-07-17 19:29:42 +02:00
|
|
|
assert(headers[':authority'].startsWith(socket.servername));
|
2017-11-26 20:40:50 +01:00
|
|
|
stream.respond({ 'content-type': 'application/json' });
|
2017-07-17 19:29:42 +02:00
|
|
|
stream.end(JSON.stringify({
|
|
|
|
servername: socket.servername,
|
|
|
|
alpnProtocol: socket.alpnProtocol
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifySecureSession(key, cert, ca, opts) {
|
2017-07-22 18:20:53 +02:00
|
|
|
const server = h2.createSecureServer({ cert, key });
|
2017-07-17 19:29:42 +02:00
|
|
|
server.on('stream', common.mustCall(onStream));
|
2018-01-16 18:34:20 +01:00
|
|
|
server.on('close', common.mustCall());
|
2017-11-26 20:40:50 +01:00
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
opts = opts || { };
|
2017-07-22 18:20:53 +02:00
|
|
|
opts.secureContext = tls.createSecureContext({ ca });
|
2017-11-26 20:40:50 +01:00
|
|
|
const client = h2.connect(`https://localhost:${server.address().port}`,
|
|
|
|
opts);
|
|
|
|
// Verify that a 'secureConnect' listener is attached
|
|
|
|
assert.strictEqual(client.socket.listenerCount('secureConnect'), 1);
|
|
|
|
const req = client.request();
|
2017-07-17 19:29:42 +02:00
|
|
|
|
2018-01-01 20:13:29 +01:00
|
|
|
client.on('connect', common.mustCall(() => {
|
|
|
|
assert(client.encrypted);
|
|
|
|
assert.strictEqual(client.alpnProtocol, 'h2');
|
|
|
|
const originSet = client.originSet;
|
|
|
|
assert(Array.isArray(originSet));
|
|
|
|
assert.strictEqual(originSet.length, 1);
|
|
|
|
assert.strictEqual(
|
|
|
|
originSet[0],
|
|
|
|
`https://${opts.servername || 'localhost'}:${server.address().port}`);
|
|
|
|
}));
|
|
|
|
|
2017-11-26 20:40:50 +01:00
|
|
|
req.on('response', common.mustCall((headers) => {
|
|
|
|
assert.strictEqual(headers[':status'], 200);
|
|
|
|
assert.strictEqual(headers['content-type'], 'application/json');
|
2018-02-13 06:09:31 +01:00
|
|
|
assert(headers.date);
|
2017-11-26 20:40:50 +01:00
|
|
|
}));
|
2017-07-17 19:29:42 +02:00
|
|
|
|
2017-11-26 20:40:50 +01:00
|
|
|
let data = '';
|
|
|
|
req.setEncoding('utf8');
|
|
|
|
req.on('data', (d) => data += d);
|
|
|
|
req.on('end', common.mustCall(() => {
|
|
|
|
const jsonData = JSON.parse(data);
|
|
|
|
assert.strictEqual(jsonData.servername,
|
|
|
|
opts.servername || 'localhost');
|
|
|
|
assert.strictEqual(jsonData.alpnProtocol, 'h2');
|
2018-01-16 18:34:20 +01:00
|
|
|
server.close(common.mustCall());
|
2017-11-26 20:40:50 +01:00
|
|
|
client[kSocket].destroy();
|
|
|
|
}));
|
2017-07-17 19:29:42 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The server can be connected as 'localhost'.
|
|
|
|
verifySecureSession(
|
2017-08-03 22:18:38 +02:00
|
|
|
loadKey('agent8-key.pem'),
|
|
|
|
loadKey('agent8-cert.pem'),
|
|
|
|
loadKey('fake-startcom-root-cert.pem'));
|
2017-07-17 19:29:42 +02:00
|
|
|
|
|
|
|
// Custom servername is specified.
|
|
|
|
verifySecureSession(
|
2017-08-03 22:18:38 +02:00
|
|
|
loadKey('agent1-key.pem'),
|
|
|
|
loadKey('agent1-cert.pem'),
|
|
|
|
loadKey('ca1-cert.pem'),
|
|
|
|
{ servername: 'agent1' });
|