From 4407b398a4a74325bd21d21e4bd09f60af896b1b Mon Sep 17 00:00:00 2001 From: Minko Gechev Date: Sat, 1 Jun 2013 15:59:19 +0300 Subject: [PATCH 1/2] Collecting the cookies information in a loop --- lib/auth.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 1186358..aea7ad1 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -22,29 +22,24 @@ function parseXauth( buf ) 0: 'Internet', 1: 'DECnet', 2: 'Chaos', - 5: 'ServerInterpreted', + 5: 'ServerInterpreted', 6: 'InternetV6' }; + var cookieFields = [ + 'address', + 'display', + 'authName', + 'authData' + ]; cookie.type = buf.unpack('n')[0]; offset += 2; - // TODO: rewrite following using loop (16bits length + string data) - var addressLen = buf.unpack('n', offset)[0]; - offset += 2; - cookie.address = buf.unpackString(addressLen, offset); - offset += addressLen; - var displayNumLen = buf.unpack('n', offset)[0]; - offset += 2; - cookie.display = buf.unpackString(displayNumLen, offset); - offset += displayNumLen; - var authNameLen = buf.unpack('n', offset)[0]; - offset += 2; - cookie.authName = buf.unpackString(authNameLen, offset); - offset += authNameLen; - var authDataLen = buf.unpack('n', offset)[0]; - offset += 2; - cookie.authData = buf.unpackString(authDataLen, offset); - offset += authDataLen; - auth.push(cookie); + for (var i = 0; i < 4; i += 1) { + var length = buf.unpack('n', offset)[0]; + offset += 2; + cookie[cookieFields[i]] = buf.unpackString(length, offset); + offset += length; + } + auth.push(cookie); } return auth; } @@ -92,4 +87,4 @@ module.exports = function( display, host, cb ) // throw 'No auth cookie matching display=' + display + ' and host=' + host; cb( '', '' ); }); -} \ No newline at end of file +}; From 9b0457817a594232a21cd359e361a976607031d8 Mon Sep 17 00:00:00 2001 From: Minko Gechev Date: Sun, 2 Jun 2013 01:23:33 +0300 Subject: [PATCH 2/2] Byte order specified --- lib/auth.js | 25 +++++++++++++------------ lib/handshake.js | 11 ++++++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index aea7ad1..e62e910 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -9,6 +9,14 @@ function parseXauth( buf ) { var offset = 0; var auth = []; + var cookieProperties = ['address', 'display', 'authName', 'authData']; + + function handleCookieProperty(property) { + var length = buf.unpack('n', offset)[0]; + offset += 2; + cookie[property] = buf.unpackString(length, offset); + offset += length; + } while (offset < buf.length) { @@ -25,20 +33,13 @@ function parseXauth( buf ) 5: 'ServerInterpreted', 6: 'InternetV6' }; - var cookieFields = [ - 'address', - 'display', - 'authName', - 'authData' - ]; cookie.type = buf.unpack('n')[0]; - offset += 2; - for (var i = 0; i < 4; i += 1) { - var length = buf.unpack('n', offset)[0]; - offset += 2; - cookie[cookieFields[i]] = buf.unpackString(length, offset); - offset += length; + if (!typeToName[cookie.type]) { + console.warn('Unknown address type'); } + offset += 2; + //JSHint becomes angry when handleCookieProperty is declared inside loop + cookieProperties.forEach(handleCookieProperty); auth.push(cookie); } return auth; diff --git a/lib/handshake.js b/lib/handshake.js index a2f717f..e791cae 100644 --- a/lib/handshake.js +++ b/lib/handshake.js @@ -180,10 +180,19 @@ bl.unpack('C', function(res) { } +function getByteOrder() { + var isLittleEndian = ((new Uint32Array((new Uint8Array([1,2,3,4])).buffer))[0] === 0x04030201); + if (isLittleEndian) { + return 'l'.charCodeAt(0); + } else { + return 'B'.charCodeAt(0); + } +} + function writeClientHello(stream, displayNum, authHost) { getAuthString( displayNum, authHost, function( authType, authData ) { - var byte_order = 'l'.charCodeAt(0); // TODO: byteorder!!! + var byte_order = getByteOrder(); var protocol_major = 11; // TODO: config? env? var protocol_minor = 0; stream.pack(