[BUGFIX] Fix "Can't get public ip address" error while tor is still establishing the connections

This commit is contained in:
Dmitry Dulepov 2016-08-06 23:35:01 +03:00
parent bbd908679d
commit c6cf57ea9e

View file

@ -17,7 +17,7 @@ from atexit import register
from argparse import ArgumentParser from argparse import ArgumentParser
from json import load from json import load
from urllib2 import urlopen, URLError from urllib2 import urlopen, URLError
from time import sleep
class TorIptables(object): class TorIptables(object):
@ -57,10 +57,17 @@ DNSPort %s
print(" {0}".format( print(" {0}".format(
"[\033[92m+\033[0m] Anonymizer status \033[92m[ON]\033[0m")) "[\033[92m+\033[0m] Anonymizer status \033[92m[ON]\033[0m"))
print(" {0}".format("[\033[92m*\033[0m] Getting public IP, please wait...")) print(" {0}".format("[\033[92m*\033[0m] Getting public IP, please wait..."))
try: retries = 0
my_public_ip = load(urlopen('http://jsonip.com'))['ip']
except URLError:
my_public_ip = None my_public_ip = None
while retries < 12 and not my_public_ip:
retries += 1
try:
my_public_ip = load(urlopen('http://jsonip.com/'))['ip']
except URLError:
sleep(5)
print(" [\033[93m?\033[0m] Still waiting for IP address...")
print
if not my_public_ip:
my_public_ip = getoutput('wget -qO - v4.ifconfig.co') my_public_ip = getoutput('wget -qO - v4.ifconfig.co')
if not my_public_ip: if not my_public_ip:
exit(" \033[91m[!]\033[0m Can't get public ip address!") exit(" \033[91m[!]\033[0m Can't get public ip address!")
@ -69,6 +76,11 @@ DNSPort %s
except CalledProcessError as err: except CalledProcessError as err:
print("\033[91m[!] Command failed: %s\033[0m" % ' '.join(err.cmd)) print("\033[91m[!] Command failed: %s\033[0m" % ' '.join(err.cmd))
# See https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy#WARNING
# See https://lists.torproject.org/pipermail/tor-talk/2014-March/032503.html
call(["iptables", "-I", "OUTPUT", "!", "-o", "lo", "!", "-d", "127.0.0.1", "!", "-s", "127.0.0.1", "-p", "tcp", "-m", "tcp", "--tcp-flags", "ACK,FIN", "ACK,FIN", "-j", "DROP"])
call(["iptables", "-I", "OUTPUT", "!", "-o", "lo", "!", "-d", "127.0.0.1", "!", "-s", "127.0.0.1", "-p", "tcp", "-m", "tcp", "--tcp-flags", "ACK,RST", "ACK,RST", "-j", "DROP"])
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-m", "owner", "--uid-owner", call(["iptables", "-t", "nat", "-A", "OUTPUT", "-m", "owner", "--uid-owner",
"%s" % self.tor_uid, "-j", "RETURN"]) "%s" % self.tor_uid, "-j", "RETURN"])
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-p", "udp", "--dport", call(["iptables", "-t", "nat", "-A", "OUTPUT", "-p", "udp", "--dport",