Skip to content

Commit

Permalink
merged #trac-11283
Browse files Browse the repository at this point in the history
  • Loading branch information
makepanic committed Apr 4, 2014
2 parents b0a3a62 + 8a152a4 commit 449ceb0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
26 changes: 22 additions & 4 deletions src/js/helpers/formatter.js
Expand Up @@ -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
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
};
6 changes: 6 additions & 0 deletions src/js/helpers/handlebarsHelper.js
Expand Up @@ -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));
});
23 changes: 23 additions & 0 deletions src/js/helpers/util.js
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/js/templates/bridgeDetail.hbs
Expand Up @@ -63,7 +63,7 @@
title="Onion-routing addresses">OR Addresses</h5>
<ul class="property-content item-list">
{{#each or_addresses}}
<li>{{ this }}</li>
<li>{{ anonIpAdress this }}</li>
{{else}}
<li>none</li>
{{/each}}
Expand Down
12 changes: 11 additions & 1 deletion test/unit/formatter.test.js
Expand Up @@ -59,8 +59,8 @@ test('port extractor formatter test', function(){
equal(testFn(NaN), dataEmpty, 'test for NaN');
equal(testFn('string'), dataEmpty, 'test for "string"');
equal(testFn('0.0.0.0:'), dataEmpty, 'test for "0.0.0.0:"');
equal(testFn('0.0.0.0:80:80'), dataEmpty, 'test for "0.0.0.0:80:80"');

equal(testFn('0.0.0.0:80:80'), '80', 'test for "0.0.0.0:80:80"');
equal(testFn('0.0.0.0:80'), '80', 'test for "0.0.0.0:80"');
equal(testFn('0.0.0.0:9000'), '9000', 'test for "0.0.0.0:9000"');

Expand Down Expand Up @@ -127,7 +127,17 @@ test('family to fingerprint formatter test', function(){
equal(testFn('string'), dataEmpty, 'test for "string"');

equal(testFn('$1234'), '1234', 'test for "$1234"');
});

test('anonymizeIpAddress formatter test', function(){

var testFn = GLOBE.Formatter.anonymizeIpAddress;

equal(testFn('128.0.0.1:9000'), 'IPv4:9000', 'test for ipv4');
equal(testFn('128.0.0.1:80'), 'IPv4:80', 'test for ipv4');
equal(testFn('::ffff:10.0.0.1:9000'), 'IPv6:9000', 'test for ipv6');
equal(testFn('1:2:3:4:5:6:7:8:80'), 'IPv6:80', 'test for ipv6');
equal(testFn('[2001:bc8:3431:101::2]:22'), 'IPv6:22', 'test for ipv6');
});


Expand Down
20 changes: 20 additions & 0 deletions test/unit/util.test.js
@@ -0,0 +1,20 @@

// test module
module('util tests module');

test('looksLikeIpV function test', function(){

var testFn = GLOBE.Util.looksLikeIpV;

equal(testFn('1:2:3:4:5:6:7:8'), '6', 'test for 6');
equal(testFn('::ffff:10.0.0.1'), '6', 'test for 6');
equal(testFn('::ffff:1.2.3.4'), '6', 'test for 6');
equal(testFn('1:2:3:4:5:6:77:88'), '6', 'test for 6');
equal(testFn('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), '6', 'test for 6');

equal(testFn('127.0.0.1'), '4', 'test for 4');
equal(testFn('192.168.1.1'), '4', 'test for 4');
equal(testFn('255.255.255.255'), '4', 'test for 4');
equal(testFn('0.0.0.0'), '4', 'test for 4');
});

0 comments on commit 449ceb0

Please sign in to comment.