Skip to content

Instantly share code, notes, and snippets.

@UlisesGascon
Created August 13, 2019 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UlisesGascon/a07847fd6bff2625e9b6ca6bf03fe708 to your computer and use it in GitHub Desktop.
Save UlisesGascon/a07847fd6bff2625e9b6ca6bf03fe708 to your computer and use it in GitHub Desktop.
Identificar trafico de tor
const http = require('http');
const {isTor, onReady } = require('./tor-data')
console.log("Data confirmation...")
onReady()
http.createServer((req, res) => {
let msg = '<h1>😥 Hello Internet User!</h1>'
if(isTor(req)) {
msg = '<h1>👹 Hello Onion User!</h1>'
}
res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
res.write(msg);
}).listen(3000);
const got = require('got');
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
db.defaults({
tor: {
exit_nodes: [],
raw: {}
}
})
.write();
const allData = () => {
return db.get('tor.raw').value()
}
const allExitNodes = () => {
return db.get('tor.exit_nodes').value()
}
const isExitNode = (ip) => {
const relays = db.get('tor.exit_nodes').value();
return relays.includes(ip)
}
const onReady = async () => {
const data = db.get('tor.raw').value()
if(!data.relays){
await updateList()
}
return true;
}
const updateList = async () => {
try {
const response = await got('https://onionoo.torproject.org/details');
const rawData = JSON.parse(response.body);
db.set('tor.raw', rawData).write()
db.set('tor.exit_nodes', rawData.relays.filter(relay => relay.flags.includes("Exit") && relay.exit_addresses).map(relay => relay.exit_addresses).flat()).write()
} catch (error) {
console.log(error.response.body);
throw error;
}
}
function getUserIp (req) {
let ip = req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
}
return ip
}
function isTor (req){
const ip = getUserIp(req)
return isExitNode(ip)
}
setInterval(() => {
console.log("[TOR][LIST] Time to update!")
updateList();
}, 600000)
module.exports = {getUserIp, isTor, allData, allExitNodes, isExitNode, onReady }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment