add get-exit-nodes script
This commit is contained in:
		
							
								
								
									
										65
									
								
								src/get-exit-nodes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/get-exit-nodes.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
const fs = require('fs');
 | 
			
		||||
const path = require('path');
 | 
			
		||||
const util = require('util');
 | 
			
		||||
const Consensus = require('./tor/Consensus');
 | 
			
		||||
const OR = require('./tor/OnionRouter');
 | 
			
		||||
 | 
			
		||||
const LogLevels = {
 | 
			
		||||
  off: 0,
 | 
			
		||||
  error: 1,
 | 
			
		||||
  warn: 2,
 | 
			
		||||
  info: 3,
 | 
			
		||||
  debug: 4
 | 
			
		||||
}
 | 
			
		||||
let logLevel = LogLevels.error;
 | 
			
		||||
function timestamp() {
 | 
			
		||||
  const d = new Date().toISOString();
 | 
			
		||||
  return [d.slice(5, 5), d.slice(11, 8)].join(' ')
 | 
			
		||||
}
 | 
			
		||||
class Logger {
 | 
			
		||||
  debug(...args) {
 | 
			
		||||
    if (LogLevels.debug > logLevel) return;
 | 
			
		||||
    const [format, ...rest] = args;
 | 
			
		||||
    console.error(timestamp(), "D", util.format(format, ...rest));
 | 
			
		||||
  }
 | 
			
		||||
  info(...args) {
 | 
			
		||||
    if (LogLevels.info > logLevel) return;
 | 
			
		||||
    const [format, ...rest] = args;
 | 
			
		||||
    console.error(timestamp(), "I", util.format(format, ...rest));
 | 
			
		||||
  }
 | 
			
		||||
  warn(...args) {
 | 
			
		||||
    if (LogLevels.warn > logLevel) return;
 | 
			
		||||
    const [format, ...rest] = args;
 | 
			
		||||
    console.error(timestamp(), "W", util.format(format, ...rest));
 | 
			
		||||
  }
 | 
			
		||||
  error(...args) {
 | 
			
		||||
    if (LogLevels.error > logLevel) return;
 | 
			
		||||
    const [format, ...rest] = args;
 | 
			
		||||
    console.error(timestamp(), "E", util.format(format, ...rest));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
(async function main(args) {
 | 
			
		||||
  try {
 | 
			
		||||
    const ip_only = args[0] === '--ip-only';
 | 
			
		||||
    const logger = new Logger();
 | 
			
		||||
    const home = process.env.HOME || process.env.USERPROFILE;
 | 
			
		||||
    const app_cache_path = path.join(home, '.cache', 'mini-tor-js');
 | 
			
		||||
    const cached_consensus_path = path.join(app_cache_path, 'cached-consensus')
 | 
			
		||||
    fs.mkdirSync(app_cache_path, {recursive: true});
 | 
			
		||||
    // Load consensus
 | 
			
		||||
    const consensus = new Consensus(logger);
 | 
			
		||||
    await consensus.fetch(cached_consensus_path);
 | 
			
		||||
    consensus.set_allowed_dir_ports(80, 443);
 | 
			
		||||
 | 
			
		||||
    const routers = consensus.get_onion_routers_by_criteria({
 | 
			
		||||
      flags: OR.valid | OR.exit
 | 
			
		||||
    });
 | 
			
		||||
    for (const router of routers) {
 | 
			
		||||
      if (ip_only) console.log(router.ip);
 | 
			
		||||
      else console.log(router.name, router.ip, router.dir_port, router.or_port);
 | 
			
		||||
    }
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    console.error(error);
 | 
			
		||||
  }
 | 
			
		||||
})(process.argv.slice(2))
 | 
			
		||||
		Reference in New Issue
	
	Block a user