|
|
@ -1,13 +1,16 @@
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from torrequest import TorRequest
|
|
|
|
import stem
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import random
|
|
|
|
from fake_useragent import UserAgent
|
|
|
|
from fake_useragent import UserAgent
|
|
|
|
|
|
|
|
from stem.control import Controller
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
class Client:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self, session=None):
|
|
|
|
self.session = requests.Session()
|
|
|
|
self.proxies = None
|
|
|
|
self.session.headers['UserAgent'] = UserAgent().random
|
|
|
|
self.headers = {'UserAgent': UserAgent().random}
|
|
|
|
|
|
|
|
|
|
|
|
def request(self, *args, method='GET', **kwargs):
|
|
|
|
def request(self, *args, method='GET', **kwargs):
|
|
|
|
if method == 'GET':
|
|
|
|
if method == 'GET':
|
|
|
@ -24,26 +27,26 @@ class Client:
|
|
|
|
raise Exception('Invalid HTTP method specified!')
|
|
|
|
raise Exception('Invalid HTTP method specified!')
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
return self.session.delete(*args, **kwargs)
|
|
|
|
return requests.delete(*args, proxies=self.proxies, headers=self.headers, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
def close(self):
|
|
|
|
self.session.close()
|
|
|
|
self.headers = None
|
|
|
|
|
|
|
|
self.proxies = None
|
|
|
|
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
def reset(self):
|
|
|
|
self.session.headers['UserAgent'] = UserAgent().random
|
|
|
|
self.headers['UserAgent'] = UserAgent().random
|
|
|
|
self.session.cookies.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
return self
|
|
|
@ -54,17 +57,30 @@ class Client:
|
|
|
|
|
|
|
|
|
|
|
|
class TorClient(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__()
|
|
|
|
super().__init__()
|
|
|
|
self.tr = TorRequest(*args, **kwargs)
|
|
|
|
self.ctrl = Controller.from_port(port=self.ctrl_port)
|
|
|
|
self.tr.session.headers['UserAgent'] = UserAgent().random
|
|
|
|
self.ctrl.authenticate(password=password)
|
|
|
|
self.session = self.tr
|
|
|
|
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):
|
|
|
|
def new_identity(self):
|
|
|
|
self.tr.reset_identity()
|
|
|
|
print('[ ] Requesting new ip adress')
|
|
|
|
print("[+] Changed IP to %s: " % self.get('https://ident.me').text)
|
|
|
|
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):
|
|
|
|
def reset(self):
|
|
|
|
self.new_identity()
|
|
|
|
self.new_identity()
|
|
|
|
self.tr.session.cookies.clear()
|
|
|
|
self.headers['UserAgent'] = UserAgent().random
|
|
|
|
self.tr.session.headers['UserAgent'] = UserAgent().random
|
|
|
|
|
|
|
|