This commit is contained in:
Rémi Oudin 2018-02-25 21:34:20 +01:00
parent e074d96f02
commit 0a676a2f65

View file

@ -2,14 +2,12 @@
Modules that handles tor instaces creations in order to safely run histories
"""
import stem.process as tor
import shutil
import asyncio
import aiohttp
import aiosocks
from aiosocks.connector import ProxyConnector, ProxyClientRequest
import async_timeout
import io
import stem.process as tor
class TorInstance():
"""
@ -34,9 +32,9 @@ class TorInstance():
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.create_session()
self.process = tor.launch_tor_with_config(
config = {
config={
'ControlPort' : str(control_port),
'SocksPort' : str(socks_port),
'DataDir' : data_dir
@ -44,14 +42,29 @@ class TorInstance():
)
return self
def __init__(self):
self.socks_port = 0
self.control_port = 0
self.data_dir = ""
self.history = None
self.proxy = ""
self.session = None
self.process = None
def create_session(self):
""" Create a aiohttp session.
"""
conn = ProxyConnector(remote_resolve=True)
return aiohttp.ClientSession(
self.session = aiohttp.ClientSession(
connector=conn,
request_class=ProxyClientRequest
)
async def query(self, url):
""" Performs a query.
"""
async with async_timeout.timeout(30):
async with self.session.get(
url,
@ -66,7 +79,7 @@ class TorInstance():
def __str__(self):
""" Utility function """
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):
""" Kills the process and remove the data dir"""
@ -77,12 +90,12 @@ class TorInstance():
async def main():
""" Test function """
for i in range(3):
a = await TorInstance.create(None)
output = await a.query("https://python.org/")
for _ in range(3):
instance = await TorInstance.create(None)
await instance.query("https://python.org/")
print("One page received")
await a.kill()
await instance.kill()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(main())