add ip v6
This commit is contained in:
@@ -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) {
|
(async function main(args) {
|
||||||
try {
|
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 logger = new Logger();
|
||||||
const home = process.env.HOME || process.env.USERPROFILE;
|
const home = process.env.HOME || process.env.USERPROFILE;
|
||||||
const app_cache_path = path.join(home, '.cache', 'mini-tor-js');
|
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({
|
const routers = consensus.get_onion_routers_by_criteria({
|
||||||
flags: OR.valid | OR.exit
|
flags: OR.valid | OR.exit
|
||||||
});
|
});
|
||||||
|
const ips_v4 = new Set();
|
||||||
|
const ips_v6 = new Set();
|
||||||
for (const router of routers) {
|
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);
|
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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
@@ -257,6 +257,11 @@ class Consensus {
|
|||||||
}
|
}
|
||||||
current_router.flags = flags;
|
current_router.flags = flags;
|
||||||
break;
|
break;
|
||||||
|
case 'a':
|
||||||
|
const r = /\[([^\[]+)]:(\d+)$/;
|
||||||
|
const m = r.exec(parts[1]);
|
||||||
|
current_router.ip_v6 = m[1];
|
||||||
|
break;
|
||||||
case 'directory-footer':
|
case 'directory-footer':
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ class OnionRouter {
|
|||||||
_consensus = null;
|
_consensus = null;
|
||||||
_nickname = '';
|
_nickname = '';
|
||||||
_ip = '';
|
_ip = '';
|
||||||
|
_ip_v6 = '';
|
||||||
_or_port = 0;
|
_or_port = 0;
|
||||||
_dir_port = 0;
|
_dir_port = 0;
|
||||||
/** @type Buffer */
|
/** @type Buffer */
|
||||||
@@ -42,6 +43,8 @@ class OnionRouter {
|
|||||||
get consensus() { return this._consensus; }
|
get consensus() { return this._consensus; }
|
||||||
get name() { return this._nickname; }
|
get name() { return this._nickname; }
|
||||||
get ip() { return this._ip; }
|
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 flags() { return this._flags; }
|
||||||
get or_port() { return this._or_port; }
|
get or_port() { return this._or_port; }
|
||||||
get dir_port() { return this._dir_port; }
|
get dir_port() { return this._dir_port; }
|
||||||
|
Reference in New Issue
Block a user