From c37fe0898cff464afb385b542fbd3432e63d3c17 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Mar 2014 21:02:14 +0100 Subject: [PATCH 1/2] https://trac.torproject.org/projects/tor/ticket/11283 --- package.json | 2 +- src/js/helpers/formatter.js | 26 ++++++++++++++++++++---- src/js/helpers/handlebarsHelper.js | 6 ++++++ src/js/helpers/util.js | 23 +++++++++++++++++++++ src/js/templates/bridgeDetail.handlebars | 2 +- test/karma.conf.js | 2 +- test/unit/formatter.test.js | 11 +++++++++- test/unit/util.test.js | 20 ++++++++++++++++++ 8 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 test/unit/util.test.js diff --git a/package.json b/package.json index 1d96f3a..656c5a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "globe", - "version": "0.4.5", + "version": "0.4.6-11283", "private": true, "scripts": { "start": "node app.js" diff --git a/src/js/helpers/formatter.js b/src/js/helpers/formatter.js index bc94263..0387532 100644 --- a/src/js/helpers/formatter.js +++ b/src/js/helpers/formatter.js @@ -100,7 +100,7 @@ GLOBE.Formatter = { }, /** - * Extracts port from a given string + * Extracts a port from a given string by returning the value after the last ':' * @param {String} value complete host + port * @returns {String} port or empty string if no port found * @example @@ -111,9 +111,12 @@ GLOBE.Formatter = { var port = GLOBE.static.messages.dataEmpty; if(typeof value === 'string'){ - var parts = value.split(':'); - if(parts.length === 2 && parts[1].length){ - port = parts[1]; + var parts = value.split(':'), + part; + + if (parts.length && parts.length > 1 && + (part = parts[parts.length - 1]).length) { + port = part; } } @@ -136,5 +139,20 @@ GLOBE.Formatter = { fingerprint = val.slice(1); } return fingerprint; + }, + + /** + * Returns a string that contains the ip version and port + * @param {String} val + * @return {String} ip version and port + * @example + * // returns 'IPv4:9000' + * Globe.Formatter.anonymizeIpAddress('128.0.0.1:9000') + */ + anonymizeIpAddress: function(val) { + var ipV = GLOBE.Util.looksLikeIpV(val), + port = GLOBE.Formatter.extractPort(val); + + return 'IPv' + ipV + ':' + port; } }; \ No newline at end of file diff --git a/src/js/helpers/handlebarsHelper.js b/src/js/helpers/handlebarsHelper.js index 50056b8..649a591 100644 --- a/src/js/helpers/handlebarsHelper.js +++ b/src/js/helpers/handlebarsHelper.js @@ -116,4 +116,10 @@ Em.Handlebars.helper('fromNow', function(value){ */ Em.Handlebars.helper('familyToFingerprint', function(value){ return new Handlebars.SafeString(GLOBE.Formatter.familyToFingerprint(value)); +}); +/** + * @see {@link GLOBE.Formatter.familyToFingerprint()} + */ +Em.Handlebars.helper('anonIpAdress', function(value){ + return new Handlebars.SafeString(GLOBE.Formatter.anonymizeIpAddress(value)); }); \ No newline at end of file diff --git a/src/js/helpers/util.js b/src/js/helpers/util.js index ba1d9ce..0f5500b 100644 --- a/src/js/helpers/util.js +++ b/src/js/helpers/util.js @@ -193,5 +193,28 @@ GLOBE.Util = { } } return periods; + }, + + /** + * Function that takes a ip address and decides if it could be a ipv6 or ipv4 address. + * Do not use this as validation for ip addresses. + * @param {String} address + * @return {undefined|String} 6, 4 or undefined (if address is no string). + */ + looksLikeIpV: function(address) { + var looksLike, + v6Result, + v4Result; + + if (typeof address === 'string') { + // I used an assignment with boolean check because .match can return null + if ((v6Result = address.match(/:/g)) && v6Result.length > 1) { + looksLike = '6'; + } else if ((v4Result = address.match(/\./g)) && v4Result.length === 3) { + looksLike = '4'; + } + } + + return looksLike; } }; \ No newline at end of file diff --git a/src/js/templates/bridgeDetail.handlebars b/src/js/templates/bridgeDetail.handlebars index dd5d435..73d95b3 100644 --- a/src/js/templates/bridgeDetail.handlebars +++ b/src/js/templates/bridgeDetail.handlebars @@ -51,7 +51,7 @@
OR Addresses