ppf: add safeguards against tor outage

This commit is contained in:
rofl0r
2019-01-08 15:43:03 +00:00
parent b6839eea79
commit 6e4c45175e

24
ppf.py
View File

@@ -43,11 +43,25 @@ def fetch_contents(url):
'Accept-Language: en-US,en;q=0.8',
'Cache-Control: max-age=0',
]
proxies = [rocksock.RocksockProxyFromURL('socks4://%s' % random.choice( config.torhosts ))]
http = RsHttp(host,ssl=ssl,port=port, keep_alive=True, timeout=config.ppf.timeout, max_tries=config.ppf.http_retries, follow_redirects=True, auto_set_cookies=True, proxies=proxies, user_agent='Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0')
if not http.connect():
_log("failed to connect to %s"%url, "ppf")
return ''
while True:
proxies = [rocksock.RocksockProxyFromURL('socks4://%s' % random.choice( config.torhosts ))]
http = RsHttp(host,ssl=ssl,port=port, keep_alive=True, timeout=config.ppf.timeout, max_tries=config.ppf.http_retries, follow_redirects=True, auto_set_cookies=True, proxies=proxies, user_agent='Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0')
if not http.connect():
_log("failed to connect to %s"%url, "ppf")
e = http.get_last_rocksock_exception()
if not e:
return ''
et = e.get_errortype()
ee = e.get_error()
ef = e.get_failedproxy()
if et == rocksock.RS_ET_OWN and \
ee == rocksock.RS_E_TARGET_CONN_REFUSED \
and ef == 0:
_log("could not connect to proxy 0 - check your connection", "error")
time.sleep(5)
continue
return ''
break
hdr, res = http.get(uri, headers)
res = res.encode('utf-8') if isinstance(res, unicode) else res
for retry_message in retry_messages: