Skip to content

Commit 5514def

Browse files
committed
support tls
1 parent d644cb8 commit 5514def

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

lib/commands/client_handshake.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const Packets = require('../packets/index.js');
1515
const ClientConstants = require('../constants/client.js');
1616
const CharsetToEncoding = require('../constants/charset_encodings.js');
1717
const auth41 = require('../auth_41.js');
18+
const {secureStream} = require('../stream.js');
1819

1920
function flagNames(flags) {
2021
const res = [];
@@ -146,25 +147,11 @@ class ClientHandshake extends Command {
146147
// send ssl upgrade request and immediately upgrade connection to secure
147148
this.clientFlags |= ClientConstants.SSL;
148149
this.sendSSLRequest(connection);
149-
connection.startTLS(err => {
150-
// after connection is secure
151-
if (err) {
152-
// SSL negotiation error are fatal
153-
err.code = 'HANDSHAKE_SSL_ERROR';
154-
err.fatal = true;
155-
this.emit('error', err);
156-
return;
157-
}
158-
// rest of communication is encrypted
159-
this.sendCredentials(connection).catch(err => {
160-
this.emit('error', err);
161-
});
162-
});
163-
} else {
164-
this.sendCredentials(connection).catch(err => {
165-
this.emit('error', err);
166-
});
150+
secureStream(connection)
167151
}
152+
this.sendCredentials(connection).catch(err => {
153+
this.emit('error', err);
154+
});
168155
if (multiFactorAuthentication) {
169156
// if the server supports multi-factor authentication, we enable it in
170157
// the client

lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
'use strict';
1717

18-
const Tls = require('tls');
1918
const EventEmitter = require('events').EventEmitter;
2019
const Readable = require('stream').Readable;
2120
const Queue = require('denque');
@@ -341,6 +340,7 @@ class Connection extends EventEmitter {
341340

342341
// 0.11+ environment
343342
startTLS(onSecure) {
343+
const Tls = require('tls');
344344
if (this.config.debug) {
345345
// eslint-disable-next-line no-console
346346
console.log('Upgrading connection to TLS');

lib/stream.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,31 @@ module.exports.getStream = function getStream(ssl = false) {
1212
const { CloudflareSocket } = require('pg-cloudflare')
1313
return new CloudflareSocket(ssl);
1414
}
15+
16+
/**
17+
* Get a TLS secured socket, compatible with the current environment,
18+
* using the socket and other settings given in `options`.
19+
*/
20+
module.exports.secureStream = function secureStream(connection) {
21+
const Tls = require('tls');
22+
if (Tls.connect) {
23+
connection.startTLS(err => {
24+
// after connection is secure
25+
if (err) {
26+
// SSL negotiation error are fatal
27+
err.code = 'HANDSHAKE_SSL_ERROR';
28+
err.fatal = true;
29+
this.emit('error', err);
30+
}
31+
});
32+
return
33+
}
34+
try {
35+
connection.stream.startTls({});
36+
}catch (err) {
37+
// SSL negotiation error are fatal
38+
err.code = 'HANDSHAKE_SSL_ERROR';
39+
err.fatal = true;
40+
this.emit('error', err);
41+
}
42+
}

0 commit comments

Comments
 (0)