-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tor.py
130 lines (110 loc) · 3.74 KB
/
Tor.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import subprocess
import time
import requests
class IPError(Exception):
pass
class ConnectionError(Exception):
pass
class InstallationError(Exception):
pass
class SessionError(Exception):
pass
class Tor:
def __init__(self):
# Proxies configuration
self.proxies = {
'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'
}
def install_tor_windows(self):
"""
Install Tor on Windows using Chocolatey package manager.
"""
try:
os.system("choco install tor")
os.system("tor")
except Exception as e:
raise InstallationError("Failed to install Tor on Windows") from e
def install_tor_linux(self):
"""
Install Tor on Linux using apt-get package manager.
"""
try:
os.system("sudo apt-get install tor")
os.system("sudo service tor start")
except Exception as e:
raise InstallationError("Failed to install Tor on Linux") from e
@property
def is_tor_installed(self):
"""
Check if Tor is installed.
Returns:
bool: True if Tor is installed, False otherwise.
"""
if os.name == "nt": # Windows
result = subprocess.run(["where", "tor"], capture_output=True, text=True)
else: # Linux
result = subprocess.run(["which", "tor"], capture_output=True, text=True)
return result.returncode == 0
def renew_ip(self):
"""
Renew the IP address by restarting the Tor service.
"""
try:
if os.name == "nt": # Windows
os.system("tor --service stop")
else: # Linux
os.system("sudo service tor stop")
time.sleep(3)
if os.name == "nt": # Windows
os.system("tor")
else: # Linux
os.system("sudo service tor start")
except Exception as e:
raise ConnectionError("Failed to renew IP address") from e
def start_tor(self):
"""
Start the Tor service.
"""
try:
if os.name == "nt": # Windows
os.system("tor")
else: # Linux
os.system("sudo service tor start")
except Exception as e:
raise ConnectionError("Failed to start Tor service") from e
def get_ip(self):
"""
Get the current IP address using an external API.
Raises:
requests.exceptions.RequestException: If there is an error in making the request.
IPError: If there is an error in retrieving the IP address.
Returns:
str: The current IP address.
"""
try:
response = requests.get('https://api.myip.com', proxies=self.proxies)
response.raise_for_status()
ip = response.json()["ip"]
print(f"Your IP is: {ip}")
return ip
except requests.exceptions.RequestException as e:
raise IPError("Failed to get IP address") from e
def get_session(self):
"""
Get a new requests Session object with configured proxies.
Raises:
ConnectionError: If there is an error in renewing the IP address.
SessionError: If there is an error in creating the session.
Returns:
requests.Session: A Session object with configured proxies.
"""
try:
self.renew_ip()
time.sleep(1)
session = requests.Session()
session.proxies = self.proxies
return session
except ConnectionError as e:
raise SessionError("Failed to create session") from e