-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtortoro.py
61 lines (50 loc) · 1.69 KB
/
tortoro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import config
import requests
from stem.control import Controller as TorController
from stem import Signal as TorSignal
IP_CHECKER = 'http://icanhazip.com/'
class TorTorO:
def __init__(self, config):
self.c = config
self._real_ip = None
@property
def real_ip(self):
if self._real_ip is None:
self._real_ip = self.check_ip()
return self._real_ip
@property
def fake_ip(self):
return self.check_ip(self.c.LOCAL_HTTP_PROXY)
def check_ip(self, proxy=None):
if proxy is not None:
resp = requests.get(IP_CHECKER, proxies={'http': proxy})
else:
resp = requests.get(IP_CHECKER)
if resp.ok:
return resp.text.strip()
def _change_tor_ident(self):
port_no = self.c.TOR_PORT
tor_pass = self.c.TOR_PASSWORD
with TorController.from_port(port=port_no) as controller:
controller.authenticate(password=tor_pass)
controller.signal(TorSignal.NEWNYM)
def get_new_ip(self):
n_attempts = 0
cur_ip = self.fake_ip
while n_attempts < self.c.MAX_ATTEMPTS:
self._change_tor_ident()
n_attempts += 1
new_ip = self.fake_ip
if new_ip != cur_ip:
return new_ip
def download_file(self, url, fname):
proxy = self.c.LOCAL_HTTP_PROXY
resp = requests.get(url, stream=True, proxies={'http': proxy})
with open(fname, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
if __name__ == '__main__':
tto = TorTorO(config)
print(tto.fake_ip)
print(tto.get_new_ip())