TorAutoVisitor/TorAutoVisitor.py

55 lines
2.3 KiB
Python
Raw Normal View History

2019-04-19 12:23:06 +03:00
#!/usr/bin/python3
import subprocess
import signal
from threading import Event
import argparse
import random
import psutil
import logging
import math
import time
import os
executable = "~/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/start-tor-browser" # Default install directory for torbrowser-launcher
exit = Event()
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(math.floor(n/10)%10!=1)*(n%10<4)*n%10::4])
def main():
logging.info(f'Directing traffic to {args.url}...')
if not args.min and not args.max:
min_time = max_time = 15
logging.warn(f'No visit time set, defaulting to {min_time} seconds!')
elif not args.min or not args.max:
min_time = max_time = args.min if args.min else args.max
logging.info(f'Using identical minimum and maximum visit times. ({min_time} seconds)')
else:
min_time = args.min
max_time = args.max
counter = 1
sleep_time = min_time
while not exit.is_set():
if min_time != max_time:
sleep_time = random.randrange(min_time, max_time)
process = subprocess.Popen([os.path.expanduser(executable), args.url, "-headless"])
logging.info(f'Visiting the site for the {ordinal(counter)} time for {round(sleep_time)} seconds. (PID: {process.pid})')
exit.wait(sleep_time)
parent = psutil.Process(process.pid)
for child in parent.children(recursive=True): # or parent.children() for recursive=False
child.kill()
parent.kill()
counter += 1
def signal_handler(sig, frame):
logging.warning("SIGINT caught, shutting down Tor...")
exit.set()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("url", type=str, help="URL to be loaded on each visit")
parser.add_argument("--min", type=int, help="Minimum visit time")
parser.add_argument("--max", type=int, help="Maximum visit time")
args = parser.parse_args()
logging.basicConfig(format="%(asctime)-15s %(message)s", datefmt='%H:%M:%S', level=logging.INFO)
signal.signal(signal.SIGINT, signal_handler)
main()