summaryrefslogtreecommitdiff
path: root/desktop/node_modules/global-agent/src/classes/HttpsProxyAgent.js
blob: 24d724f097f735ecce4f1f77044f199bad0e667d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow

import net from 'net';
import tls from 'tls';
import type {
  ConnectionCallbackType,
  ConnectionConfigurationType,
} from '../types';
import Agent from './Agent';

class HttpsProxyAgent extends Agent {
  // eslint-disable-next-line unicorn/prevent-abbreviations
  constructor (...args: *) {
    super(...args);

    this.protocol = 'https:';
    this.defaultPort = 443;
  }

  createConnection (configuration: ConnectionConfigurationType, callback: ConnectionCallbackType) {
    const socket = net.connect(
      configuration.proxy.port,
      configuration.proxy.hostname,
    );

    socket.on('error', (error) => {
      callback(error);
    });

    socket.once('data', () => {
      const secureSocket = tls.connect({
        ...configuration.tls,
        socket,
      });

      callback(null, secureSocket);
    });

    let connectMessage = '';

    connectMessage += 'CONNECT ' + configuration.host + ':' + configuration.port + ' HTTP/1.1\r\n';
    connectMessage += 'Host: ' + configuration.host + ':' + configuration.port + '\r\n';

    if (configuration.proxy.authorization) {
      connectMessage += 'Proxy-Authorization: Basic ' + Buffer.from(configuration.proxy.authorization).toString('base64') + '\r\n';
    }

    connectMessage += '\r\n';

    socket.write(connectMessage);
  }
}

export default HttpsProxyAgent;