Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Created June 24, 2024 14:19
Show Gist options
  • Save BuonOmo/a76351b504f8036dc4c73f5e6c076b7e to your computer and use it in GitHub Desktop.
Save BuonOmo/a76351b504f8036dc4c73f5e6c076b7e to your computer and use it in GitHub Desktop.
Playing with tor in command lines
#!/usr/bin/env ruby
# Usage: ./torloop <command>
#
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment