Skip to content

Commit

Permalink
Merge pull request #22 from nibrag/starttls
Browse files Browse the repository at this point in the history
New starttls implemenation
  • Loading branch information
nibrag authored Nov 20, 2017
2 parents d6fef33 + efd1ff0 commit 932374c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion aiosocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)
from .protocols import Socks4Protocol, Socks5Protocol, DEFAULT_LIMIT

__version__ = '0.2.5'
__version__ = '0.2.6'

__all__ = ('Socks4Protocol', 'Socks5Protocol', 'Socks4Auth',
'Socks5Auth', 'Socks4Addr', 'Socks5Addr', 'SocksError',
Expand Down
30 changes: 16 additions & 14 deletions aiosocks/protocols.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import socket
import struct
from asyncio import sslproto

from . import constants as c
from .helpers import (
Socks4Addr, Socks5Addr, Socks5Auth, Socks4Auth
Expand Down Expand Up @@ -67,20 +69,20 @@ async def negotiate(self, reader, writer):
if self._ssl:
# Creating a ssl transport needs to be reworked.
# See details: http://bugs.python.org/issue23749
sock = self._transport.get_extra_info('socket')

# temporary fix:
self._transport.pause_reading()
self._transport._closing = True
self._transport._sock = None
self._transport._protocol = None
self._transport._loop = None

self._transport = self._loop._make_ssl_transport(
rawsock=sock, protocol=self._app_protocol,
sslcontext=self._ssl, server_side=False,
server_hostname=self._server_hostname,
waiter=self._waiter)
self._tls_protocol = sslproto.SSLProtocol(
app_protocol=self, sslcontext=self._ssl, server_side=False,
server_hostname=self._server_hostname, waiter=self._waiter,
loop=self._loop, call_connection_made=False)

# starttls
original_transport = self._transport
self._transport.set_protocol(self._tls_protocol)
self._transport = self._tls_protocol._app_transport

self._tls_protocol.connection_made(original_transport)

self._loop.call_soon(self._app_protocol.connection_made,
self._transport)
else:
self._loop.call_soon(self._app_protocol.connection_made,
self._transport)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
raise RuntimeError('Unable to determine version.')


if sys.version_info < (3, 5, 0):
raise RuntimeError("aiosocks requires Python 3.5+")
if sys.version_info < (3, 5, 3):
raise RuntimeError("aiosocks requires Python 3.5.3+")


setup(
Expand Down
8 changes: 6 additions & 2 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,12 @@ async def handler(request):
ws = RawTestServer(handler, scheme='https', host='127.0.0.1', loop=loop)
await ws.start_server(loop=loop, ssl=sslcontext)

v_fp = b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9\x1a\xe3\xc5\x7f\x89\xe7l\xf9'
inv_fp = b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9\x1a\xe3\xc5\x7f\x89\xe7l\x10'
v_fp = (b'0\x9a\xc9D\x83\xdc\x91\'\x88\x91\x11\xa1d\x97\xfd'
b'\xcb~7U\x14D@L'
b'\x11\xab\x99\xa8\xae\xb7\x14\xee\x8b')
inv_fp = (b'0\x9d\xc9D\x83\xdc\x91\'\x88\x91\x11\xa1d\x97\xfd'
b'\xcb~7U\x14D@L'
b'\x11\xab\x99\xa8\xae\xb7\x14\xee\x9e')

async with FakeSocks4Srv(loop) as srv:
v_conn = ProxyConnector(loop=loop, remote_resolve=False,
Expand Down
7 changes: 2 additions & 5 deletions tests/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import socket
import ssl as ssllib
from unittest import mock
from asyncio import coroutine as coro
from asyncio import coroutine as coro, sslproto
from aiohttp.test_utils import make_mocked_coro
import aiosocks.constants as c
from aiosocks.protocols import BaseSocksProtocol
Expand Down Expand Up @@ -272,10 +272,7 @@ async def test_base_make_ssl_proto():
proto._transport = mock.Mock()
await proto.negotiate(None, None)

mtr = loop_mock._make_ssl_transport

assert mtr.called
assert mtr.call_args[1]['sslcontext'] is ssl_context
assert isinstance(proto._transport, sslproto._SSLProtocolTransport)


async def test_base_func_negotiate_cb_call():
Expand Down

0 comments on commit 932374c

Please sign in to comment.