• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const bench = common.createBenchmark(main, {
4  dur: [5],
5  securing: ['SecurePair', 'TLSSocket', 'clear'],
6  size: [100, 1024, 1024 * 1024]
7}, {
8  flags: ['--no-warnings']
9});
10
11const fixtures = require('../../test/common/fixtures');
12const tls = require('tls');
13const net = require('net');
14
15const REDIRECT_PORT = 28347;
16
17function main({ dur, size, securing }) {
18  const chunk = Buffer.alloc(size, 'b');
19
20  const options = {
21    key: fixtures.readKey('rsa_private.pem'),
22    cert: fixtures.readKey('rsa_cert.crt'),
23    ca: fixtures.readKey('rsa_ca.crt'),
24    ciphers: 'AES256-GCM-SHA384',
25    isServer: true,
26    requestCert: true,
27    rejectUnauthorized: true,
28  };
29
30  const server = net.createServer(onRedirectConnection);
31  server.listen(REDIRECT_PORT, () => {
32    const proxy = net.createServer(onProxyConnection);
33    proxy.listen(common.PORT, () => {
34      const clientOptions = {
35        port: common.PORT,
36        ca: options.ca,
37        key: options.key,
38        cert: options.cert,
39        isServer: false,
40        rejectUnauthorized: false,
41      };
42      const network = securing === 'clear' ? net : tls;
43      const conn = network.connect(clientOptions, () => {
44        setTimeout(() => {
45          const mbits = (received * 8) / (1024 * 1024);
46          bench.end(mbits);
47          if (conn)
48            conn.destroy();
49          server.close();
50          proxy.close();
51        }, dur * 1000);
52        bench.start();
53        conn.on('drain', write);
54        write();
55      });
56      conn.on('error', (e) => {
57        throw new Error(`Client error: ${e}`);
58      });
59
60      function write() {
61        while (false !== conn.write(chunk));
62      }
63    });
64  });
65
66  function onProxyConnection(conn) {
67    const client = net.connect(REDIRECT_PORT, () => {
68      switch (securing) {
69        case 'SecurePair':
70          securePair(conn, client);
71          break;
72        case 'TLSSocket':
73          secureTLSSocket(conn, client);
74          break;
75        case 'clear':
76          conn.pipe(client);
77          break;
78        default:
79          throw new Error('Invalid securing method');
80      }
81    });
82  }
83
84  function securePair(conn, client) {
85    const serverCtx = tls.createSecureContext(options);
86    const serverPair = tls.createSecurePair(serverCtx, true, true, false);
87    conn.pipe(serverPair.encrypted);
88    serverPair.encrypted.pipe(conn);
89    serverPair.on('error', (error) => {
90      throw new Error(`Pair error: ${error}`);
91    });
92    serverPair.cleartext.pipe(client);
93  }
94
95  function secureTLSSocket(conn, client) {
96    const serverSocket = new tls.TLSSocket(conn, options);
97    serverSocket.on('error', (e) => {
98      throw new Error(`Socket error: ${e}`);
99    });
100    serverSocket.pipe(client);
101  }
102
103  let received = 0;
104  function onRedirectConnection(conn) {
105    conn.on('data', (chunk) => {
106      received += chunk.length;
107    });
108  }
109}
110