add ip v6

This commit is contained in:
2025-09-30 20:01:51 -04:00
parent 4d6497c196
commit dd4b2fbe62
3 changed files with 37 additions and 2 deletions

View File

@@ -39,9 +39,19 @@ class Logger {
}
}
function ip4ToInt(ipAddress) {
const parts = ipAddress.split('.');
if (parts.length !== 4) {
throw new Error('Invalid IP address');
}
const intIp = (+parts[0] << 24) + (+parts[1] << 16) + (+parts[2] << 8) + (+parts[3]);
return intIp >>> 0; // Ensure unsigned 32-bit integer
}
(async function main(args) {
try {
const ip_only = args[0] === '--ip-only';
const ip4_only = args[0] === '--ip4-only';
const ip6_only = args[0] === '--ip6-only';
const logger = new Logger();
const home = process.env.HOME || process.env.USERPROFILE;
const app_cache_path = path.join(home, '.cache', 'mini-tor-js');
@@ -55,10 +65,27 @@ class Logger {
const routers = consensus.get_onion_routers_by_criteria({
flags: OR.valid | OR.exit
});
const ips_v4 = new Set();
const ips_v6 = new Set();
for (const router of routers) {
if (ip_only) console.log(router.ip);
if (ip4_only) ips_v4.add(router.ip)
else if (ip6_only) {
router.ip_v6 && ips_v6.add(router.ip_v6);
}
else console.log(router.name, router.ip, router.dir_port, router.or_port);
}
if (ip4_only) {
const ips = [...ips_v4].sort((a, b) => ip4ToInt(a) - ip4ToInt(b));
for (const ip of ips) {
console.log(ip);
}
}
if (ip6_only) {
const ips = [...ips_v6].sort((a, b) => a < b ? -1 : 1);
for (const ip of ips) {
console.log(ip);
}
}
} catch (error) {
console.error(error);
}

View File

@@ -257,6 +257,11 @@ class Consensus {
}
current_router.flags = flags;
break;
case 'a':
const r = /\[([^\[]+)]:(\d+)$/;
const m = r.exec(parts[1]);
current_router.ip_v6 = m[1];
break;
case 'directory-footer':
return;
}

View File

@@ -18,6 +18,7 @@ class OnionRouter {
_consensus = null;
_nickname = '';
_ip = '';
_ip_v6 = '';
_or_port = 0;
_dir_port = 0;
/** @type Buffer */
@@ -42,6 +43,8 @@ class OnionRouter {
get consensus() { return this._consensus; }
get name() { return this._nickname; }
get ip() { return this._ip; }
get ip_v6() { return this._ip_v6; }
set ip_v6(value) { this._ip_v6 = value; }
get flags() { return this._flags; }
get or_port() { return this._or_port; }
get dir_port() { return this._dir_port; }