tor_runner can make requests
This commit is contained in:
parent
ae5699c089
commit
e074d96f02
2 changed files with 38 additions and 6 deletions
|
@ -5,6 +5,11 @@ Modules that handles tor instaces creations in order to safely run histories
|
||||||
import stem.process as tor
|
import stem.process as tor
|
||||||
import shutil
|
import shutil
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import aiohttp
|
||||||
|
import aiosocks
|
||||||
|
from aiosocks.connector import ProxyConnector, ProxyClientRequest
|
||||||
|
import async_timeout
|
||||||
|
import io
|
||||||
|
|
||||||
class TorInstance():
|
class TorInstance():
|
||||||
"""
|
"""
|
||||||
|
@ -17,7 +22,7 @@ class TorInstance():
|
||||||
TOR_RUNNER = 0
|
TOR_RUNNER = 0
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create(cls):
|
async def create(cls, history):
|
||||||
""" Factory creation of tor processes"""
|
""" Factory creation of tor processes"""
|
||||||
socks_port = cls.BASE_SOCKS_PORT + cls.TOR_RUNNER
|
socks_port = cls.BASE_SOCKS_PORT + cls.TOR_RUNNER
|
||||||
control_port = cls.BASE_CONTROL_PORT + cls.TOR_RUNNER
|
control_port = cls.BASE_CONTROL_PORT + cls.TOR_RUNNER
|
||||||
|
@ -27,6 +32,9 @@ class TorInstance():
|
||||||
self.socks_port = socks_port
|
self.socks_port = socks_port
|
||||||
self.control_port = control_port
|
self.control_port = control_port
|
||||||
self.data_dir = data_dir
|
self.data_dir = data_dir
|
||||||
|
self.history = history
|
||||||
|
self.proxy = "socks5://127.0.0.1:{}".format(self.socks_port)
|
||||||
|
self.session = self.create_session()
|
||||||
self.process = tor.launch_tor_with_config(
|
self.process = tor.launch_tor_with_config(
|
||||||
config = {
|
config = {
|
||||||
'ControlPort' : str(control_port),
|
'ControlPort' : str(control_port),
|
||||||
|
@ -36,21 +44,43 @@ class TorInstance():
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def create_session(self):
|
||||||
|
conn = ProxyConnector(remote_resolve=True)
|
||||||
|
return aiohttp.ClientSession(
|
||||||
|
connector=conn,
|
||||||
|
request_class=ProxyClientRequest
|
||||||
|
)
|
||||||
|
|
||||||
|
async def query(self, url):
|
||||||
|
async with async_timeout.timeout(30):
|
||||||
|
async with self.session.get(
|
||||||
|
url,
|
||||||
|
proxy=self.proxy,
|
||||||
|
proxy_auth=None) as resp:
|
||||||
|
try:
|
||||||
|
return await resp.text()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
""" Utility function """
|
""" Utility function """
|
||||||
return "[TOR] SOCKSPort: {0.socks_port}, ControlPort: "
|
return ('[TOR] SOCKSPort: {0.socks_port}, ControlPort: '
|
||||||
"{0.control_port}, DataDir: {0.data_dir}".format(self)
|
'{0.control_port}, DataDir: {0.data_dir}'.format(self))
|
||||||
|
|
||||||
async def kill(self):
|
async def kill(self):
|
||||||
""" Kills the process and remove the data dir"""
|
""" Kills the process and remove the data dir"""
|
||||||
self.process.kill()
|
self.process.kill()
|
||||||
|
self.session.close()
|
||||||
shutil.rmtree(self.data_dir)
|
shutil.rmtree(self.data_dir)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
""" Test function """
|
""" Test function """
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
a = await TorInstance.create()
|
a = await TorInstance.create(None)
|
||||||
print(a)
|
output = await a.query("https://python.org/")
|
||||||
|
print("One page received")
|
||||||
await a.kill()
|
await a.kill()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
aiodns==1.1.1
|
aiodns==1.1.1
|
||||||
aiohttp==3.0.1
|
aiohttp==2.3.2
|
||||||
async-timeout==2.0.0
|
async-timeout==2.0.0
|
||||||
attrs==17.4.0
|
attrs==17.4.0
|
||||||
cchardet==2.1.1
|
cchardet==2.1.1
|
||||||
|
@ -12,3 +12,5 @@ pycares==2.3.0
|
||||||
pytz==2017.3
|
pytz==2017.3
|
||||||
yarl==1.1.1
|
yarl==1.1.1
|
||||||
beautifulsoup4==4.6.0
|
beautifulsoup4==4.6.0
|
||||||
|
stem==1.6.0
|
||||||
|
pycurl==7.43.0.1
|
||||||
|
|
Loading…
Reference in a new issue