-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net-address: Add get group function.
This commit adds the getGroup function to net addresses. It allows for network addresses to be grouped into buckets, such that we can limit the number of outgoing connections per group to a certain amount. This will allow for increased connection diversity and reduced attack surface area.
- Loading branch information
Showing
2 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* eslint-env mocha */ | ||
/* eslint prefer-arrow-callback: "off" */ | ||
|
||
'use strict'; | ||
|
||
const assert = require('bsert'); | ||
const NetAddress = require('../lib/net/netaddress'); | ||
|
||
describe('NetAddress', function() { | ||
it('should return the correct group', () => { | ||
// Local -> !Routable() | ||
assert.bufferEqual( | ||
NetAddress.fromHost('127.0.0.1', 13038, null, 'testnet').getGroup(), | ||
Buffer.from([0]) | ||
); | ||
|
||
// RFC1918 -> !Routable() | ||
assert.bufferEqual( | ||
NetAddress.fromHost('169.254.1.1', 13038, null, 'testnet').getGroup(), | ||
Buffer.from([0]) | ||
); | ||
|
||
// IPv4 | ||
assert.bufferEqual( | ||
NetAddress.fromHost('1.2.3.4', 13038, null, 'testnet').getGroup(), | ||
Buffer.from([1, 1, 2]) | ||
); | ||
|
||
// RFC6145 | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'::FFFF:0:102:304', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([1, 1, 2]) | ||
); | ||
|
||
// RFC6052 | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'64:FF9B::102:304', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([1, 1, 2]) | ||
); | ||
|
||
// RFC3964 | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'2002:102:304:9999:9999:9999:9999:9999', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([1, 1, 2]) | ||
); | ||
|
||
// RFC4380 | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'2001:0:9999:9999:9999:9999:FEFD:FCFB', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([1, 1, 2]) | ||
); | ||
|
||
// Tor | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'FD87:D87E:EB43:edb1:8e4:3588:e546:35ca', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([3, 239]) | ||
); | ||
|
||
// he.net | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'2001:470:abcd:9999:9999:9999:9999:9999', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([2, 32, 1, 4, 112, 175]) | ||
); | ||
|
||
// IPv6 | ||
assert.bufferEqual( | ||
NetAddress.fromHost( | ||
'2001:2001:9999:9999:9999:9999:9999:9999', | ||
13038, | ||
null, | ||
'testnet' | ||
).getGroup(), | ||
Buffer.from([2, 32, 1, 32, 1]) | ||
); | ||
}); | ||
}); |