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_ci// This test ensures that the data received through tls over http tunnel
281cb0ef41Sopenharmony_ci// is same as what is sent.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst assert = require('assert');
311cb0ef41Sopenharmony_ciconst https = require('https');
321cb0ef41Sopenharmony_ciconst net = require('net');
331cb0ef41Sopenharmony_ciconst http = require('http');
341cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cilet gotRequest = false;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent1-key.pem');
391cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent1-cert.pem');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciconst options = { key, cert };
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst server = https.createServer(options, common.mustCall((req, res) => {
441cb0ef41Sopenharmony_ci  console.log('SERVER: got request');
451cb0ef41Sopenharmony_ci  res.writeHead(200, {
461cb0ef41Sopenharmony_ci    'content-type': 'text/plain'
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci  console.log('SERVER: sending response');
491cb0ef41Sopenharmony_ci  res.end('hello world\n');
501cb0ef41Sopenharmony_ci}));
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciconst proxy = net.createServer((clientSocket) => {
531cb0ef41Sopenharmony_ci  console.log('PROXY: got a client connection');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  let serverSocket = null;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  clientSocket.on('data', (chunk) => {
581cb0ef41Sopenharmony_ci    if (!serverSocket) {
591cb0ef41Sopenharmony_ci      // Verify the CONNECT request
601cb0ef41Sopenharmony_ci      assert.strictEqual(chunk.toString(),
611cb0ef41Sopenharmony_ci                         `CONNECT localhost:${server.address().port} ` +
621cb0ef41Sopenharmony_ci                         'HTTP/1.1\r\n' +
631cb0ef41Sopenharmony_ci                         'Proxy-Connections: keep-alive\r\n' +
641cb0ef41Sopenharmony_ci                         `Host: localhost:${proxy.address().port}\r\n` +
651cb0ef41Sopenharmony_ci                         'Connection: close\r\n\r\n');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci      console.log('PROXY: got CONNECT request');
681cb0ef41Sopenharmony_ci      console.log('PROXY: creating a tunnel');
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci      // create the tunnel
711cb0ef41Sopenharmony_ci      serverSocket = net.connect(server.address().port, common.mustCall(() => {
721cb0ef41Sopenharmony_ci        console.log('PROXY: replying to client CONNECT request');
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci        // Send the response
751cb0ef41Sopenharmony_ci        clientSocket.write('HTTP/1.1 200 OK\r\nProxy-Connections: keep' +
761cb0ef41Sopenharmony_ci          '-alive\r\nConnections: keep-alive\r\nVia: ' +
771cb0ef41Sopenharmony_ci          `localhost:${proxy.address().port}\r\n\r\n`);
781cb0ef41Sopenharmony_ci      }));
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci      serverSocket.on('data', (chunk) => {
811cb0ef41Sopenharmony_ci        clientSocket.write(chunk);
821cb0ef41Sopenharmony_ci      });
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci      serverSocket.on('end', common.mustCall(() => {
851cb0ef41Sopenharmony_ci        clientSocket.destroy();
861cb0ef41Sopenharmony_ci      }));
871cb0ef41Sopenharmony_ci    } else {
881cb0ef41Sopenharmony_ci      serverSocket.write(chunk);
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci  });
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  clientSocket.on('end', () => {
931cb0ef41Sopenharmony_ci    serverSocket.destroy();
941cb0ef41Sopenharmony_ci  });
951cb0ef41Sopenharmony_ci});
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciserver.listen(0);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciproxy.listen(0, common.mustCall(() => {
1001cb0ef41Sopenharmony_ci  console.log('CLIENT: Making CONNECT request');
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  const req = http.request({
1031cb0ef41Sopenharmony_ci    port: proxy.address().port,
1041cb0ef41Sopenharmony_ci    method: 'CONNECT',
1051cb0ef41Sopenharmony_ci    path: `localhost:${server.address().port}`,
1061cb0ef41Sopenharmony_ci    headers: {
1071cb0ef41Sopenharmony_ci      'Proxy-Connections': 'keep-alive'
1081cb0ef41Sopenharmony_ci    }
1091cb0ef41Sopenharmony_ci  });
1101cb0ef41Sopenharmony_ci  req.useChunkedEncodingByDefault = false; // for v0.6
1111cb0ef41Sopenharmony_ci  req.on('response', onResponse); // for v0.6
1121cb0ef41Sopenharmony_ci  req.on('upgrade', onUpgrade);   // for v0.6
1131cb0ef41Sopenharmony_ci  req.on('connect', onConnect);   // for v0.7 or later
1141cb0ef41Sopenharmony_ci  req.end();
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  function onResponse(res) {
1171cb0ef41Sopenharmony_ci    // Very hacky. This is necessary to avoid http-parser leaks.
1181cb0ef41Sopenharmony_ci    res.upgrade = true;
1191cb0ef41Sopenharmony_ci  }
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  function onUpgrade(res, socket, head) {
1221cb0ef41Sopenharmony_ci    // Hacky.
1231cb0ef41Sopenharmony_ci    process.nextTick(() => {
1241cb0ef41Sopenharmony_ci      onConnect(res, socket, head);
1251cb0ef41Sopenharmony_ci    });
1261cb0ef41Sopenharmony_ci  }
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  function onConnect(res, socket, header) {
1291cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 200);
1301cb0ef41Sopenharmony_ci    console.log('CLIENT: got CONNECT response');
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci    // detach the socket
1331cb0ef41Sopenharmony_ci    socket.removeAllListeners('data');
1341cb0ef41Sopenharmony_ci    socket.removeAllListeners('close');
1351cb0ef41Sopenharmony_ci    socket.removeAllListeners('error');
1361cb0ef41Sopenharmony_ci    socket.removeAllListeners('drain');
1371cb0ef41Sopenharmony_ci    socket.removeAllListeners('end');
1381cb0ef41Sopenharmony_ci    socket.ondata = null;
1391cb0ef41Sopenharmony_ci    socket.onend = null;
1401cb0ef41Sopenharmony_ci    socket.ondrain = null;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci    console.log('CLIENT: Making HTTPS request');
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    https.get({
1451cb0ef41Sopenharmony_ci      path: '/foo',
1461cb0ef41Sopenharmony_ci      key: key,
1471cb0ef41Sopenharmony_ci      cert: cert,
1481cb0ef41Sopenharmony_ci      socket: socket,  // reuse the socket
1491cb0ef41Sopenharmony_ci      agent: false,
1501cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1511cb0ef41Sopenharmony_ci    }, (res) => {
1521cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200);
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci      res.on('data', common.mustCall((chunk) => {
1551cb0ef41Sopenharmony_ci        assert.strictEqual(chunk.toString(), 'hello world\n');
1561cb0ef41Sopenharmony_ci        console.log('CLIENT: got HTTPS response');
1571cb0ef41Sopenharmony_ci        gotRequest = true;
1581cb0ef41Sopenharmony_ci      }));
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
1611cb0ef41Sopenharmony_ci        proxy.close();
1621cb0ef41Sopenharmony_ci        server.close();
1631cb0ef41Sopenharmony_ci      }));
1641cb0ef41Sopenharmony_ci    }).on('error', (er) => {
1651cb0ef41Sopenharmony_ci      // We're ok with getting ECONNRESET in this test, but it's
1661cb0ef41Sopenharmony_ci      // timing-dependent, and thus unreliable. Any other errors
1671cb0ef41Sopenharmony_ci      // are just failures, though.
1681cb0ef41Sopenharmony_ci      if (er.code !== 'ECONNRESET')
1691cb0ef41Sopenharmony_ci        throw er;
1701cb0ef41Sopenharmony_ci    }).end();
1711cb0ef41Sopenharmony_ci  }
1721cb0ef41Sopenharmony_ci}));
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ciprocess.on('exit', () => {
1751cb0ef41Sopenharmony_ci  assert.ok(gotRequest);
1761cb0ef41Sopenharmony_ci});
177