def make_socks5(loop, *, addr=None, auth=None, rr=True, dst=None, r=None, ap_factory=None, whiter=None): addr = addr or aiosocksy.Socks5Addr('localhost', 1080) auth = auth or aiosocksy.Socks5Auth('user', 'pwd') dst = dst or ('python.org', 80) proto = aiosocksy.Socks5Protocol(proxy=addr, proxy_auth=auth, dst=dst, remote_resolve=rr, loop=loop, app_protocol_factory=ap_factory, waiter=whiter) proto._stream_writer = mock.Mock() proto._stream_writer.drain = make_mocked_coro(True) if not isinstance(r, (list, tuple)): proto.read_response = mock.Mock( side_effect=coro(mock.Mock(return_value=r))) else: proto.read_response = mock.Mock( side_effect=coro(mock.Mock(side_effect=r))) proto._get_dst_addr = mock.Mock( side_effect=coro(mock.Mock(return_value=(socket.AF_INET, '127.0.0.1')))) return proto
async def test_socks5_cmd_not_granted(loop): async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x01\x00') as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with pytest.raises(aiosocksy.SocksError) as ct: await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert 'General SOCKS server failure' in str(ct)
async def test_socks5_invalid_address_type(loop): async with FakeSocksSrv(loop, b'\x05\x02\x01\x00\x05\x00\x00\xFF') as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with pytest.raises(aiosocksy.SocksError) as ct: await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert 'invalid data' in str(ct)
async def test_socks5_auth_ver_err(loop): async with FakeSocksSrv(loop, b'\x04\x02') as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with pytest.raises(aiosocksy.SocksError) as ct: await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert 'invalid version' in str(ct)
async def test_socks5_auth_failed(loop): async with FakeSocksSrv(loop, b'\x05\x02\x01\x01') as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with pytest.raises(aiosocksy.SocksError) as ct: await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert 'authentication failed' in str(ct)
async def test_connection_fail(): addr = aiosocksy.Socks5Addr('localhost') auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) loop_mock = mock.Mock() loop_mock.create_connection = make_mocked_coro(raise_exception=OSError()) with pytest.raises(aiosocksy.SocksConnectionError): await aiosocksy.create_connection(None, addr, auth, dst, loop=loop_mock)
def test_socks5_ctor(loop): addr = aiosocksy.Socks5Addr('localhost', 1080) auth = aiosocksy.Socks5Auth('user', 'pwd') dst = ('python.org', 80) with pytest.raises(ValueError): aiosocksy.Socks5Protocol(None, None, dst, loop=loop, waiter=None, app_protocol_factory=None) with pytest.raises(ValueError): aiosocksy.Socks5Protocol(None, auth, dst, loop=loop, waiter=None, app_protocol_factory=None) with pytest.raises(ValueError): aiosocksy.Socks5Protocol(aiosocksy.Socks4Addr('host'), auth, dst, loop=loop, waiter=None, app_protocol_factory=None) with pytest.raises(ValueError): aiosocksy.Socks5Protocol(addr, aiosocksy.Socks4Auth('l'), dst, loop=loop, waiter=None, app_protocol_factory=None) aiosocksy.Socks5Protocol(addr, None, dst, loop=loop, waiter=None, app_protocol_factory=None) aiosocksy.Socks5Protocol(addr, auth, dst, loop=loop, waiter=None, app_protocol_factory=None)
def __init__(self, address, port, login=None, password=None, timeout=10, loop=None): addr = aiosocksy.Socks5Addr(address, port) if login and password: auth = aiosocksy.Socks5Auth(login, password=password) else: auth = None conn = self.connector(proxy=addr, proxy_auth=auth, loop=loop) session = aiohttp.ClientSession(connector=conn) super().__init__(timeout, loop, session)
async def test_socks5_atype_domain(loop): pld = b'\x05\x02\x01\x00\x05\x00\x00\x03\x0apython.org\x04W' async with FakeSocksSrv(loop, pld) as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) transport, protocol = await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert protocol.proxy_sockname == (b'python.org', 1111) transport.close()
def get_proxy_data(): if config.PROXY_URL and config.PROXY_LOGIN and config.PROXY_PASSWORD: proxy = config.PROXY_URL if proxy.startswith('socks5'): import aiosocksy logger.info('Socks5 proxy enabled.') proxy_auth = aiosocksy.Socks5Auth(login=config.PROXY_LOGIN, password=config.PROXY_PASSWORD) else: import aiohttp logger.info('HTTP proxy enabled.') proxy_auth = aiohttp.BasicAuth(login=config.PROXY_LOGIN, password=config.PROXY_PASSWORD) else: logger.info('Proxy disabled.') proxy = None proxy_auth = None return proxy, proxy_auth
async def test_negotiate_fail(): addr = aiosocksy.Socks5Addr('localhost') auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) loop_mock = mock.Mock() loop_mock.create_connection = make_mocked_coro((mock.Mock(), mock.Mock())) with mock.patch('aiosocksy.asyncio.Future') as future_mock: future_mock.side_effect = make_mocked_coro( raise_exception=aiosocksy.SocksError()) with pytest.raises(aiosocksy.SocksError): await aiosocksy.create_connection(None, addr, auth, dst, loop=loop_mock)
async def test_socks5_connect_success_usr_pwd(loop): pld = b'\x05\x02\x01\x00\x05\x00\x00\x01\x01\x01\x01\x01\x04Wtest' async with FakeSocksSrv(loop, pld) as srv: addr = aiosocksy.Socks5Addr('127.0.0.1', srv.port) auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) transport, protocol = await aiosocksy.create_connection(None, addr, auth, dst, loop=loop) assert protocol.proxy_sockname == ('1.1.1.1', 1111) data = await protocol._stream_reader.read(4) assert data == b'test' transport.close()
async def test_open_connection(): addr = aiosocksy.Socks5Addr('localhost') auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) transp, proto = mock.Mock(), mock.Mock() reader, writer = mock.Mock(), mock.Mock() proto.app_protocol.reader, proto.app_protocol.writer = reader, writer loop_mock = mock.Mock() loop_mock.create_connection = make_mocked_coro((transp, proto)) with mock.patch('aiosocksy.asyncio.Future') as future_mock: future_mock.side_effect = make_mocked_coro(True) r, w = await aiosocksy.open_connection(addr, auth, dst, loop=loop_mock) assert reader is r assert writer is w
async def test_create_connection_init(): addr = aiosocksy.Socks5Addr('localhost') auth = aiosocksy.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) # proxy argument with pytest.raises(AssertionError) as ct: await aiosocksy.create_connection(None, None, auth, dst) assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct) with pytest.raises(AssertionError) as ct: await aiosocksy.create_connection(None, auth, auth, dst) assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct) # proxy_auth with pytest.raises(AssertionError) as ct: await aiosocksy.create_connection(None, addr, addr, dst) assert 'proxy_auth must be None or Socks4Auth()' in str(ct) # dst with pytest.raises(AssertionError) as ct: await aiosocksy.create_connection(None, addr, auth, None) assert 'invalid dst format, tuple("dst_host", dst_port))' in str(ct) # addr and auth compatibility with pytest.raises(ValueError) as ct: await aiosocksy.create_connection(None, addr, aiosocksy.Socks4Auth(''), dst) assert 'proxy is Socks5Addr but proxy_auth is not Socks5Auth' in str(ct) with pytest.raises(ValueError) as ct: await aiosocksy.create_connection(None, aiosocksy.Socks4Addr(''), auth, dst) assert 'proxy is Socks4Addr but proxy_auth is not Socks4Auth' in str(ct) # test ssl, server_hostname with pytest.raises(ValueError) as ct: await aiosocksy.create_connection(None, addr, auth, dst, server_hostname='python.org') assert 'server_hostname is only meaningful with ssl' in str(ct)
def test_socks5_auth1(): with pytest.raises(ValueError): aiosocksy.Socks5Auth(None, '')
def test_socks5_auth3(): auth = aiosocksy.Socks5Auth('usr', 'pwd', encoding='ascii') assert auth.login == b'usr' assert auth.password == b'pwd'
def test_socks5_auth4(): auth = aiosocksy.Socks5Auth('usrё', 'pwdё', encoding='utf-8') assert auth.login == b'usr\xd1\x91' assert auth.password == b'pwd\xd1\x91'
'/chats': text_dir + 'DataChats.txt', '/links': text_dir + 'DataLinks.txt', '/rules': text_dir + 'DataRules.txt', '/start': text_dir + 'DataStart.txt', '/gender': text_dir + 'DataGender.txt', '/channels': text_dir + 'DataChannels.txt', 'kek_file_ids': text_dir + 'DataKek_IDs.txt', 'chromo': gen_dir + 'chromo_count.txt', 'bot_logs': gen_dir + 'bot_logs.txt', 'last_post': gen_dir + 'last_post.txt', 'vk_config': gen_dir + 'vk_config.json', 'bot_killed': gen_dir + 'they_killed_me.txt', 'vk_last_post': gen_dir + 'vk_last_post.txt', 'kek_requests': gen_dir + 'kek_requests.txt' } gif_links = [ 'https://t.me/mechmath/127603', 'https://t.me/mechmath/257601', 'https://t.me/mechmath/257606' ] # лимит вызова /kek в час limit_kek = 60 # ID администраторов бота admin_ids = [28006241, 207275675, 217917985, 126442350, 221439208, 147100358, 258145124] PROXY_URL = tokens.socks5_url PROXY_AUTH = aiosocksy.Socks5Auth(login=tokens.socks5_login, password=tokens.socks5_password)
async def http_request(self, url, *, method="GET", json=None, timeout=10.0, verify=True, proxy=None): close_session = False proxy_auth = None proxy_url = None if isinstance(verify, str): sslcontext = ssl.create_default_context(cadata=verify) else: sslcontext = None if proxy: proxy_auth = aiosocksy.Socks5Auth(proxy["username"], proxy["password"]) connector = aiosocksy.connector.ProxyConnector( remote_resolve=False, verify_ssl=verify, ssl_context=sslcontext, ) session = aiohttp.ClientSession( connector=connector, request_class=aiosocksy.connector.ProxyClientRequest, ) proxy_url = "socks5://{host}:{port}".format_map(proxy) close_session = True elif sslcontext: conn = aiohttp.TCPConnector(ssl_context=sslcontext) session = aiohttp.ClientSession(connector=conn) close_session = True elif verify is True: session = self.http_client_v elif verify is False: session = self.http_client_no_v else: raise ValueError("invalid arguments to http_request") func = getattr(session, method.lower()) try: with async_timeout.timeout(timeout): async with func(url, json=json, proxy=proxy_url, proxy_auth=proxy_auth) as response: if response.headers.get("content-type", "").startswith("application/json"): resp_content = await response.json() else: resp_content = await response.text() result = HTTPResponse(body=resp_content, status=response.status) finally: if close_session: await session.close() return result
def test_socks5_auth2(): with pytest.raises(ValueError): aiosocksy.Socks5Auth('', None)