#!/usr/bin/env ruby # Usage: ./torloop # # Runs a command over and over again with # a new tor identity each time. # # The command should use the tor network # under socks5 proxy on port 9050. # # If you want tor output, `tail -f tmp/out` # Kill process on 9050 port port = `lsof -i :9050 | tail -1 | awk '{print $2}'`.chomp system("kill -9 #{port}") unless port.empty? def start_tor spawn("tor", out: "tmp/out", err: "tmp/err") end def new_identity(pid) system("kill -HUP #{pid}") end def show_ip `curl --silent --socks5 localhost:9050 api.ipify.org`.chomp end tor_pid = start_tor at_exit { Process.kill "KILL", tor_pid } at_exit { FileUtils.rm_rf("tmp") } sleep 1 until File.read("tmp/out").match? /Bootstrapped 100% \(done\): Done/ ip = show_ip loop do puts "Current IP: #{ip}" puts "Running command: #{ARGV.join(" ")}" system(*ARGV) new_identity(tor_pid) sleep 1 until ip != (ip = show_ip) end