Skip to content

Commit 158562f

Browse files
harbulotbrianc
authored andcommitted
Initial support for SSL/TLS connections.
1 parent 66b569c commit 158562f

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/client.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var Client = function(config) {
2424
this.encoding = 'utf8';
2525
this.processID = null;
2626
this.secretKey = null;
27-
var self = this;
27+
this.ssl = config.ssl || false;
2828
};
2929

3030
util.inherits(Client, EventEmitter);
@@ -43,6 +43,16 @@ p.connect = function(callback) {
4343

4444
//once connection is established send startup message
4545
con.on('connect', function() {
46+
if (self.ssl) {
47+
con.requestSsl();
48+
} else {
49+
con.startup({
50+
user: self.user,
51+
database: self.database
52+
});
53+
}
54+
});
55+
con.on('sslconnect', function() {
4656
con.startup({
4757
user: self.user,
4858
database: self.database

lib/connection.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Connection = function(config) {
1717
this.encoding = 'utf8';
1818
this.parsedStatements = {};
1919
this.writer = new Writer();
20+
this.checkSslResponse = false;
2021
};
2122

2223
util.inherits(Connection, EventEmitter);
@@ -37,14 +38,42 @@ p.connect = function(port, host) {
3738
this.stream.on('connect', function() {
3839
self.emit('connect');
3940
});
40-
41+
42+
this.on('sslresponse', function(msg) {
43+
if (msg.text == 0x53) {
44+
var tls = require('tls');
45+
self.stream.removeAllListeners();
46+
self.stream = tls.connect({ socket: self.stream, servername: host, rejectUnauthorized: true });
47+
self.stream.on('data', function(buffer) {
48+
self.setBuffer(buffer);
49+
var msg;
50+
while(msg = self.parseMessage()) {
51+
self.emit('message', msg);
52+
self.emit(msg.name, msg);
53+
}
54+
});
55+
self.stream.on('error', function(error) {
56+
self.emit('error', error);
57+
});
58+
self.emit('sslconnect');
59+
} else {
60+
throw new Error("The server doesn't support SSL/TLS connections.");
61+
}
62+
});
4163

4264
this.stream.on('data', function(buffer) {
4365
self.setBuffer(buffer);
4466
var msg;
45-
while(msg = self.parseMessage()) {
46-
self.emit('message', msg);
47-
self.emit(msg.name, msg);
67+
if (self.checkSslResponse) {
68+
while(msg = self.readSslResponse()) {
69+
self.emit('message', msg);
70+
self.emit(msg.name, msg);
71+
}
72+
} else {
73+
while(msg = self.parseMessage()) {
74+
self.emit('message', msg);
75+
self.emit(msg.name, msg);
76+
}
4877
}
4978
});
5079

@@ -53,6 +82,22 @@ p.connect = function(port, host) {
5382
});
5483
};
5584

85+
p.requestSsl = function(config) {
86+
this.checkSslResponse = true;
87+
88+
var bodyBuffer = this.writer
89+
.addInt16(0x04D2)
90+
.addInt16(0x162F).flush();
91+
92+
var length = bodyBuffer.length + 4;
93+
94+
var buffer = new Writer()
95+
.addInt32(length)
96+
.add(bodyBuffer)
97+
.join();
98+
this.stream.write(buffer);
99+
}
100+
56101
p.startup = function(config) {
57102
var bodyBuffer = this.writer
58103
.addInt16(3)
@@ -225,6 +270,16 @@ p.setBuffer = function(buffer) {
225270
this.offset = 0;
226271
};
227272

273+
p.readSslResponse = function() {
274+
var remaining = this.buffer.length - (this.offset);
275+
if(remaining < 1) {
276+
this.lastBuffer = this.buffer;
277+
this.lastOffset = this.offset;
278+
return false;
279+
}
280+
return { name: 'sslresponse', text: this.buffer[this.offset++] };
281+
};
282+
228283
p.parseMessage = function() {
229284
var remaining = this.buffer.length - (this.offset);
230285
if(remaining < 5) {

0 commit comments

Comments
 (0)