From cd661dd819e61955c8dd520f43964a2b3ef6a082 Mon Sep 17 00:00:00 2001 From: emdee Date: Sat, 8 Oct 2022 18:01:52 +0000 Subject: [PATCH] trying to fix nodes parsing --- wrapper/__init__.py | 2 +- wrapper/tox.py | 94 +++++++---- wrapper_tests/support_testing.py | 261 ++++++------------------------- wrapper_tests/tests_wrapper.py | 35 ++--- 4 files changed, 127 insertions(+), 265 deletions(-) diff --git a/wrapper/__init__.py b/wrapper/__init__.py index f8ea22b..2b8d997 100644 --- a/wrapper/__init__.py +++ b/wrapper/__init__.py @@ -1,5 +1,5 @@ # -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*- -# You need a libs directory beside this directory +# You need a libs directory beside this directory # and you need to link your libtoxcore.so and libtoxav.so # and libtoxencryptsave.so into ../libs/ # Link all 3 to libtoxcore.so if you have only libtoxcore.so diff --git a/wrapper/tox.py b/wrapper/tox.py index ece67fb..ed1b6e7 100644 --- a/wrapper/tox.py +++ b/wrapper/tox.py @@ -11,11 +11,19 @@ except: from toxav import ToxAV from libtox import LibToxCore +# callbacks can be called in any thread so were being careful +# tox.py can be called by callbacks def LOG_ERROR(a): print('EROR> '+a) def LOG_WARN(a): print('WARN> '+a) -def LOG_INFO(a): print('INFO> '+a) -def LOG_DEBUG(a): print('DBUG> '+a) -def LOG_TRACE(a): pass # print('TRAC> '+a) +def LOG_INFO(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 20 + if bVERBOSE: print('INFO> '+a) +def LOG_DEBUG(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 10-1 + if bVERBOSE: print('DBUG> '+a) +def LOG_TRACE(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel < 10 + if bVERBOSE: print('TRAC> '+a) global aTIMES aTIMES=dict() @@ -57,7 +65,6 @@ class ToxOptions(Structure): ] - class GroupChatSelfPeerInfo(Structure): _fields_ = [ ('nick', c_char_p), @@ -405,6 +412,9 @@ class Tox: # Internal client information (Tox address/id) # ----------------------------------------------------------------------------------------------------------------- + def self_get_toxid(self, address=None): + return self_get_address(self, address=None) + def self_get_address(self, address=None): """ Writes the Tox friend address of the client to a byte array. The address is not in human-readable format. If a @@ -832,7 +842,7 @@ class Tox: The return value is equal to the `length` argument received by the last `friend_name` callback. """ tox_err_friend_query = c_int() - LOG_DEBUG(f"tox_friend_get_name_size") + LOG_TRACE(f"tox_friend_get_name_size") result = Tox.libtoxcore.tox_friend_get_name_size(self._tox_pointer, c_uint32(friend_number), byref(tox_err_friend_query)) @@ -907,7 +917,7 @@ class Tox: :return: length of the friend's status message """ tox_err_friend_query = c_int() - LOG_DEBUG(f"tox_friend_get_status_message_size") + LOG_TRACE(f"tox_friend_get_status_message_size") result = Tox.libtoxcore.tox_friend_get_status_message_size(self._tox_pointer, c_uint32(friend_number), byref(tox_err_friend_query)) tox_err_friend_query = tox_err_friend_query.value @@ -1865,15 +1875,22 @@ class Tox: else: nick_length = len(nick) cnick = c_char_p(nick) - result = Tox.libtoxcore.tox_group_new(self._tox_pointer, privacy_state, + result = Tox.libtoxcore.tox_group_new(self._tox_pointer, + privacy_state, group_name, len(group_name), - cnick, nick_length, + cnick, + nick_length, byref(error)) if error.value: - LOG_ERROR(f"group_new {error.value}") - raise RuntimeError("group_new {error.value}") + # -1 TOX_ERR_GROUP_NEW_TOO_LONG + # -2 TOX_ERR_GROUP_NEW_EMPTY + # -3 TOX_ERR_GROUP_NEW_INIT + # -4 TOX_ERR_GROUP_NEW_STATE + # -5 TOX_ERR_GROUP_NEW_ANNOUNCE + LOG_ERROR(f"group_new {error.value} {TOX_ERR_GROUP_NEW[error.value]}") + raise RuntimeError(f"group_new {error.value}") return result def group_join(self, chat_id, password, nick, status): @@ -2021,7 +2038,7 @@ class Tox: """ error = c_int() - LOG_DEBUG(f"tox_group_self_get_name_size") + LOG_TRACE(f"tox_group_self_get_name_size") result = Tox.libtoxcore.tox_group_self_get_name_size(self._tox_pointer, group_number, byref(error)) if error.value: LOG_ERROR(f"group_self_get_name_size {error.value}") @@ -2313,8 +2330,8 @@ class Tox: """ error = c_int() + LOG_TRACE(f"tox_group_get_topic_size") try: - LOG_DEBUG(f"tox_group_get_topic_size") result = Tox.libtoxcore.tox_group_get_topic_size(self._tox_pointer, group_number, byref(error)) except Exception as e: LOG_WARN(f" Exception {e}") @@ -2323,7 +2340,6 @@ class Tox: if error.value: LOG_ERROR(f" {error.value}") raise RuntimeError(f" {error.value}") - LOG_DEBUG(f"tox_group_get_topic_size") return result def group_get_topic(self, group_number): @@ -2352,7 +2368,6 @@ class Tox: return value is unspecified. """ error = c_int() - LOG_DEBUG(f"tox_group_get_name_size") result = Tox.libtoxcore.tox_group_get_name_size(self._tox_pointer, group_number, byref(error)) if error.value: LOG_ERROR(f" {error.value}") @@ -2385,14 +2400,23 @@ class Tox: :return chat id. """ + LOG_INFO(f"tox_group_get_id group_number={group_number}") error = c_int() buff = create_string_buffer(TOX_GROUP_CHAT_ID_SIZE) result = Tox.libtoxcore.tox_group_get_chat_id(self._tox_pointer, group_number, buff, byref(error)) if error.value: - LOG_ERROR(f"tox_group_get_chat_id {error.value}") - raise RuntimeError(f" {error.value}") + if error.value == 1: + LOG_ERROR(f"tox_group_get_chat_id ERROR GROUP_STATE_QUERIES_GROUP_NOT_FOUND group_number={group_number}") + else: + LOG_ERROR(f"tox_group_get_chat_id group_number={group_number} {error.value}") + raise RuntimeError(f"tox_group_get_chat_id {error.value}") +# +# QObject::setParent: Cannot set parent, new parent is in a different thread +# QObject::installEventFilter(): Cannot filter events for objects in a different thread. +# QBasicTimer::start: Timers cannot be started from another thread + LOG_TRACE(f"tox_group_get_chat_id") return bin_to_string(buff, TOX_GROUP_CHAT_ID_SIZE) @@ -2461,7 +2485,7 @@ class Tox: """ error = c_int() - LOG_DEBUG(f"tox_group_get_password_size") + LOG_TRACE(f"tox_group_get_password_size") result = Tox.libtoxcore.tox_group_get_password_size(self._tox_pointer, group_number, byref(error)) if error.value: LOG_ERROR(f" {error.value}") @@ -2767,7 +2791,7 @@ class Tox: # result = f(byref(error)) # return result - def group_invite_accept(self, invite_data, friend_number, nick, status, password=None): + def group_invite_accept(self, invite_data, friend_number, nick, status, password=''): """ Accept an invite to a group chat that the client previously received from a friend. The invite is only valid while the inviter is present in the group. @@ -2780,21 +2804,38 @@ class Tox: error = c_int() f = Tox.libtoxcore.tox_group_invite_accept f.restype = c_uint32 - nick = bytes(nick, 'utf-8') - invite_data = bytes(invite_data, 'utf-8') + try: + nick = bytes(nick, 'utf-8') + except: + nick = b'' + try: + if password is None: password = b'' + password = bytes(password, 'utf-8') + except: + password = b'' + invite_data = invite_data or b'' if False: # API change peer_info = self.group_self_peer_info_new() peer_info.contents.nick = c_char_p(nick) peer_info.contents.nick_length = len(nick) peer_info.contents.user_status = status - result = f(self._tox_pointer, c_uint32(friend_number), invite_data, len(invite_data), - nick, len(nick), - password, len(password) if password is not None else 0, - byref(error)) + LOG_INFO(f"group_invite_accept friend_number={friend_number} nick={nick} {invite_data}") + try: + assert type(invite_data) == bytes + result = f(self._tox_pointer, + c_uint32(friend_number), + invite_data, len(invite_data), + c_char_p(nick), len(nick), + c_char_p(password), len(password) if password is not None else 0, + byref(error)) + except Exception as e: + LOG_ERROR(f"group_invite_accept ERROR {e}") + raise if error.value: - LOG_ERROR(f" {error.value}") - raise RuntimeError(f" {error.value}") + # The invite data is not in the expected format. + LOG_ERROR(f"group_invite_accept {TOX_ERR_GROUP_INVITE_ACCEPT[error.value]}") + raise RuntimeError(f"group_invite_accept {TOX_ERR_GROUP_INVITE_ACCEPT[error.value]} {error.value}") return result def callback_group_invite(self, callback, user_data): @@ -2815,7 +2856,6 @@ class Tox: Tox.libtoxcore.tox_callback_group_invite(self._tox_pointer, POINTER(None)()) self.group_invite_cb = None return - LOG_DEBUG(f"tox_callback_group_invite") c_callback = CFUNCTYPE(None, c_void_p, c_uint32, POINTER(c_uint8), c_size_t, POINTER(c_uint8), c_size_t, c_void_p) self.group_invite_cb = c_callback(callback) diff --git a/wrapper_tests/support_testing.py b/wrapper_tests/support_testing.py index 6edc7a7..3c65dd8 100644 --- a/wrapper_tests/support_testing.py +++ b/wrapper_tests/support_testing.py @@ -5,6 +5,7 @@ import sys import argparse import re import logging +import shutil import json from ctypes import * import time, contextlib @@ -72,185 +73,12 @@ lBOOLEANS = [ 'core_logging', ] +sDIR = os.environ.get('TMPDIR', '/tmp') +sTOX_VERSION = "1000002018" +bHAVE_NMAP = shutil.which('nmap') +bHAVE_JQ = shutil.which('jq') +bHAVE_BASH = shutil.which('bash') -lNEW = [ # ngc_jfreeg2: - ('104.244.74.69', 38445, # tox.plastiras.org - '5E47BA1DC3913EB2CBF2D64CE4F23D8BFE5391BFABE5C43C5BAD13F0A414CD77'), - ('172.93.52.70', 33445, - '79CADA4974B0926F286F025CD5FFDF3E654A3758C53E0273ECFC4D12C21DCA48'), - ] - -lGOOD = [ - ("95.79.50.56", - 33445, - "8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832"), - ("85.143.221.42", - 33445, - "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"), - ("tox.verdict.gg", - 33445, - "1C5293AEF2114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976"), - ("tox.initramfs.io", - 33445, - "3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25"), - ("144.217.167.73", - 33445, - "7E5668E0EE09E19F320AD47902419331FFEE147BB3606769CFBE921A2A2FD34C"), - ("tox.abilinski.com", - 33445, - "10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E"), - ("tox.novg.net", - 33445, - "D527E5847F8330D628DAB1814F0A422F6DC9D0A300E6C357634EE2DA88C35463"), - ("198.199.98.108", - 33445, - "BEF0CFB37AF874BD17B9A8F9FE64C75521DB95A37D33C5BDB00E9CF58659C04F"), - ('82.196.15.215', 33445, # tox.kurnevsky.net - "82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23"), - ("81.169.136.229", - 33445, - "E0DB78116AC6500398DDBA2AEEF3220BB116384CAB714C5D1FCD61EA2B69D75E"), - ("tox2.abilinski.com", - 33445, - "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D"), - ("bg.tox.dcntrlzd.network", - 33445, - "20AD2A54D70E827302CDF5F11D7C43FA0EC987042C36628E64B2B721A1426E36"), - ("46.101.197.175", - 33445, - "CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707"), - ("tox1.mf-net.eu", - 33445, - "B3E5FA80DC8EBD1149AD2AB35ED8B85BD546DEDE261CA593234C619249419506"), - ("tox2.mf-net.eu", - 33445, - "70EA214FDE161E7432530605213F18F7427DC773E276B3E317A07531F548545F"), - ("195.201.7.101", - 33445, - "B84E865125B4EC4C368CD047C72BCE447644A2DC31EF75BD2CDA345BFD310107"), - ("gt.sot-te.ch", - 33445, - "F4F4856F1A311049E0262E9E0A160610284B434F46299988A9CB42BD3D494618"), - ("5.19.249.240", - 38296, - "DA98A4C0CD7473A133E115FEA2EBDAEEA2EF4F79FD69325FC070DA4DE4BA3238"), - ("188.225.9.167", - 33445, - "1911341A83E02503AB1FD6561BD64AF3A9D6C3F12B5FBB656976B2E678644A67"), - ("122.116.39.151", - 33445, - "5716530A10D362867C8E87EE1CD5362A233BAFBBA4CF47FA73B7CAD368BD5E6E"), - ("195.123.208.139", - 33445, - "534A589BA7427C631773D13083570F529238211893640C99D1507300F055FE73"), - ("104.225.141.59", - 43334, - "933BA20B2E258B4C0D475B6DECE90C7E827FE83EFA9655414E7841251B19A72C"), - ("139.162.110.188", - 33445, - "F76A11284547163889DDC89A7738CF271797BF5E5E220643E97AD3C7E7903D55"), - ("198.98.49.206", - 33445, - "28DB44A3CEEE69146469855DFFE5F54DA567F5D65E03EFB1D38BBAEFF2553255"), - ("172.105.109.31", - 33445, - "D46E97CF995DC1820B92B7D899E152A217D36ABE22730FEA4B6BF1BFC06C617C"), - ("ru.tox.dcntrlzd.network", - 33445, - "DBB2E896990ECC383DA2E68A01CA148105E34F9B3B9356F2FE2B5096FDB62762"), - ("91.146.66.26", - 33445, - "B5E7DAC610DBDE55F359C7F8690B294C8E4FCEC4385DE9525DBFA5523EAD9D53"), - ("tox01.ky0uraku.xyz", - 33445, - "FD04EB03ABC5FC5266A93D37B4D6D6171C9931176DC68736629552D8EF0DE174"), - ("tox02.ky0uraku.xyz", - 33445, - "D3D6D7C0C7009FC75406B0A49E475996C8C4F8BCE1E6FC5967DE427F8F600527"), - ("tox.plastiras.org", - 33445, - "8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725"), - ("141.95.108.234", - 33445, - "2DEF3156812324B1593A6442C937EAE0A8BD98DE529D2D4A7DD4BA6CB3ECF262"), - ("kusoneko.moe", - 33445, - "BE7ED53CD924813507BA711FD40386062E6DC6F790EFA122C78F7CDEEE4B6D1B"), - ("tox2.plastiras.org", - 33445, - "B6626D386BE7E3ACA107B46F48A5C4D522D29281750D44A0CBA6A2721E79C951"), - ("tox.plastiras.org", - 33445, - "8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725"), - ("141.95.108.234", - 33445, - "2DEF3156812324B1593A6442C937EAE0A8BD98DE529D2D4A7DD4BA6CB3ECF262"), - ("kusoneko.moe", - 33445, - "BE7ED53CD924813507BA711FD40386062E6DC6F790EFA122C78F7CDEEE4B6D1B"), - ('84.22.115.205', 33445, # tox.verdict.gg - '1C5293AEF2114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976'), -] - -lRELAYS = [ - ("tox02.ky0uraku.xyz",33445, "D3D6D7C0C7009FC75406B0A49E475996C8C4F8BCE1E6FC5967DE427F8F600527"), - ("tox.plastiras.org", 443, "8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725"), - ("46.101.197.175", 33445, "CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707"), - ("122.116.39.151", 3389, "5716530A10D362867C8E87EE1CD5362A233BAFBBA4CF47FA73B7CAD368BD5E6E"), - ("172.105.109.31", 33445, "D46E97CF995DC1820B92B7D899E152A217D36ABE22730FEA4B6BF1BFC06C617C"), - ("178.62.250.138", 33445, "788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B"), - ("198.46.138.44", 33445, "F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67"), - ("51.15.37.145", 33445, "6FC41E2BD381D37E9748FC0E0328CE086AF9598BECC8FEB7DDF2E440475F300E"), - ("130.133.110.14", 33445, "461FA3776EF0FA655F1A05477DF1B3B614F7D6B124F7DB1DD4FE3C08B03B640F"), - ("198.98.51.198", 33445, "1D5A5F2F5D6233058BF0259B09622FB40B482E4FA0931EB8FD3AB8E7BF7DAF6F"), - ("108.61.165.198", 33445, "8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832"), - ("185.25.116.107", 33445, "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43"), - ("5.189.176.217", 5190, "2B2137E094F743AC8BD44652C55F41DFACC502F125E99E4FE24D40537489E32F"), - ("217.182.143.254", 2306, "7AED21F94D82B05774F697B209628CD5A9AD17E0C073D9329076A4C28ED28147"), - ("104.223.122.15", 33445, "0FB96EEBFB1650DDB52E70CF773DDFCABE25A95CC3BB50FC251082E4B63EF82A"), - ("d4rk4.ru", 1813, "53737F6D47FA6BD2808F378E339AF45BF86F39B64E79D6D491C53A1D522E7039"), - ("104.233.104.126", 33445, "EDEE8F2E839A57820DE3DA4156D88350E53D4161447068A3457EE8F59F362414"), - ("51.254.84.212", 33445, "AEC204B9A4501412D5F0BB67D9C81B5DB3EE6ADA64122D32A3E9B093D544327D"), - ("88.99.133.52", 33445, "2D320F971EF2CA18004416C2AAE7BA52BF7949DB34EA8E2E21AF67BD367BE211"), - ("185.58.206.164", 33445, "24156472041E5F220D1FA11D9DF32F7AD697D59845701CDD7BE7D1785EB9DB39"), - ("92.54.84.70", 33445, "5625A62618CB4FCA70E147A71B29695F38CC65FF0CBD68AD46254585BE564802"), - ("195.93.190.6", 33445, "FB4CE0DDEFEED45F26917053E5D24BDDA0FA0A3D83A672A9DA2375928B37023D"), - ("tox.uplinklabs.net", 33445, "1A56EA3EDF5DF4C0AEABBF3C2E4E603890F87E983CAC8A0D532A335F2C6E3E1F"), - ("95.215.44.78", 33445, "672DBE27B4ADB9D5FB105A6BB648B2F8FDB89B3323486A7A21968316E012023C"), - ("163.172.136.118", 33445, "2C289F9F37C20D09DA83565588BF496FAB3764853FA38141817A72E3F18ACA0B"), - ("sorunome.de", 33445, "02807CF4F8BB8FB390CC3794BDF1E8449E9A8392C5D3F2200019DA9F1E812E46"), - ("37.97.185.116", 33445, "E59A0E71ADA20D35BD1B0957059D7EF7E7792B3D680AE25C6F4DBBA09114D165"), - ("193.124.186.205", 5228, "9906D65F2A4751068A59D30505C5FC8AE1A95E0843AE9372EAFA3BAB6AC16C2C"), - ("80.87.193.193", 33445, "B38255EE4B054924F6D79A5E6E5889EC94B6ADF6FE9906F97A3D01E3D083223A"), - ("hibiki.eve.moe", 33445, "D3EB45181B343C2C222A5BCF72B760638E15ED87904625AAD351C594EEFAE03E"), - ("46.229.52.198", 33445, "813C8F4187833EF0655B10F7752141A352248462A567529A38B6BBF73E979307"), - ("144.217.86.39", 33445, "7E5668E0EE09E19F320AD47902419331FFEE147BB3606769CFBE921A2A2FD34C"), - ("77.37.160.178", 33440, "CE678DEAFA29182EFD1B0C5B9BC6999E5A20B50A1A6EC18B91C8EBB591712416"), - ("85.21.144.224", 33445, "8F738BBC8FA9394670BCAB146C67A507B9907C8E564E28C2B59BEBB2FF68711B"), - ("95.215.46.114", 33445, "5823FB947FF24CF83DDFAC3F3BAA18F96EA2018B16CC08429CB97FA502F40C23"), -] - -lBAD = [('xxx.garbage.zzz', 33445, - '0123456789114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976',), - ('www.google.com', 33445, # no port - '0123456789114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976',), - ('www.google.com', 80, # no ToX port - '0123456789114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976',)] - -lLOCAL = [# /etc/init.d/tox-bootstrapd.conf - ('127.0.0.1', 33446, - 'EC8F7405F79F281569B6C66D9F03490973AB99BC9175C44FBEF4C3428A63B80D'), - ('127.0.0.1', 33447, - 'EC8F7405F79F281569B6C66D9F03490973AB99BC9175C44FBEF4C3428A63B80D'), - ('127.0.0.1', 3389, - 'EC8F7405F79F281569B6C66D9F03490973AB99BC9175C44FBEF4C3428A63B80D'), - # /var/local/src/c-toxcore/build/DHT_bootstrap - ('127.0.0.1', 33445, - '6DB2DEA1785F4A7AEAB52F1F4BC90D6266FBC375DEF316DED59F4E814F97F438'), - # /var/local/src/toxcore/build/DHT_bootstrap - ('127.0.0.1', 33445, - 'A22E68642917F424E5B38E98CACE38A4906B67228D83E507084400B597D5722E'), - ] def assert_main_thread(): # this "instance" method is very useful! @@ -511,27 +339,28 @@ def lSdSamplerates(iDev): def _get_nodes_path(oArgs=None): if oArgs and hasattr(oArgs, 'nodes_json') and oArgs.nodes_json: LOG.debug("_get_nodes_path: " +oArgs.nodes_json) - return oArgs.nodes_json - default = os.path.join(get_user_config_path(), 'toxygen_nodes.json') + default = oArgs.nodes_json + elif get_user_config_path: + default = os.path.join(get_user_config_path(), 'toxygen_nodes.json') + else: + # Windwoes + default = os.path.join(os.getenv('HOME'), '.config', 'tox', 'toxygen_nodes.json') LOG.debug("_get_nodes_path: " +default) return default -DEFAULT_NODES_COUNT = 4 +DEFAULT_NODES_COUNT = 8 def generate_nodes( oArgs=None, nodes_count=DEFAULT_NODES_COUNT, ipv='ipv4', udp_not_tcp=True): - if oArgs is None: - # Windwoes - sFile = os.path.join(os.getenv('HOME'), '.config', 'tox', 'DHTnodes.json') - else: - sFile = _get_nodes_path(oArgs=oArgs) + sFile = _get_nodes_path(oArgs=oArgs) return generate_nodes_from_file(sFile, nodes_count=nodes_count, ipv=ipv, udp_not_tcp=udp_not_tcp) +aNODES_CACHE = {} def generate_nodes_from_file(sFile, nodes_count=DEFAULT_NODES_COUNT, ipv='ipv4', @@ -540,35 +369,41 @@ def generate_nodes_from_file(sFile, """https://github.com/TokTok/c-toxcore/issues/469 I had a conversation with @irungentoo on IRC about whether we really need to call tox_bootstrap() when having UDP disabled and why. The answer is yes, because in addition to TCP relays (tox_add_tcp_relay()), toxcore also needs to know addresses of UDP onion nodes in order to work correctly. The DHT, however, is not used when UDP is disabled. tox_bootstrap() function resolves the address passed to it as argument and calls onion_add_bs_node_path() and DHT_bootstrap() functions. Although calling DHT_bootstrap() is not really necessary as DHT is not used, we still need to resolve the address of the DHT node in order to populate the onion routes with onion_add_bs_node_path() call. """ - if not os.path.exists(sFile): - LOG.error("generate_nodes_from_file file not found " +sFile) - return [] - try: - with open(sFile, 'rt') as fl: - json_nodes = json.loads(fl.read())['nodes'] - except Exception as e: - LOG.error(f"generate_nodes_from_file error {sFile}\n{e}") - return [] + key = sFile +',' +ipv + key += ',0' if udp_not_tcp else ',1' + if key in aNODES_CACHE: + sorted_nodes = aNODES_CACHE[key] else: - LOG.info("generate_nodes_from_file " +sFile) + if not os.path.exists(sFile): + LOG.error("generate_nodes_from_file file not found " +sFile) + return [] + try: + with open(sFile, 'rt') as fl: + json_nodes = json.loads(fl.read())['nodes'] + except Exception as e: + LOG.error(f"generate_nodes_from_file error {sFile}\n{e}") + return [] + else: + LOG.debug("generate_nodes_from_file " +sFile) - if udp_not_tcp: - nodes = [(node[ipv], node['port'], node['public_key'],) for - node in json_nodes if node[ipv] != 'NONE' \ - and node["status_udp"] in [True, "true"] - ] - else: - nodes = [] - elts = [(node[ipv], node['tcp_ports'], node['public_key'],) \ - for node in json_nodes if node[ipv] != 'NONE' \ - and node['last_ping'] > 0 - and node["status_tcp"] in [True, "true"] - ] - for (ipv4, ports, public_key,) in elts: - for port in ports: - nodes += [(ipv4, port, public_key)] - sorted_nodes = sorted(nodes) - random.shuffle(sorted_nodes) + if udp_not_tcp: + nodes = [(node[ipv], node['port'], node['public_key'],) for + node in json_nodes if node[ipv] != 'NONE' \ + and node["status_udp"] in [True, "true"] + ] + else: + nodes = [] + elts = [(node[ipv], node['tcp_ports'], node['public_key'],) \ + for node in json_nodes if node[ipv] != 'NONE' \ + and node['last_ping'] > 0 + and node["status_tcp"] in [True, "true"] + ] + for (ipv4, ports, public_key,) in elts: + for port in ports: + nodes += [(ipv4, port, public_key)] + sorted_nodes = sorted(nodes) + aNODES_CACHE[key] = sorted_nodes + if nodes_count is not None and len(sorted_nodes) > nodes_count: sorted_nodes = sorted_nodes[-nodes_count:] LOG.debug(f"generate_nodes_from_file {sFile} len={len(sorted_nodes)}") diff --git a/wrapper_tests/tests_wrapper.py b/wrapper_tests/tests_wrapper.py index 35bf0e3..e9677a6 100644 --- a/wrapper_tests/tests_wrapper.py +++ b/wrapper_tests/tests_wrapper.py @@ -72,10 +72,8 @@ from wrapper.toxcore_enums_and_consts import TOX_CONNECTION, TOX_USER_STATUS, \ try: import support_testing as ts - from support_testing import lGOOD, lLOCAL except ImportError: import wrapper_tests.support_testing as ts - from wrapper_tests.support_testing import lGOOD, lLOCAL try: from toxygen_tests import test_sound_notification @@ -272,22 +270,17 @@ class ToxSuite(unittest.TestCase): if not bIS_LOCAL and not ts.bAreWeConnected(): LOG.warn(f"prepare not local and NOT CONNECTED") - self.lUdp = [] - self.lTcp = [] - if oTOX_OARGS.network in ['newlocal', 'localnew']: - oTOX_OARGS.network = 'newlocal' - self.lUdp = lLOCAL - self.bTest = True - elif oTOX_OARGS.network in ['test', 'new']: - self.lUdp = ts.lNEW - self.bTest = True - else: - self.bTest = False - self.lUdp = ts.lGOOD - if oTOX_OARGS.proxy_port > 0: - self.lTcp = ts.lRELAYS - else: - self.lUdp = lGOOD + self.lUdp = ts.generate_nodes( + oArgs=oTOX_OARGS, + nodes_count=8, + ipv='ipv4', + udp_not_tcp=True) + + self.lTcp = ts.generate_nodes( + oArgs=oTOX_OARGS, + nodes_count=8, + ipv='ipv4', + udp_not_tcp=False) def get_connection_status(self): # if not self.connected @@ -733,14 +726,8 @@ class ToxSuite(unittest.TestCase): def test_tests_start(self): # works LOG.info("test_tests_start " ) - global lLOCAL port = ts.tox_bootstrapd_port() - if port: - elt = list(lLOCAL[0]) - elt[1] = port - lLOCAL.insert(0,elt) - assert len(self.bob._address) == 2*TOX_ADDRESS_SIZE, len(self.bob._address) assert len(self.alice._address) == 2*TOX_ADDRESS_SIZE, \ len(self.alice._address)