diff --git a/src/js/helpers/formatter.js b/src/js/helpers/formatter.js index 17d58c0..db4155a 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; } } @@ -145,5 +148,20 @@ GLOBE.Formatter = { fixed = (val * 100).toFixed(precision) + '%'; } return fixed; + }, + + /** + * 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 40b2a9b..15b7054 100644 --- a/src/js/helpers/handlebarsHelper.js +++ b/src/js/helpers/handlebarsHelper.js @@ -122,4 +122,10 @@ Em.Handlebars.helper('familyToFingerprint', function(value){ */ Em.Handlebars.helper('percent', function(value, precision){ return new Handlebars.SafeString(GLOBE.Formatter.percent(value, precision)); +}); +/** + * @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 901607a..e2247f3 100644 --- a/src/js/helpers/util.js +++ b/src/js/helpers/util.js @@ -197,6 +197,29 @@ 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; + }, + processHistoryResponse: function(fieldMapping, response){ var hasRelays = response && response.relays && response.relays.length, hasBridges = response && response.bridges && response.bridges.length, diff --git a/src/js/templates/bridgeDetail.hbs b/src/js/templates/bridgeDetail.hbs index 57983eb..8c9b20e 100644 --- a/src/js/templates/bridgeDetail.hbs +++ b/src/js/templates/bridgeDetail.hbs @@ -63,7 +63,7 @@ title="Onion-routing addresses">OR Addresses