Commit 9bda68f7 authored by Maxim Gubin's avatar Maxim Gubin Committed by Francesco Poldi

Tor proxies autorotation on time out (#281)

* Update run

* Update get

* Update config

* Fix import
parent 9093d9f7
......@@ -56,6 +56,8 @@ class Config:
Proxy_host = None
Proxy_port = 0
Proxy_type = None
Tor_control_port = 9051
Tor_control_password = None
Retweets = False
Query = None
Hide_output = False
......
......@@ -2,6 +2,7 @@ from async_timeout import timeout
from datetime import datetime
from bs4 import BeautifulSoup
import sys
import socket
import aiohttp
import asyncio
import concurrent.futures
......@@ -79,7 +80,17 @@ async def MobileRequest(url, **options):
async with aiohttp.ClientSession() as session:
return await Response(session, url)
def ForceNewTorIdentity(config):
try:
tor_c = socket.create_connection(('127.0.0.1', config.Tor_control_port))
tor_c.send('AUTHENTICATE "{}"\r\nSIGNAL NEWNYM\r\n'.format(config.Tor_control_password).encode())
response = tor_c.recv(1024)
if response != b'250 OK\r\n250 OK\r\n':
sys.stderr.write('Unexpected response from Tor control port: {}\n'.format(response))
except Exception as e:
sys.stderr.write('Error connecting to Tor control port: {}\n'.format(repr(e)))
sys.stderr.write('If you want to rotate Tor ports automatically - enable Tor control port\n')
async def Request(url, connector=None, params=[], headers=[]):
#loggin.info("[<] " + str(datetime.now()) + ':: get+Request')
if connector:
......
from . import datelock, feed, get, output, verbose, storage
from asyncio import get_event_loop
from asyncio import get_event_loop, TimeoutError
from datetime import timedelta, datetime
from .storage import db
import sys
#import logging
......@@ -55,6 +56,19 @@ class Twint:
elif self.config.TwitterSearch:
self.feed, self.init = feed.Json(response)
break
except TimeoutError as e:
if self.config.Proxy_host.lower() == "tor":
print("[?] Timed out, changing Tor identity...")
if self.config.Tor_control_password is None:
sys.stderr.write("Error: config.Tor_control_password must be set for proxy autorotation!\r\n")
sys.stderr.write("Info: What is it? See https://stem.torproject.org/faq.html#can-i-interact-with-tors-controller-interface-directly\r\n")
break
else:
get.ForceNewTorIdentity(self.config)
continue
else:
print(str(e))
break
except Exception as e:
# Sometimes Twitter says there is no data. But it's a lie.
consecutive_errors_count += 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment