Skip to content

Commit

Permalink
tls_socket: Replaced previous classes with es6 classes #2190
Browse files Browse the repository at this point in the history
  • Loading branch information
varunzxzx authored and msimerson committed Oct 7, 2017
1 parent 6429fdf commit 01c3a80
Showing 1 changed file with 117 additions and 117 deletions.
234 changes: 117 additions & 117 deletions tls_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,122 +34,122 @@ class pluggableStream extends stream.Stream {
this._pendingCallbacks = [];
if (socket) this.attach(socket);
}
}

pluggableStream.prototype.pause = function () {
if (this.targetsocket.pause) {
this.targetsocket.pause();
this.readable = false;
pause () {
if (this.targetsocket.pause) {
this.targetsocket.pause();
this.readable = false;
}
}
};

pluggableStream.prototype.resume = function () {
if (this.targetsocket.resume) {
this.readable = true;
this.targetsocket.resume();
resume () {
if (this.targetsocket.resume) {
this.readable = true;
this.targetsocket.resume();
}
}
};

pluggableStream.prototype.attach = function (socket) {
const self = this;
self.targetsocket = socket;
self.targetsocket.on('data', function (data) {
self.emit('data', data);
});
self.targetsocket.on('connect', (a, b) => {
self.emit('connect', a, b);
});
self.targetsocket.on('secureConnection', function (a, b) {
self.emit('secureConnection', a, b);
self.emit('secure', a, b);
});
self.targetsocket.on('secure', function (a, b) {
self.emit('secureConnection', a, b);
self.emit('secure', a, b);
});
self.targetsocket.on('end', function () {
self.writable = self.targetsocket.writable;
self.emit('end');
});
self.targetsocket.on('close', function (had_error) {
self.writable = self.targetsocket.writable;
self.emit('close', had_error);
});
self.targetsocket.on('drain', function () {
self.emit('drain');
});
self.targetsocket.once('error', function (exception) {
self.writable = self.targetsocket.writable;
self.emit('error', exception);
});
self.targetsocket.on('timeout', function () {
self.emit('timeout');
});
if (self.targetsocket.remotePort) {
self.remotePort = self.targetsocket.remotePort;
}
if (self.targetsocket.remoteAddress) {
self.remoteAddress = self.targetsocket.remoteAddress;
attach (socket) {
const self = this;
self.targetsocket = socket;
self.targetsocket.on('data', function (data) {
self.emit('data', data);
});
self.targetsocket.on('connect', (a, b) => {
self.emit('connect', a, b);
});
self.targetsocket.on('secureConnection', function (a, b) {
self.emit('secureConnection', a, b);
self.emit('secure', a, b);
});
self.targetsocket.on('secure', function (a, b) {
self.emit('secureConnection', a, b);
self.emit('secure', a, b);
});
self.targetsocket.on('end', function () {
self.writable = self.targetsocket.writable;
self.emit('end');
});
self.targetsocket.on('close', function (had_error) {
self.writable = self.targetsocket.writable;
self.emit('close', had_error);
});
self.targetsocket.on('drain', function () {
self.emit('drain');
});
self.targetsocket.once('error', function (exception) {
self.writable = self.targetsocket.writable;
self.emit('error', exception);
});
self.targetsocket.on('timeout', function () {
self.emit('timeout');
});
if (self.targetsocket.remotePort) {
self.remotePort = self.targetsocket.remotePort;
}
if (self.targetsocket.remoteAddress) {
self.remoteAddress = self.targetsocket.remoteAddress;
}
}
};

pluggableStream.prototype.clean = function (data) {
if (this.targetsocket && this.targetsocket.removeAllListeners) {
this.targetsocket.removeAllListeners('data');
this.targetsocket.removeAllListeners('secureConnection');
this.targetsocket.removeAllListeners('secure');
this.targetsocket.removeAllListeners('end');
this.targetsocket.removeAllListeners('close');
this.targetsocket.removeAllListeners('error');
this.targetsocket.removeAllListeners('drain');
clean (data) {
if (this.targetsocket && this.targetsocket.removeAllListeners) {
this.targetsocket.removeAllListeners('data');
this.targetsocket.removeAllListeners('secureConnection');
this.targetsocket.removeAllListeners('secure');
this.targetsocket.removeAllListeners('end');
this.targetsocket.removeAllListeners('close');
this.targetsocket.removeAllListeners('error');
this.targetsocket.removeAllListeners('drain');
}
this.targetsocket = {};
this.targetsocket.write = function () {};
}
this.targetsocket = {};
this.targetsocket.write = function () {};
};

pluggableStream.prototype.write = function (data, encoding, callback) {
if (this.targetsocket.write) {
return this.targetsocket.write(data, encoding, callback);
write (data, encoding, callback) {
if (this.targetsocket.write) {
return this.targetsocket.write(data, encoding, callback);
}
return false;
}
return false;
};

pluggableStream.prototype.end = function (data, encoding) {
if (this.targetsocket.end) {
return this.targetsocket.end(data, encoding);
end (data, encoding) {
if (this.targetsocket.end) {
return this.targetsocket.end(data, encoding);
}
}
};

pluggableStream.prototype.destroySoon = function () {
if (this.targetsocket.destroySoon) {
return this.targetsocket.destroySoon();
destroySoon () {
if (this.targetsocket.destroySoon) {
return this.targetsocket.destroySoon();
}
}
};

pluggableStream.prototype.destroy = function () {
if (this.targetsocket.destroy) {
return this.targetsocket.destroy();
destroy () {
if (this.targetsocket.destroy) {
return this.targetsocket.destroy();
}
}
};

pluggableStream.prototype.setKeepAlive = function (bool) {
this._keepalive = bool;
return this.targetsocket.setKeepAlive(bool);
};
setKeepAlive (bool) {
this._keepalive = bool;
return this.targetsocket.setKeepAlive(bool);
}

pluggableStream.prototype.setNoDelay = function (/* true||false */) {
};
setNoDelay (/* true||false */) {
}

pluggableStream.prototype.unref = function () {
return this.targetsocket.unref();
};
unref () {
return this.targetsocket.unref();
}

pluggableStream.prototype.setTimeout = function (timeout) {
this._timeout = timeout;
return this.targetsocket.setTimeout(timeout);
};
setTimeout (timeout) {
this._timeout = timeout;
return this.targetsocket.setTimeout(timeout);
}
}

exports.parse_x509_names = function (string) {
exports.parse_x509_names = string => {
// receives the text value of a x509 certificate and returns an array of
// of names extracted from the Subject CN and the v3 Subject Alternate Names
const names_found = [];
Expand Down Expand Up @@ -179,7 +179,7 @@ exports.parse_x509_names = function (string) {
return names_found;
}

exports.parse_x509_expire = function (file, string) {
exports.parse_x509_expire = (file, string) => {

const dateMatch = /Not After : (.*)/.exec(string);
if (!dateMatch) return;
Expand All @@ -188,7 +188,7 @@ exports.parse_x509_expire = function (file, string) {
return new Date(dateMatch[1]);
}

exports.parse_x509 = function (string) {
exports.parse_x509 = string => {
const res = {};

const match = /^([^-]*)?([-]+BEGIN (?:\w+\s)?PRIVATE KEY[-]+[^-]+[-]+END (?:\w+\s)?PRIVATE KEY[-]+\n)([^]*)$/.exec(string);
Expand All @@ -207,7 +207,7 @@ exports.parse_x509 = function (string) {
return res;
}

exports.load_tls_ini = function () {
exports.load_tls_ini = () => {
const tlss = this;

log.loginfo('loading tls.ini');
Expand All @@ -229,7 +229,7 @@ exports.load_tls_ini = function () {
'+main.honorCipherOrder',
'-main.requestOCSP',
]
}, function () {
}, () => {
tlss.load_tls_ini();
});

Expand Down Expand Up @@ -259,12 +259,12 @@ exports.load_tls_ini = function () {
return cfg;
}

exports.saveOpt = function (name, opt, val) {
exports.saveOpt = (name, opt, val) => {
if (certsByHost[name] === undefined) certsByHost[name] = {};
certsByHost[name][opt] = val;
}

exports.applySocketOpts = function (name) {
exports.applySocketOpts = name => {
const tlss = this;

if (!certsByHost[name]) certsByHost[name] = {};
Expand Down Expand Up @@ -325,7 +325,7 @@ exports.applySocketOpts = function (name) {
})
}

exports.load_default_opts = function () {
exports.load_default_opts = () => {
const tlss = this;

const cfg = certsByHost['*'];
Expand Down Expand Up @@ -389,7 +389,7 @@ function SNICallback (servername, sniDone) {
sniDone(null, ctxByHost[servername]);
}

exports.get_certs_dir = function (tlsDir, done) {
exports.get_certs_dir = (tlsDir, done) => {
const tlss = this;

tlss.config.getDir(tlsDir, {}, (iterErr, files) => {
Expand Down Expand Up @@ -465,7 +465,7 @@ exports.get_certs_dir = function (tlsDir, done) {
})
}

exports.getSocketOpts = function (name, done) {
exports.getSocketOpts = (name, done) => {
const tlss = this;

function getTlsOpts () {
Expand All @@ -490,7 +490,7 @@ exports.getSocketOpts = function (name, done) {
function pipe (cleartext, socket) {
cleartext.socket = socket;

function onerror (e) {
const onerror = e => {
}

function onclose () {
Expand All @@ -502,7 +502,7 @@ function pipe (cleartext, socket) {
socket.on('close', onclose);
}

exports.ensureDhparams = function (done) {
exports.ensureDhparams = done => {
const tlss = this;

// empty/missing dhparams file
Expand Down Expand Up @@ -538,7 +538,7 @@ exports.ensureDhparams = function (done) {
});
}

exports.addOCSP = function (server) {
exports.addOCSP = server => {
if (!ocsp) {
log.logdebug('addOCSP: not available');
return;
Expand All @@ -550,17 +550,17 @@ exports.addOCSP = function (server) {
}

log.logdebug('adding OCSPRequest listener');
server.on('OCSPRequest', function (cert, issuer, ocr_cb) {
server.on('OCSPRequest', (cert, issuer, ocr_cb) => {
log.logdebug('OCSPRequest: ' + cert);
ocsp.getOCSPURI(cert, function (err, uri) {
ocsp.getOCSPURI(cert, (err, uri) => {
log.logdebug('OCSP Request, URI: ' + uri + ', err=' +err);
if (err) return ocr_cb(err);
if (uri === null) return ocr_cb(); // not working OCSP server

const req = ocsp.request.generate(cert, issuer);

// look for a cached value first
ocspCache.probe(req.id, function (err2, cached) {
ocspCache.probe(req.id, (err2, cached) => {
if (err2) return ocr_cb(err2);

if (cached) {
Expand All @@ -580,7 +580,7 @@ exports.addOCSP = function (server) {
})
}

exports.shutdown = function () {
exports.shutdown = () => {
if (ocsp) cleanOcspCache();
}

Expand All @@ -595,13 +595,13 @@ exports.certsByHost = certsByHost;
exports.ocsp = ocsp;

function createServer (cb) {
const server = net.createServer(function (cryptoSocket) {
const server = net.createServer(cryptoSocket => {

const socket = new pluggableStream(cryptoSocket);

exports.addOCSP(server);

socket.upgrade = function (cb2) {
socket.upgrade = cb2 => {
log.logdebug('Upgrading to TLS');

socket.clean();
Expand All @@ -616,10 +616,10 @@ function createServer (cb) {
pipe(cleartext, cryptoSocket);

cleartext
.on('error', (exception) => {
.on('error', exception => {
socket.emit('error', exception);
})
.on('secure', function () {
.on('secure', () => {
log.logdebug('TLS secured.');
socket.emit('secure');
if (cb2) cb2(
Expand Down Expand Up @@ -662,7 +662,7 @@ function connect (port, host, cb) {

const socket = new pluggableStream(cryptoSocket);

socket.upgrade = function (options, cb2) {
socket.upgrade = (options, cb2) => {
socket.clean();
cryptoSocket.removeAllListeners('data');

Expand All @@ -673,7 +673,7 @@ function connect (port, host, cb) {

pipe(cleartext, cryptoSocket);

cleartext.on('error', function (err) {
cleartext.on('error', err => {
if (err.reason) {
log.logerror("client TLS error: " + err);
}
Expand Down

0 comments on commit 01c3a80

Please sign in to comment.