diff --git a/toriptables2.py b/toriptables2.py index 9c27bef..312ba86 100755 --- a/toriptables2.py +++ b/toriptables2.py @@ -12,6 +12,7 @@ from commands import getoutput from subprocess import call, check_call, CalledProcessError from os.path import isfile, basename from os import devnull +import os from sys import stdout, stderr from atexit import register from argparse import ArgumentParser @@ -39,7 +40,7 @@ AutomapHostsOnResolve 1 TransPort %s DNSPort %s ''' % (basename(__file__), self.trans_port, self.virtual_net, - self.trans_port, self.local_dnsport) + self.trans_port, self.local_dnsport) def flush_iptables_rules(self): call(["iptables", "-F"]) @@ -58,27 +59,7 @@ DNSPort %s stdout=fnull, stderr=fnull) if tor_restart is 0: - print(" {0}".format( - "[\033[92m+\033[0m] Anonymizer status \033[92m[ON]\033[0m")) - print(" {0}".format( - "[\033[92m*\033[0m] Getting public IP, please wait...")) - retries = 0 - my_public_ip = None - while retries < 12 and not my_public_ip: - retries += 1 - try: - my_public_ip = load(urlopen('https://check.torproject.org/api/ip'))['IP'] - except URLError: - sleep(5) - print(" [\033[93m?\033[0m] Still waiting for IP address...") - except ValueError: - break - print - if not my_public_ip: - my_public_ip = getoutput('wget -qO - ifconfig.me') - if not my_public_ip: - exit(" \033[91m[!]\033[0m Can't get public ip address!") - print(" {0}".format("[\033[92m+\033[0m] Your IP is \033[92m%s\033[0m" % my_public_ip)) + self.get_ip() except CalledProcessError as err: print("\033[91m[!] Command failed: %s\033[0m" % ' '.join(err.cmd)) @@ -113,7 +94,28 @@ DNSPort %s self.tor_uid, "-j", "ACCEPT"]) call(["iptables", "-A", "OUTPUT", "-j", "REJECT"]) - + def get_ip(self): + print(" {0}".format( + "[\033[92m+\033[0m] Anonymizer status \033[92m[ON]\033[0m")) + print(" {0}".format( + "[\033[92m*\033[0m] Getting public IP, please wait...")) + retries = 0 + my_public_ip = None + while retries < 12 and not my_public_ip: + retries += 1 + try: + my_public_ip = load(urlopen('https://check.torproject.org/api/ip'))['IP'] + except URLError: + sleep(5) + print(" [\033[93m?\033[0m] Still waiting for IP address...") + except ValueError: + break + print + if not my_public_ip: + my_public_ip = getoutput('wget -qO - ifconfig.me') + if not my_public_ip: + exit(" \033[91m[!]\033[0m Can't get public ip address!") + print(" {0}".format("[\033[92m+\033[0m] Your IP is \033[92m%s\033[0m" % my_public_ip)) if __name__ == '__main__': parser = ArgumentParser( description= @@ -126,6 +128,14 @@ if __name__ == '__main__': '--flush', action='store_true', help='This option flushes the iptables rules to default') + parser.add_argument('-r', + '--refresh', + action='store_true', + help='This option will change the circuit and gives another IP address') + parser.add_argument('-i', + '--ip', + action='store_true', + help='This option will output the current public IP address') args = parser.parse_args() try: @@ -141,6 +151,11 @@ if __name__ == '__main__': load_tables.flush_iptables_rules() print(" {0}".format( "[\033[93m!\033[0m] Anonymizer status \033[91m[OFF]\033[0m")) + elif args.ip: + load_tables.get_ip() + elif args.refresh: + os.system("kill -HUP $(pidof tor)") + load_tables.get_ip() else: parser.print_help() except Exception as err: