11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciif (!common.hasCrypto) 251cb0ef41Sopenharmony_ci common.skip('missing crypto'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst assert = require('assert'); 281cb0ef41Sopenharmony_ciconst tls = require('tls'); 291cb0ef41Sopenharmony_ciconst cluster = require('cluster'); 301cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst workerCount = 4; 331cb0ef41Sopenharmony_ciconst expectedReqCount = 16; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciif (cluster.isPrimary) { 361cb0ef41Sopenharmony_ci let reusedCount = 0; 371cb0ef41Sopenharmony_ci let reqCount = 0; 381cb0ef41Sopenharmony_ci let lastSession = null; 391cb0ef41Sopenharmony_ci let shootOnce = false; 401cb0ef41Sopenharmony_ci let workerPort = null; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci function shoot() { 431cb0ef41Sopenharmony_ci console.error('[primary] connecting', 441cb0ef41Sopenharmony_ci workerPort, 'session?', !!lastSession); 451cb0ef41Sopenharmony_ci const c = tls.connect(workerPort, { 461cb0ef41Sopenharmony_ci session: lastSession, 471cb0ef41Sopenharmony_ci rejectUnauthorized: false 481cb0ef41Sopenharmony_ci }, () => { 491cb0ef41Sopenharmony_ci c.end(); 501cb0ef41Sopenharmony_ci }).on('close', () => { 511cb0ef41Sopenharmony_ci // Wait for close to shoot off another connection. We don't want to shoot 521cb0ef41Sopenharmony_ci // until a new session is allocated, if one will be. The new session is 531cb0ef41Sopenharmony_ci // not guaranteed on secureConnect (it depends on TLS1.2 vs TLS1.3), but 541cb0ef41Sopenharmony_ci // it is guaranteed to happen before the connection is closed. 551cb0ef41Sopenharmony_ci if (++reqCount === expectedReqCount) { 561cb0ef41Sopenharmony_ci Object.keys(cluster.workers).forEach(function(id) { 571cb0ef41Sopenharmony_ci cluster.workers[id].send('die'); 581cb0ef41Sopenharmony_ci }); 591cb0ef41Sopenharmony_ci } else { 601cb0ef41Sopenharmony_ci shoot(); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci }).once('session', (session) => { 631cb0ef41Sopenharmony_ci assert(!lastSession); 641cb0ef41Sopenharmony_ci lastSession = session; 651cb0ef41Sopenharmony_ci }); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci c.resume(); // See close_notify comment in server 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci function fork() { 711cb0ef41Sopenharmony_ci const worker = cluster.fork(); 721cb0ef41Sopenharmony_ci worker.on('message', ({ msg, port }) => { 731cb0ef41Sopenharmony_ci console.error('[primary] got %j', msg); 741cb0ef41Sopenharmony_ci if (msg === 'reused') { 751cb0ef41Sopenharmony_ci ++reusedCount; 761cb0ef41Sopenharmony_ci } else if (msg === 'listening' && !shootOnce) { 771cb0ef41Sopenharmony_ci workerPort = port || workerPort; 781cb0ef41Sopenharmony_ci shootOnce = true; 791cb0ef41Sopenharmony_ci shoot(); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci worker.on('exit', () => { 841cb0ef41Sopenharmony_ci console.error('[primary] worker died'); 851cb0ef41Sopenharmony_ci }); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci for (let i = 0; i < workerCount; i++) { 881cb0ef41Sopenharmony_ci fork(); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci process.on('exit', () => { 921cb0ef41Sopenharmony_ci assert.strictEqual(reqCount, expectedReqCount); 931cb0ef41Sopenharmony_ci assert.strictEqual(reusedCount + 1, reqCount); 941cb0ef41Sopenharmony_ci }); 951cb0ef41Sopenharmony_ci return; 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciconst key = fixtures.readKey('rsa_private.pem'); 991cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('rsa_cert.crt'); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciconst options = { key, cert }; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciconst server = tls.createServer(options, (c) => { 1041cb0ef41Sopenharmony_ci console.error('[worker] connection reused?', c.isSessionReused()); 1051cb0ef41Sopenharmony_ci if (c.isSessionReused()) { 1061cb0ef41Sopenharmony_ci process.send({ msg: 'reused' }); 1071cb0ef41Sopenharmony_ci } else { 1081cb0ef41Sopenharmony_ci process.send({ msg: 'not-reused' }); 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci // Used to just .end(), but that means client gets close_notify before 1111cb0ef41Sopenharmony_ci // NewSessionTicket. Send data until that problem is solved. 1121cb0ef41Sopenharmony_ci c.end('x'); 1131cb0ef41Sopenharmony_ci}); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciserver.listen(0, () => { 1161cb0ef41Sopenharmony_ci const { port } = server.address(); 1171cb0ef41Sopenharmony_ci process.send({ 1181cb0ef41Sopenharmony_ci msg: 'listening', 1191cb0ef41Sopenharmony_ci port, 1201cb0ef41Sopenharmony_ci }); 1211cb0ef41Sopenharmony_ci}); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciprocess.on('message', function listener(msg) { 1241cb0ef41Sopenharmony_ci console.error('[worker] got %j', msg); 1251cb0ef41Sopenharmony_ci if (msg === 'die') { 1261cb0ef41Sopenharmony_ci server.close(() => { 1271cb0ef41Sopenharmony_ci console.error('[worker] server close'); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci process.exit(); 1301cb0ef41Sopenharmony_ci }); 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci}); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciprocess.on('exit', () => { 1351cb0ef41Sopenharmony_ci console.error('[worker] exit'); 1361cb0ef41Sopenharmony_ci}); 137