An attempt at adding UDP support to aiosocks. Untested due to lack of server support.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
1.3 KiB

  1. import asyncio
  2. import contextlib
  3. import socket
  4. from unittest import mock
  5. import gc
  6. try:
  7. from asyncio import ensure_future
  8. except ImportError:
  9. ensure_future = asyncio.async
  10. def fake_coroutine(return_value):
  11. def coro(*args, **kwargs):
  12. if isinstance(return_value, Exception):
  13. raise return_value
  14. return return_value
  15. return mock.Mock(side_effect=asyncio.coroutine(coro))
  16. def find_unused_port():
  17. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18. s.bind(('127.0.0.1', 0))
  19. port = s.getsockname()[1]
  20. s.close()
  21. return port
  22. @contextlib.contextmanager
  23. def fake_socks_srv(loop, write_buff):
  24. transports = []
  25. class SocksPrimitiveProtocol(asyncio.Protocol):
  26. _transport = None
  27. def connection_made(self, transport):
  28. self._transport = transport
  29. transports.append(transport)
  30. def data_received(self, data):
  31. self._transport.write(write_buff)
  32. port = find_unused_port()
  33. def factory():
  34. return SocksPrimitiveProtocol()
  35. srv = loop.run_until_complete(
  36. loop.create_server(factory, '127.0.0.1', port))
  37. yield port
  38. for tr in transports:
  39. tr.close()
  40. srv.close()
  41. loop.run_until_complete(srv.wait_closed())
  42. gc.collect()