• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const fixtures = require('../common/fixtures');
25
26if (!common.hasCrypto)
27  common.skip('missing crypto');
28
29const assert = require('assert');
30const tls = require('tls');
31
32const net = require('net');
33
34const options = {
35  key: fixtures.readKey('rsa_private.pem'),
36  cert: fixtures.readKey('rsa_cert.crt')
37};
38
39const body = 'A'.repeat(40000);
40
41// the "proxy" server
42const a = tls.createServer(options, function(socket) {
43  const myOptions = {
44    host: '127.0.0.1',
45    port: b.address().port,
46    rejectUnauthorized: false
47  };
48  const dest = net.connect(myOptions);
49  dest.pipe(socket);
50  socket.pipe(dest);
51
52  dest.on('end', function() {
53    socket.destroy();
54  });
55});
56
57// the "target" server
58const b = tls.createServer(options, function(socket) {
59  socket.end(body);
60});
61
62a.listen(0, function() {
63  b.listen(0, function() {
64    const myOptions = {
65      host: '127.0.0.1',
66      port: a.address().port,
67      rejectUnauthorized: false
68    };
69    const socket = tls.connect(myOptions);
70    const ssl = tls.connect({
71      socket: socket,
72      rejectUnauthorized: false
73    });
74    ssl.setEncoding('utf8');
75    let buf = '';
76    ssl.on('data', function(data) {
77      buf += data;
78    });
79    ssl.on('end', common.mustCall(function() {
80      assert.strictEqual(buf, body);
81      ssl.end();
82      a.close();
83      b.close();
84    }));
85  });
86});
87