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.
 
 

52 lines
1.2 KiB

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