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