Fix test-securepair-server
This commit is contained in:
parent
a2b3c865c9
commit
a6f6532dfb
@ -145,17 +145,18 @@ function setImplmentationMethods (self) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function onReadable (readable, writeable) {
|
function onReadable (readable, writable) {
|
||||||
assert(this.socket);
|
assert(this.socket);
|
||||||
var socket = this.socket;
|
var socket = this.socket;
|
||||||
socket._onReadable();
|
socket._onReadable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function onWritable (readable, writeable) {
|
function onWritable (readable, writable) {
|
||||||
assert(this.socket);
|
assert(this.socket);
|
||||||
var socket = this.socket;
|
var socket = this.socket;
|
||||||
if (socket._connecting) {
|
if (socket._connecting) {
|
||||||
|
assert(socket.writable);
|
||||||
socket._onConnect();
|
socket._onConnect();
|
||||||
} else {
|
} else {
|
||||||
socket._onWritable();
|
socket._onWritable();
|
||||||
@ -472,6 +473,7 @@ Stream.prototype._onConnect = function () {
|
|||||||
// connection established
|
// connection established
|
||||||
this._connecting = false;
|
this._connecting = false;
|
||||||
this.resume();
|
this.resume();
|
||||||
|
assert(this.writable);
|
||||||
this.readable = this.writable = true;
|
this.readable = this.writable = true;
|
||||||
try {
|
try {
|
||||||
this.emit('connect');
|
this.emit('connect');
|
||||||
@ -690,10 +692,11 @@ Stream.prototype._shutdown = function () {
|
|||||||
if (!this.writable) {
|
if (!this.writable) {
|
||||||
throw new Error('The connection is not writable');
|
throw new Error('The connection is not writable');
|
||||||
} else {
|
} else {
|
||||||
if (this.readable) {
|
|
||||||
// readable and writable
|
// readable and writable
|
||||||
this.writable = false;
|
this.writable = false;
|
||||||
|
|
||||||
|
if (this.readable) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this._shutdownImpl();
|
this._shutdownImpl();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -66,11 +66,15 @@ function SecurePair(credentials, isServer) {
|
|||||||
/* Acts as a r/w stream to the cleartext side of the stream. */
|
/* Acts as a r/w stream to the cleartext side of the stream. */
|
||||||
this.cleartext = new stream.Stream();
|
this.cleartext = new stream.Stream();
|
||||||
this.cleartext.readable = true;
|
this.cleartext.readable = true;
|
||||||
|
this.cleartext.writable = true;
|
||||||
|
|
||||||
/* Acts as a r/w stream to the encrypted side of the stream. */
|
/* Acts as a r/w stream to the encrypted side of the stream. */
|
||||||
this.encrypted = new stream.Stream();
|
this.encrypted = new stream.Stream();
|
||||||
this.encrypted.readable = true;
|
this.encrypted.readable = true;
|
||||||
|
this.encrypted.writable = true;
|
||||||
|
|
||||||
this.cleartext.write = function (data) {
|
this.cleartext.write = function (data) {
|
||||||
|
if (typeof data == 'string') data = Buffer(data);
|
||||||
debug('clearIn data');
|
debug('clearIn data');
|
||||||
self._clearInPending.push(data);
|
self._clearInPending.push(data);
|
||||||
self._cycle();
|
self._cycle();
|
||||||
@ -104,6 +108,7 @@ function SecurePair(credentials, isServer) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.encrypted.pause = function () {
|
this.encrypted.pause = function () {
|
||||||
|
if (typeof data == 'string') data = Buffer(data);
|
||||||
debug('pause encrypted');
|
debug('pause encrypted');
|
||||||
self._encryptedWriteState = false;
|
self._encryptedWriteState = false;
|
||||||
};
|
};
|
||||||
|
@ -11,8 +11,10 @@ Stream.prototype.pipe = function (dest, options) {
|
|||||||
var source = this;
|
var source = this;
|
||||||
|
|
||||||
function ondata (chunk) {
|
function ondata (chunk) {
|
||||||
|
if (dest.writable) {
|
||||||
if (false === dest.write(chunk)) source.pause();
|
if (false === dest.write(chunk)) source.pause();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
source.on("data", ondata);
|
source.on("data", ondata);
|
||||||
|
|
||||||
@ -36,9 +38,9 @@ Stream.prototype.pipe = function (dest, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dest.on('close', function () {
|
dest.on('close', function () {
|
||||||
dest.removeListener('data', ondata);
|
source.removeListener('data', ondata);
|
||||||
dest.removeListener('drain', ondrain);
|
dest.removeListener('drain', ondrain);
|
||||||
dest.removeListener('end', onend);
|
source.removeListener('end', onend);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,50 +12,51 @@ var key = fs.readFileSync(join(common.fixturesDir, "agent.key")).toString();
|
|||||||
var cert = fs.readFileSync(join(common.fixturesDir, "agent.crt")).toString();
|
var cert = fs.readFileSync(join(common.fixturesDir, "agent.crt")).toString();
|
||||||
|
|
||||||
function log (a) {
|
function log (a) {
|
||||||
console.log('***server*** ' + a);
|
console.error('***server*** ' + a);
|
||||||
}
|
}
|
||||||
|
|
||||||
var server = net.createServer(function (socket) {
|
var server = net.createServer(function (socket) {
|
||||||
connections++;
|
connections++;
|
||||||
log('connection');
|
log('connection fd=' + socket.fd);
|
||||||
var sslcontext = crypto.createCredentials({key: key, cert: cert});
|
var sslcontext = crypto.createCredentials({key: key, cert: cert});
|
||||||
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
||||||
|
|
||||||
var spair = crypto.createPair(sslcontext, true);
|
var pair = crypto.createPair(sslcontext, true);
|
||||||
|
|
||||||
spair.encrypted.pipe(socket);
|
assert.ok(pair.encrypted.writable);
|
||||||
socket.pipe(spair.encrypted);
|
assert.ok(pair.cleartext.writable);
|
||||||
|
|
||||||
var clear = spair.cleartext;
|
pair.encrypted.pipe(socket);
|
||||||
|
socket.pipe(pair.encrypted);
|
||||||
|
|
||||||
log('i set it secure');
|
log('i set it secure');
|
||||||
|
|
||||||
spair.on('secure', function () {
|
pair.on('secure', function () {
|
||||||
log('connected+secure!');
|
log('connected+secure!');
|
||||||
clear.write(new Buffer('hello\r\n'));
|
pair.cleartext.write('hello\r\n');
|
||||||
log(spair.getPeerCertificate());
|
log(pair.getPeerCertificate());
|
||||||
log(spair.getCipher());
|
log(pair.getCipher());
|
||||||
});
|
});
|
||||||
|
|
||||||
clear.on('data', function (data) {
|
pair.cleartext.on('data', function (data) {
|
||||||
log('read %d bytes', data.length);
|
log('read bytes ' + data.length);
|
||||||
clear.write(data);
|
pair.cleartext.write(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('end', function (err) {
|
socket.on('end', function () {
|
||||||
log('all done: '+ err);
|
log('socket end');
|
||||||
clear.write(new Buffer('goodbye\r\n'));
|
pair.cleartext.write('goodbye\r\n');
|
||||||
clear.end();
|
pair.cleartext.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
clear.on('error', function(err) {
|
pair.cleartext.on('error', function(err) {
|
||||||
log('got error: ');
|
log('got error: ');
|
||||||
log(err);
|
log(err);
|
||||||
log(err.stack);
|
log(err.stack);
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
spair.encrypted.on('error', function(err) {
|
pair.encrypted.on('error', function(err) {
|
||||||
log('encrypted error: ');
|
log('encrypted error: ');
|
||||||
log(err);
|
log(err);
|
||||||
log(err.stack);
|
log(err.stack);
|
||||||
@ -69,7 +70,11 @@ var server = net.createServer(function (socket) {
|
|||||||
socket.destroy();
|
socket.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
spair.on('error', function(err) {
|
socket.on('close', function(err) {
|
||||||
|
log('socket closed');
|
||||||
|
});
|
||||||
|
|
||||||
|
pair.on('error', function(err) {
|
||||||
log('secure error: ');
|
log('secure error: ');
|
||||||
log(err);
|
log(err);
|
||||||
log(err.stack);
|
log(err.stack);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user