| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,14 +3,15 @@ var events = require('events'); | |||
| 3 | 3 | var stream = require('stream'); | |
| 4 | 4 | var assert = process.assert; | |
| 5 | 5 | ||
| 6 | - var debugLevel = parseInt(process.env.NODE_DEBUG, 16); | ||
| 7 | 6 | ||
| 7 | + var debugLevel = parseInt(process.env.NODE_DEBUG, 16); | ||
| 8 | 8 | function debug () { | |
| 9 | 9 | if (debugLevel & 0x2) { | |
| 10 | 10 | util.error.apply(this, arguments); | |
| 11 | 11 | } | |
| 12 | 12 | } | |
| 13 | 13 | ||
| 14 | + | ||
| 14 | 15 | /* Lazy Loaded crypto object */ | |
| 15 | 16 | var SecureStream = null; | |
| 16 | 17 | ||
@@ -69,24 +70,24 @@ function SecurePair(credentials, isServer) { | |||
| 69 | 70 | this.encrypted = new stream.Stream(); | |
| 70 | 71 | this.encrypted.readable = true; | |
| 71 | 72 | ||
| 72 | - this.cleartext.write = function(data) { | ||
| 73 | + this.cleartext.write = function (data) { | ||
| 73 | 74 | debug('clearIn data'); | |
| 74 | 75 | self._clearInPending.push(data); | |
| 75 | 76 | self._cycle(); | |
| 76 | 77 | return self._cleartextWriteState; | |
| 77 | 78 | }; | |
| 78 | 79 | ||
| 79 | - this.cleartext.pause = function() { | ||
| 80 | + this.cleartext.pause = function () { | ||
| 80 | 81 | debug('paused cleartext'); | |
| 81 | 82 | self._cleartextWriteState = false; | |
| 82 | 83 | }; | |
| 83 | 84 | ||
| 84 | - this.cleartext.resume = function() { | ||
| 85 | + this.cleartext.resume = function () { | ||
| 85 | 86 | debug('resumed cleartext'); | |
| 86 | 87 | self._cleartextWriteState = true; | |
| 87 | 88 | }; | |
| 88 | 89 | ||
| 89 | - this.cleartext.end = function(err) { | ||
| 90 | + this.cleartext.end = function (err) { | ||
| 90 | 91 | debug('cleartext end'); | |
| 91 | 92 | if (!self._done) { | |
| 92 | 93 | self._ssl.shutdown(); | |
@@ -95,24 +96,24 @@ function SecurePair(credentials, isServer) { | |||
| 95 | 96 | self._destroy(err); | |
| 96 | 97 | }; | |
| 97 | 98 | ||
| 98 | - this.encrypted.write = function(data) { | ||
| 99 | + this.encrypted.write = function (data) { | ||
| 99 | 100 | debug('encIn data'); | |
| 100 | 101 | self._encInPending.push(data); | |
| 101 | 102 | self._cycle(); | |
| 102 | 103 | return self._encryptedWriteState; | |
| 103 | 104 | }; | |
| 104 | 105 | ||
| 105 | - this.encrypted.pause = function() { | ||
| 106 | + this.encrypted.pause = function () { | ||
| 106 | 107 | debug('pause encrypted'); | |
| 107 | 108 | self._encryptedWriteState = false; | |
| 108 | 109 | }; | |
| 109 | 110 | ||
| 110 | - this.encrypted.resume = function() { | ||
| 111 | + this.encrypted.resume = function () { | ||
| 111 | 112 | debug('resume encrypted'); | |
| 112 | 113 | self._encryptedWriteState = true; | |
| 113 | 114 | }; | |
| 114 | 115 | ||
| 115 | - this.encrypted.end = function(err) { | ||
| 116 | + this.encrypted.end = function (err) { | ||
| 116 | 117 | debug('encrypted end'); | |
| 117 | 118 | if (!self._done) { | |
| 118 | 119 | self._ssl.shutdown(); | |
@@ -121,7 +122,7 @@ function SecurePair(credentials, isServer) { | |||
| 121 | 122 | self._destroy(err); | |
| 122 | 123 | }; | |
| 123 | 124 | ||
| 124 | - this.cleartext.on('end', function(err) { | ||
| 125 | + this.cleartext.on('end', function (err) { | ||
| 125 | 126 | debug('clearIn end'); | |
| 126 | 127 | if (!self._done) { | |
| 127 | 128 | self._ssl.shutdown(); | |
@@ -130,45 +131,46 @@ function SecurePair(credentials, isServer) { | |||
| 130 | 131 | self._destroy(err); | |
| 131 | 132 | }); | |
| 132 | 133 | ||
| 133 | - this.cleartext.on('close', function() { | ||
| 134 | + this.cleartext.on('close', function () { | ||
| 134 | 135 | debug('source close'); | |
| 135 | 136 | self.emit('close'); | |
| 136 | 137 | self._destroy(); | |
| 137 | 138 | }); | |
| 138 | 139 | ||
| 139 | - this.encrypted.on('end', function() { | ||
| 140 | + this.encrypted.on('end', function () { | ||
| 140 | 141 | if (!self._done) { | |
| 141 | 142 | self._error(new Error('Encrypted stream ended before secure pair was done')); | |
| 142 | 143 | } | |
| 143 | 144 | }); | |
| 144 | 145 | ||
| 145 | - this.encrypted.on('close', function() { | ||
| 146 | + this.encrypted.on('close', function () { | ||
| 146 | 147 | if (!self._done) { | |
| 147 | 148 | self._error(new Error('Encrypted stream closed before secure pair was done')); | |
| 148 | 149 | } | |
| 149 | 150 | }); | |
| 150 | 151 | ||
| 151 | - this.cleartext.on('drain', function() { | ||
| 152 | + this.cleartext.on('drain', function () { | ||
| 152 | 153 | debug('source drain'); | |
| 153 | 154 | self._cycle(); | |
| 154 | 155 | self.encrypted.resume(); | |
| 155 | 156 | }); | |
| 156 | 157 | ||
| 157 | - this.encrypted.on('drain', function() { | ||
| 158 | + this.encrypted.on('drain', function () { | ||
| 158 | 159 | debug('target drain'); | |
| 159 | 160 | self._cycle(); | |
| 160 | 161 | self.cleartext.resume(); | |
| 161 | 162 | }); | |
| 162 | 163 | ||
| 163 | - process.nextTick(function() { | ||
| 164 | + process.nextTick(function () { | ||
| 164 | 165 | self._ssl.start(); | |
| 165 | 166 | self._cycle(); | |
| 166 | 167 | }); | |
| 167 | 168 | } | |
| 168 | 169 | ||
| 169 | 170 | util.inherits(SecurePair, events.EventEmitter); | |
| 170 | 171 | ||
| 171 | - exports.createSecurePair = function(credentials, isServer) { | ||
| 172 | + | ||
| 173 | + exports.createSecurePair = function (credentials, isServer) { | ||
| 172 | 174 | var pair = new SecurePair(credentials, isServer); | |
| 173 | 175 | return pair; | |
| 174 | 176 | }; | |
@@ -183,23 +185,23 @@ exports.createSecurePair = function(credentials, isServer) { | |||
| 183 | 185 | * the ciphers. | |
| 184 | 186 | * | |
| 185 | 187 | * The four pipelines, using terminology of the client (server is just reversed): | |
| 186 | - * 1) Encrypted Output stream (Writing encrypted data to peer) | ||
| 187 | - * 2) Encrypted Input stream (Reading encrypted data from peer) | ||
| 188 | - * 3) Cleartext Output stream (Decrypted content from the peer) | ||
| 189 | - * 4) Cleartext Input stream (Cleartext content to send to the peer) | ||
| 188 | + * (1) Encrypted Output stream (Writing encrypted data to peer) | ||
| 189 | + * (2) Encrypted Input stream (Reading encrypted data from peer) | ||
| 190 | + * (3) Cleartext Output stream (Decrypted content from the peer) | ||
| 191 | + * (4) Cleartext Input stream (Cleartext content to send to the peer) | ||
| 190 | 192 | * | |
| 191 | 193 | * This function attempts to pull any available data out of the Cleartext | |
| 192 | - * input stream (#4), and the Encrypted input stream (#2). Then it pushes | ||
| 193 | - * any data available from the cleartext output stream (#3), and finally | ||
| 194 | - * from the Encrypted output stream (#1) | ||
| 194 | + * input stream (4), and the Encrypted input stream (2). Then it pushes | ||
| 195 | + * any data available from the cleartext output stream (3), and finally | ||
| 196 | + * from the Encrypted output stream (1) | ||
| 195 | 197 | * | |
| 196 | 198 | * It is called whenever we do something with OpenSSL -- post reciving content, | |
| 197 | 199 | * trying to flush, trying to change ciphers, or shutting down the connection. | |
| 198 | 200 | * | |
| 199 | 201 | * Because it is also called everywhere, we also check if the connection | |
| 200 | 202 | * has completed negotiation and emit 'secure' from here if it has. | |
| 201 | 203 | */ | |
| 202 | - SecurePair.prototype._cycle = function() { | ||
| 204 | + SecurePair.prototype._cycle = function () { | ||
| 203 | 205 | if (this._done) { | |
| 204 | 206 | return; | |
| 205 | 207 | } | |
@@ -279,26 +281,26 @@ SecurePair.prototype._cycle = function() { | |||
| 279 | 281 | } | |
| 280 | 282 | ||
| 281 | 283 | mover( | |
| 282 | - function(pool, offset, length) { | ||
| 284 | + function (pool, offset, length) { | ||
| 283 | 285 | debug('reading from clearOut'); | |
| 284 | 286 | return self._ssl.clearOut(pool, offset, length); | |
| 285 | 287 | }, | |
| 286 | - function(chunk) { | ||
| 288 | + function (chunk) { | ||
| 287 | 289 | self.cleartext.emit('data', chunk); | |
| 288 | 290 | }, | |
| 289 | - function(bytesRead) { | ||
| 291 | + function (bytesRead) { | ||
| 290 | 292 | return bytesRead > 0 && self._cleartextWriteState === true; | |
| 291 | 293 | }); | |
| 292 | 294 | ||
| 293 | 295 | mover( | |
| 294 | - function(pool, offset, length) { | ||
| 296 | + function (pool, offset, length) { | ||
| 295 | 297 | debug('reading from encOut'); | |
| 296 | 298 | return self._ssl.encOut(pool, offset, length); | |
| 297 | 299 | }, | |
| 298 | - function(chunk) { | ||
| 300 | + function (chunk) { | ||
| 299 | 301 | self.encrypted.emit('data', chunk); | |
| 300 | 302 | }, | |
| 301 | - function(bytesRead) { | ||
| 303 | + function (bytesRead) { | ||
| 302 | 304 | return bytesRead > 0 && self._encryptedWriteState === true; | |
| 303 | 305 | }); | |
| 304 | 306 | ||
@@ -310,8 +312,8 @@ SecurePair.prototype._cycle = function() { | |||
| 310 | 312 | } | |
| 311 | 313 | }; | |
| 312 | 314 | ||
| 313 | - SecurePair.prototype._destroy = function(err) | ||
| 314 | - { | ||
| 315 | + | ||
| 316 | + SecurePair.prototype._destroy = function (err) { | ||
| 315 | 317 | if (!this._done) { | |
| 316 | 318 | this._done = true; | |
| 317 | 319 | this._ssl.close(); | |
@@ -320,17 +322,17 @@ SecurePair.prototype._destroy = function(err) | |||
| 320 | 322 | } | |
| 321 | 323 | }; | |
| 322 | 324 | ||
| 323 | - SecurePair.prototype._error = function (err) | ||
| 324 | - { | ||
| 325 | + | ||
| 326 | + SecurePair.prototype._error = function (err) { | ||
| 325 | 327 | this.emit('error', err); | |
| 326 | 328 | }; | |
| 327 | 329 | ||
| 328 | - SecurePair.prototype.getPeerCertificate = function (err) | ||
| 329 | - { | ||
| 330 | + | ||
| 331 | + SecurePair.prototype.getPeerCertificate = function (err) { | ||
| 330 | 332 | return this._ssl.getPeerCertificate(); | |
| 331 | 333 | }; | |
| 332 | 334 | ||
| 333 | - SecurePair.prototype.getCipher = function (err) | ||
| 334 | - { | ||
| 335 | + | ||
| 336 | + SecurePair.prototype.getCipher = function (err) { | ||
| 335 | 337 | return this._ssl.getCurrentCipher(); | |
| 336 | 338 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments