mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
allow options to pass to js ssl connection bindings
This commit is contained in:
parent
a1d00919b0
commit
9341efe669
@ -17,7 +17,10 @@ var Client = function(config) {
|
||||
this.database = config.database || defaults.database;
|
||||
this.port = config.port || defaults.port;
|
||||
this.host = config.host || defaults.host;
|
||||
this.connection = config.connection || new Connection({stream: config.stream});
|
||||
this.connection = config.connection || new Connection({
|
||||
stream: config.stream,
|
||||
ssl: config.ssl
|
||||
});
|
||||
this.queryQueue = [];
|
||||
this.password = config.password || defaults.password;
|
||||
this.binary = config.binary || defaults.binary;
|
||||
|
||||
@ -17,7 +17,7 @@ var Connection = function(config) {
|
||||
this.encoding = 'utf8';
|
||||
this.parsedStatements = {};
|
||||
this.writer = new Writer();
|
||||
this.checkSslResponse = false;
|
||||
this.ssl = config.ssl || false;
|
||||
};
|
||||
|
||||
util.inherits(Connection, EventEmitter);
|
||||
@ -26,10 +26,9 @@ var p = Connection.prototype;
|
||||
|
||||
p.connect = function(port, host) {
|
||||
|
||||
if(this.stream.readyState === 'closed'){
|
||||
if (this.stream.readyState === 'closed') {
|
||||
this.stream.connect(port, host);
|
||||
}
|
||||
else if(this.stream.readyState == 'open') {
|
||||
} else if (this.stream.readyState == 'open') {
|
||||
this.emit('connect');
|
||||
}
|
||||
|
||||
@ -39,47 +38,54 @@ p.connect = function(port, host) {
|
||||
self.emit('connect');
|
||||
});
|
||||
|
||||
this.on('sslresponse', function(msg) {
|
||||
if (msg.text == 0x53) {
|
||||
var tls = require('tls');
|
||||
self.stream.removeAllListeners();
|
||||
self.stream = tls.connect({ socket: self.stream, servername: host, rejectUnauthorized: true });
|
||||
self.stream.on('data', function(buffer) {
|
||||
self.setBuffer(buffer);
|
||||
var msg;
|
||||
while(msg = self.parseMessage()) {
|
||||
self.emit('message', msg);
|
||||
self.emit(msg.name, msg);
|
||||
}
|
||||
});
|
||||
self.stream.on('error', function(error) {
|
||||
self.emit('error', error);
|
||||
});
|
||||
self.emit('sslconnect');
|
||||
} else {
|
||||
throw new Error("The server doesn't support SSL/TLS connections.");
|
||||
}
|
||||
});
|
||||
|
||||
this.stream.on('data', function(buffer) {
|
||||
self.setBuffer(buffer);
|
||||
var msg;
|
||||
if (self.checkSslResponse) {
|
||||
while(msg = self.readSslResponse()) {
|
||||
self.emit('message', msg);
|
||||
self.emit(msg.name, msg);
|
||||
}
|
||||
} else {
|
||||
while(msg = self.parseMessage()) {
|
||||
self.emit('message', msg);
|
||||
self.emit(msg.name, msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.stream.on('error', function(error) {
|
||||
self.emit('error', error);
|
||||
});
|
||||
|
||||
if(this.ssl) {
|
||||
this.stream.once('data', function(buffer) {
|
||||
self.setBuffer(buffer);
|
||||
var msg = self.readSslResponse();
|
||||
self.emit('message', msg);
|
||||
self.emit(msg.name, msg);
|
||||
});
|
||||
this.once('sslresponse', function(msg) {
|
||||
if (msg.text == 0x53) {
|
||||
var tls = require('tls');
|
||||
self.stream.removeAllListeners();
|
||||
self.stream = tls.connect({
|
||||
socket: self.stream,
|
||||
servername: host,
|
||||
rejectUnauthorized: ssl.rejectUnauthorized,
|
||||
ca: ssl.ca,
|
||||
pfx: ssl.pfx,
|
||||
key: ssl.key,
|
||||
passphrase: ssl.passphrase,
|
||||
cert: ssl.cert,
|
||||
NPNProtocols: ssl.NPNProtocols
|
||||
});
|
||||
self.attachListeners(self.stream);
|
||||
self.emit('sslconnect');
|
||||
} else {
|
||||
self.emit('error', new Error("The server doesn't support SSL/TLS connections."));
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
this.attachListeners(this.stream);
|
||||
}
|
||||
};
|
||||
|
||||
p.attachListeners = function(stream) {
|
||||
var self = this;
|
||||
stream.on('data', function(buffer) {
|
||||
self.setBuffer(buffer);
|
||||
var msg;
|
||||
while(msg = self.parseMessage()) {
|
||||
self.emit('message', msg);
|
||||
self.emit(msg.name, msg);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
p.requestSsl = function(config) {
|
||||
|
||||
14
test/integration/client/ssl-tests.js
Normal file
14
test/integration/client/ssl-tests.js
Normal file
@ -0,0 +1,14 @@
|
||||
var pg = require(__dirname + '/../../../lib');
|
||||
var config = require(__dirname + '/test-helper').config;
|
||||
test('can connect with ssl', function() {
|
||||
return false;
|
||||
config.ssl = {
|
||||
rejectUnauthorized: false
|
||||
};
|
||||
pg.connect(config, assert.success(function(client) {
|
||||
return false;
|
||||
client.query('SELECT NOW()', assert.success(function() {
|
||||
pg.end();
|
||||
}));
|
||||
}));
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user