-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorify_linux.py
More file actions
74 lines (65 loc) · 2.98 KB
/
Copy pathtorify_linux.py
File metadata and controls
74 lines (65 loc) · 2.98 KB
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
import requests
import subprocess
import psutil
import time
import sys
from stem import Signal
from stem.control import Controller
from colorama import Fore,init
# initialize color
init()
class TorifyLinux:
def __init__(self):
self.tor_proxy_host = '127.0.0.1'
self.tor_proxy_port = 9050
self.ip_check = 'https://api.my-ip.io/v2/ip.json'
def run_command_in_background(self,command, output_file):
# Run the command in the background and redirect the output to a file
process = subprocess.Popen(command, shell=True, stdout=open(output_file, 'w'), stderr=subprocess.STDOUT)
# Return the process object
return process
def make_tor_request(self,url):
session = requests.Session()
session.proxies = {'http': f'socks5://{self.tor_proxy_host}:{self.tor_proxy_port}',
'https': f'socks5://{self.tor_proxy_host}:{self.tor_proxy_port}'}
try:
response = session.get(url)
return response.json()['ip']
except requests.exceptions.RequestException as e:
return "Error making request"
def killtor(self,process_name):
try:
for process in psutil.process_iter(['pid', 'name']):
if process.info['name'] == process_name:
pid = process.info['pid']
psutil.Process(pid).terminate()
print(f"[{Fore.GREEN}INF{Fore.RESET}] Process with name {process_name} (PID: {pid}) terminated successfully.")
except Exception as e:
print(f"[{Fore.RED}ERR{Fore.RESET}] Error terminating process with name {process_name}: {e}")
def renew(self):
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
def run(self,times=10):
# Attempt to get a new IP address
print(f"[{Fore.GREEN}INF{Fore.RESET}] Time to rotate the ip address is ({times}s)")
print(f"[{Fore.GREEN}INF{Fore.RESET}] Please run your browser or tools over this proxy => socks5://{self.tor_proxy_host}:{self.tor_proxy_port}")
print(f"[{Fore.GREEN}INF{Fore.RESET}] Starting new tor...")
pid = self.run_command_in_background("tor &","log.txt")
time.sleep(2)
while True:
try:
initial_ip = self.make_tor_request(self.ip_check)
print(f"[{Fore.GREEN}INF{Fore.RESET}] Initial IP address: {initial_ip}")
for timesec in range(times):
try:
print(f"[{Fore.GREEN}INF{Fore.RESET}] {times-timesec}s To renew the ip address ",end="\r")
time.sleep(1)
except KeyboardInterrupt:
self.killtor('tor')
sys.exit()
self.renew()
except Exception as e:
print(f"[{Fore.RED}ERR{Fore.RESET}] {e}")
self.killtor('tor')
sys.exit()