From 23aa5bd1373ba155f10385ff098726b70d91c223 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 23 Feb 2020 11:57:16 +0100 Subject: [PATCH] Remove tor request and switch to standalone requests --- Pipfile | 2 +- Pipfile.lock | 18 ++-------------- lib/client.py | 58 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/Pipfile b/Pipfile index a90a90f..4d7b373 100644 --- a/Pipfile +++ b/Pipfile @@ -7,8 +7,8 @@ verify_ssl = true [packages] requests = "*" -torrequest = "*" fake-useragent = "*" +stem = "*" [requires] python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock index a4ca2e7..3969581 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "06db23fe7606fa26a31e3b7af11b3de2e5245c2abbbd74db4e986e8ff2595ea7" + "sha256": "1b9a80175da06b66acee213480ffc51f4c7c2566c55ed79e717e786b4653e26b" }, "pipfile-spec": 6, "requires": { @@ -44,14 +44,6 @@ ], "version": "==2.9" }, - "pysocks": { - "hashes": [ - "sha256:08e69f092cc6dbe92a0fdd16eeb9b9ffbc13cadfe5ca4c7bd92ffb078b293299", - "sha256:2725bd0a9925919b9b51739eea5f9e2bae91e83288108a9ad338b2e3a4435ee5", - "sha256:3f8804571ebe159c380ac6de37643bb4685970655d3bba243530d6558b799aa0" - ], - "version": "==1.7.1" - }, "requests": { "hashes": [ "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee", @@ -64,14 +56,8 @@ "hashes": [ "sha256:a0b48ea6224e95f22aa34c0bc3415f0eb4667ddeae3dfb5e32a6920c185568c2" ], - "version": "==1.8.0" - }, - "torrequest": { - "hashes": [ - "sha256:3745d4ea3ffda98d7a034363c787adb37aab77bdab40094a4d937392cd4dae82" - ], "index": "pypi", - "version": "==0.1.0" + "version": "==1.8.0" }, "urllib3": { "hashes": [ diff --git a/lib/client.py b/lib/client.py index c918b76..af0aa55 100644 --- a/lib/client.py +++ b/lib/client.py @@ -1,13 +1,16 @@ import requests -from torrequest import TorRequest +import stem +import time +import random from fake_useragent import UserAgent +from stem.control import Controller class Client: - def __init__(self): - self.session = requests.Session() - self.session.headers['UserAgent'] = UserAgent().random + def __init__(self, session=None): + self.proxies = None + self.headers = {'UserAgent': UserAgent().random} def request(self, *args, method='GET', **kwargs): if method == 'GET': @@ -24,26 +27,26 @@ class Client: raise Exception('Invalid HTTP method specified!') def get(self, *args, **kwargs): - return self.session.get(*args, **kwargs) + return requests.get(*args, proxies=self.proxies, headers=self.headers, **kwargs) def post(self, *args, **kwargs): - return self.session.get(*args, **kwargs) + return requests.get(*args, proxies=self.proxies, headers=self.headers, **kwargs) def put(self, *args, **kwargs): - return self.session.put(*args, **kwargs) + return requests.put(*args, proxies=self.proxies, headers=self.headers, **kwargs) def patch(self, *args, **kwargs): - return self.session.patch(*args, **kwargs) + return requests.patch(*args, proxies=self.proxies, headers=self.headers, **kwargs) - def delete(self,*args, **kwargs): - return self.session.delete(*args, **kwargs) + def delete(self, *args, **kwargs): + return requests.delete(*args, proxies=self.proxies, headers=self.headers, **kwargs) def close(self): - self.session.close() + self.headers = None + self.proxies = None def reset(self): - self.session.headers['UserAgent'] = UserAgent().random - self.session.cookies.clear() + self.headers['UserAgent'] = UserAgent().random def __enter__(self): return self @@ -54,17 +57,30 @@ class Client: class TorClient(Client): - def __init__(self, *args, **kwargs): + def __init__(self, proxy_port=9050, ctrl_port=9051, password=None): + self.proxy_port = proxy_port + self.ctrl_port = ctrl_port super().__init__() - self.tr = TorRequest(*args, **kwargs) - self.tr.session.headers['UserAgent'] = UserAgent().random - self.session = self.tr + self.ctrl = Controller.from_port(port=self.ctrl_port) + self.ctrl.authenticate(password=password) + self.ip_retrieval_sites = [ + 'http://ipecho.net/plain', + 'https://ident.me', + ] + self.proxies = { + 'http': 'socks5://127.0.0.1:%d' % self.proxy_port, + 'https': 'socks5://127.0.0.1:%d' % self.proxy_port + } def new_identity(self): - self.tr.reset_identity() - print("[+] Changed IP to %s: " % self.get('https://ident.me').text) + print('[ ] Requesting new ip adress') + self.ctrl.signal(stem.Signal.NEWNYM) + time.sleep(self.ctrl.get_newnym_wait()) + print("[+] Changed IP to %s: " % self.get(random.choice(self.ip_retrieval_sites)).text) + + def close(self): + self.ctrl.close() def reset(self): self.new_identity() - self.tr.session.cookies.clear() - self.tr.session.headers['UserAgent'] = UserAgent().random + self.headers['UserAgent'] = UserAgent().random