Skip to content

Commit

Permalink
Merge PR #779 from 'pinheadmz/public-ipv6'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 29, 2022
2 parents 9cf8cb8 + c4284d6 commit 1326351
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
16 changes: 6 additions & 10 deletions lib/net/hostlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,9 @@ class HostList {

if (!src) {
for (const dest of this.local.values()) {
if (this.network.type === 'main') {
// Disable everything else for now.
if (dest.type < HostList.scores.UPNP)
continue;
}
// Disable everything except MANUAL
if (dest.type < HostList.scores.UPNP)
continue;

if (dest.addr.hasKey())
continue;
Expand All @@ -1052,11 +1050,9 @@ class HostList {
}

for (const dest of this.local.values()) {
if (this.network.type === 'main') {
// Disable everything else for now.
if (dest.type < HostList.scores.UPNP)
continue;
}
// Disable everything except MANUAL
if (dest.type < HostList.scores.UPNP)
continue;

if (dest.addr.hasKey())
continue;
Expand Down
27 changes: 13 additions & 14 deletions test/net-hostlist-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,14 @@ describe('Net HostList', function() {
});

// w/o src it will only take into account the score.
it('should get local w/o src', () => {
it('should only get local w/o src if MANUAL', () => {
const services = 1000;
const port = regtest.port;

const addrs = [];

// w/o key
for (let i = HostList.scores.NONE; i < HostList.scores.MAX - 1; i++) {
for (let i = HostList.scores.NONE; i < HostList.scores.MAX; i++) {
const address = new NetAddress({
host: getRandomIPv4(),
port: port,
Expand All @@ -563,16 +563,16 @@ describe('Net HostList', function() {
key: Buffer.alloc(33, 1)
}), HostList.scores.MAX]);

const max = addrs[HostList.scores.MAX - 2];
const manual = addrs[HostList.scores.MANUAL];

const hosts = getHostsFromLocals(addrs);
const local = hosts.getLocal();

assert.strictEqual(local, max[0]);
assert.strictEqual(local, manual[0]);
});

it('should get local (score)', () => {
// NOTE: main network ignores everything other than MANUAL type.
// NOTE: We ignore everything other than MANUAL type.
// - scores.IF - addresses from the network interfaces.
// - scores.BIND - Listening IP. (publicHost/publicPort is MANUAL)
// - scores.DNS - Domain that needs to be resolved.
Expand All @@ -595,12 +595,11 @@ describe('Net HostList', function() {
});

// testnet/regtest
for (let type = scores.NONE; type < scores.MAX; type++) {
const addrs = naddrsByScore.slice(scores.NONE, type + 1);
const hosts = getHostsFromLocals(addrs, { network: regtest });
{
const hosts = getHostsFromLocals(naddrsByScore, { network: regtest });
const src = getRandomNetAddr();
const best = hosts.getLocal(src);
assert.strictEqual(best, naddrsByScore[type][0]);
assert.strictEqual(best, naddrsByScore[scores.MANUAL][0]);
}

// mainnet
Expand All @@ -612,7 +611,7 @@ describe('Net HostList', function() {
}

{
// everything below MANUAL is skipped on main network.
// everything below MANUAL is skipped.
const addrs = naddrsByScore.slice(scores.NONE, scores.UPNP);
const hosts = getHostsFromLocals(addrs, { network: mainnet });
const src = getRandomNetAddr(mainnet);
Expand Down Expand Up @@ -716,8 +715,8 @@ describe('Net HostList', function() {
const {scores} = HostList;

const rawDests = [
[getRandomIPv4(), scores.IF],
[getRandomIPv4(), scores.BIND]
[getRandomIPv4(), scores.UPNP],
[getRandomIPv4(), scores.MANUAL]
];

const dests = rawDests.map(([h, s]) => {
Expand All @@ -734,14 +733,14 @@ describe('Net HostList', function() {
}

{
// we should get BIND, because BIND > IF
// we should get MANUAL only
const addr = getRandomNetAddr();
const local = hosts.getLocal(addr);
assert.strictEqual(local, dests[1][0]);
}

{
// with markLocal IF should get the same score (type remains).
// with markLocal UPNP should get the same score (type remains).
hosts.markLocal(dests[0][0]);
const addr = getRandomNetAddr();
const local = hosts.getLocal(addr);
Expand Down

0 comments on commit 1326351

Please sign in to comment.