This commit is contained in:
emdee 2022-10-13 14:15:18 +00:00
parent bccd262a68
commit 4e44a559e7
3 changed files with 49 additions and 32 deletions

View File

@ -27,7 +27,7 @@ def LOG_TRACE(a):
UINT32_MAX = 2 ** 32 -1 UINT32_MAX = 2 ** 32 -1
class ToxError(RuntimeError): pass class ToxError(RuntimeError): pass
global aTIMES global aTIMES
aTIMES=dict() aTIMES=dict()
def bTooSoon(key, sSlot, fSec=10.0): def bTooSoon(key, sSlot, fSec=10.0):
@ -2415,7 +2415,7 @@ class Tox:
else: else:
LOG_ERROR(f"tox_group_get_chat_id group_number={group_number} {error.value}") LOG_ERROR(f"tox_group_get_chat_id group_number={group_number} {error.value}")
raise ToxError(f"tox_group_get_chat_id {error.value}") raise ToxError(f"tox_group_get_chat_id {error.value}")
# #
# QObject::setParent: Cannot set parent, new parent is in a different thread # QObject::setParent: Cannot set parent, new parent is in a different thread
# QObject::installEventFilter(): Cannot filter events for objects in a different thread. # QObject::installEventFilter(): Cannot filter events for objects in a different thread.
# QBasicTimer::start: Timers cannot be started from another thread # QBasicTimer::start: Timers cannot be started from another thread
@ -2657,8 +2657,8 @@ class Tox:
message_type, message, message_type, message,
len(message), byref(error)) len(message), byref(error))
if error.value: if error.value:
LOG_ERROR(f" {error.value}") LOG_ERROR(f"group_send_private_message {TOX_ERR_GROUP_SEND_PRIVATE_MESSAGE[error.value]}")
raise ToxError(f" {error.value}") raise ToxError(f"group_send_private_message {TOX_ERR_GROUP_SEND_PRIVATE_MESSAGE[error.value]}")
return result return result
def group_send_message(self, group_number, type, message): def group_send_message(self, group_number, type, message):

View File

@ -7,6 +7,7 @@ import re
import logging import logging
import shutil import shutil
import json import json
import socket
from ctypes import * from ctypes import *
import time, contextlib import time, contextlib
import unittest import unittest
@ -92,7 +93,7 @@ lDEAD_BS = [
# at 3 different places. Giving up. # at 3 different places. Giving up.
'104.244.74.69', '104.244.74.69',
'172.93.52.70', '172.93.52.70',
'tox2.abilinski.com', 'tox.abilinski.com',
# Failed to resolve "tox3.plastiras.org". # Failed to resolve "tox3.plastiras.org".
"tox3.plastiras.org", "tox3.plastiras.org",
] ]
@ -368,15 +369,21 @@ def _get_nodes_path(oArgs=None):
DEFAULT_NODES_COUNT = 8 DEFAULT_NODES_COUNT = 8
def generate_nodes( global aNODES
oArgs=None, aNODES = {}
def generate_nodes(oArgs=None,
nodes_count=DEFAULT_NODES_COUNT, nodes_count=DEFAULT_NODES_COUNT,
ipv='ipv4', ipv='ipv4',
udp_not_tcp=True): udp_not_tcp=True):
global aNODES
sKey = ipv
sKey += ',0' if udp_not_tcp else ',1'
if sKey in aNODES: return aNODES[sKey]
sFile = _get_nodes_path(oArgs=oArgs) sFile = _get_nodes_path(oArgs=oArgs)
return generate_nodes_from_file(sFile, aNODES[sKey] = generate_nodes_from_file(sFile,
nodes_count=nodes_count, nodes_count=nodes_count,
ipv=ipv, udp_not_tcp=udp_not_tcp) ipv=ipv, udp_not_tcp=udp_not_tcp)
return aNODES[sKey]
aNODES_CACHE = {} aNODES_CACHE = {}
def generate_nodes_from_file(sFile, def generate_nodes_from_file(sFile,
@ -457,23 +464,28 @@ def bootstrap_good(lelts, lToxes):
for largs in lelts: for largs in lelts:
host, port, key = largs host, port, key = largs
if largs[0] in lDEAD_BS: continue if largs[0] in lDEAD_BS: continue
try:
host = socket.gethostbyname(largs[0])
except:
continue
assert len(key) == 64, key assert len(key) == 64, key
if type(port) == str: if type(port) == str:
port = int(port) port = int(port)
try: try:
oRet = elt.bootstrap(largs[0], oRet = elt.bootstrap(host,
port, port,
largs[2]) largs[2])
except Exception as e: except Exception as e:
LOG.error('bootstrap to ' +largs[0] +':' +str(largs[1]) \ LOG.error('bootstrap to ' +host +':' +str(largs[1]) \
+' ' +str(e)) +' ' +str(e))
continue continue
if not oRet: if not oRet:
LOG.warn('bootstrap failed to ' +largs[0] +' : ' +str(oRet)) LOG.warn('bootstrap failed to ' +host +' : ' +str(oRet))
elif elt.self_get_connection_status() != TOX_CONNECTION['NONE']:
LOG.info('bootstrap to ' +host +' connected')
break
else: else:
if elt.self_get_connection_status() != TOX_CONNECTION['NONE']: LOG.debug('bootstrap to ' +host +' not connected')
LOG.debug('bootstrap to ' +largs[0] +' connected')
return
def bootstrap_tcp(lelts, lToxes): def bootstrap_tcp(lelts, lToxes):
LOG.info('bootstraping tcp') LOG.info('bootstraping tcp')
@ -481,7 +493,11 @@ def bootstrap_tcp(lelts, lToxes):
for largs in lelts: for largs in lelts:
if largs[0] in lDEAD_BS: continue if largs[0] in lDEAD_BS: continue
try: try:
oRet = elt.add_tcp_relay(largs[0], host = socket.gethostbyname(largs[0])
except:
continue
try:
oRet = elt.add_tcp_relay(host,
int(largs[1]), int(largs[1]),
largs[2]) largs[2])
except Exception as e: except Exception as e:
@ -489,11 +505,12 @@ def bootstrap_tcp(lelts, lToxes):
continue continue
if not oRet: if not oRet:
LOG.warn('bootstrap_tcp failed to ' +largs[0] +' : ' +str(oRet)) LOG.warn('bootstrap_tcp failed to ' +largs[0] +' : ' +str(oRet))
elif elt.self_get_connection_status() != TOX_CONNECTION['NONE']:
LOG.info('bootstrap_tcp to ' +largs[0] +' connected')
break
else: else:
if elt.self_get_connection_status() != TOX_CONNECTION['NONE']: LOG.debug('bootstrap_tcp to ' +largs[0] +' not connected')
LOG.debug('bootstrap_tcp to ' +largs[0] +' connected')
break
def setup_logging(oArgs): def setup_logging(oArgs):
global LOG global LOG
if coloredlogs: if coloredlogs:

View File

@ -122,14 +122,14 @@ if not hasattr(unittest, 'skip'):
return _wrap1 return _wrap1
unittest.skip = unittest_skip unittest.skip = unittest_skip
def iNodeInfo(sProt, sHost, sPort, key=None, environ=None, bTest=False): def iNmapInfo(sProt, sHost, sPort, key=None, environ=None, bTest=False):
sFile = os.path.join("/tmp", f"{sHost}.{os.getpid()}.nmap") sFile = os.path.join("/tmp", f"{sHost}.{os.getpid()}.nmap")
if sProt in ['socks', 'socks5', 'tcp4']: if sProt in ['socks', 'socks5', 'tcp4']:
cmd = f"nmap -Pn -n -sT -p T:{sPort} {sHost} | grep /tcp >{sFile}" cmd = f"nmap -Pn -n -sT -p T:{sPort} {sHost} | grep /tcp >{sFile}"
else: else:
cmd = f"nmap -Pn -n -sU -p U:{sPort} {sHost} | grep /tcp >{sFile}" cmd = f"nmap -Pn -n -sU -p U:{sPort} {sHost} | grep /tcp >{sFile}"
iRet = os.system(cmd) iRet = os.system(cmd)
LOG.debug(f"iNodeInfo cmd={cmd} {iRet}") LOG.debug(f"iNmapInfo cmd={cmd} {iRet}")
if iRet != 0: if iRet != 0:
return iRet return iRet
assert os.path.exists(sFile), sFile assert os.path.exists(sFile), sFile
@ -137,12 +137,12 @@ def iNodeInfo(sProt, sHost, sPort, key=None, environ=None, bTest=False):
l = oFd.readlines() l = oFd.readlines()
assert len(l) assert len(l)
s = '\n'.join([s.strip() for s in l]) s = '\n'.join([s.strip() for s in l])
LOG.debug(f"iNodeInfo: {s}") LOG.debug(f"iNmapInfo: {s}")
return 0 return 0
def bootstrap_iNodeInfo(lElts): def bootstrap_iNmapInfo(lElts):
if not bIS_LOCAL and not ts.bAreWeConnected(): if not bIS_LOCAL and not ts.bAreWeConnected():
LOG.warn(f"bootstrap_iNodeInfo not local and NOT CONNECTED") LOG.warn(f"bootstrap_iNmapInfo not local and NOT CONNECTED")
return True return True
env = dict() env = dict()
if oTOX_OARGS.proxy_type == 2: if oTOX_OARGS.proxy_type == 2:
@ -157,12 +157,12 @@ def bootstrap_iNodeInfo(lElts):
if elts[0] in ts.lDEAD_BS: continue if elts[0] in ts.lDEAD_BS: continue
iRet = -1 iRet = -1
try: try:
iRet = iNodeInfo(protocol, *elts) iRet = iNmapInfo(protocol, *elts)
if iRet != 0: if iRet != 0:
LOG.warn('iNodeInfo to ' +repr(elts[0]) +' retval=' +str(iRet)) LOG.warn('iNmapInfo to ' +repr(elts[0]) +' retval=' +str(iRet))
lRetval += [False] lRetval += [False]
else: else:
LOG.info(f'bootstrap_iNodeInfo ' LOG.info(f'bootstrap_iNmapInfo '
+f" net={oTOX_OARGS.network}" +f" net={oTOX_OARGS.network}"
+f" prot={protocol}" +f" prot={protocol}"
+f" proxy={oTOX_OARGS.proxy_type}" +f" proxy={oTOX_OARGS.proxy_type}"
@ -170,7 +170,7 @@ def bootstrap_iNodeInfo(lElts):
) )
lRetval += [True] lRetval += [True]
except Exception as e: except Exception as e:
LOG.error('iNodeInfo to ' +repr(elts[0]) +' : ' +str(e) \ LOG.error('iNmapInfo to ' +repr(elts[0]) +' : ' +str(e) \
+'\n' + traceback.format_exc()) +'\n' + traceback.format_exc())
lRetval += [False] lRetval += [False]
return any(lRetval) return any(lRetval)
@ -806,7 +806,7 @@ class ToxSuite(unittest.TestCase):
LOG.warn(f"bootstrap_local NOT CONNECTED iStatus={iStatus}") LOG.warn(f"bootstrap_local NOT CONNECTED iStatus={iStatus}")
return False return False
def test_bootstrap_iNodeInfo(self): # works def test_bootstrap_iNmapInfo(self): # works
if oTOX_OARGS.network in ['new', 'newlocal', 'localnew']: if oTOX_OARGS.network in ['new', 'newlocal', 'localnew']:
lElts = self.lUdp lElts = self.lUdp
elif oTOX_OARGS.proxy_port > 0: elif oTOX_OARGS.proxy_port > 0:
@ -816,7 +816,7 @@ class ToxSuite(unittest.TestCase):
lRetval = [] lRetval = []
random.shuffle(lElts) random.shuffle(lElts)
# assert # assert
bootstrap_iNodeInfo(lElts) bootstrap_iNmapInfo(lElts)
def test_self_get_secret_key(self): # works def test_self_get_secret_key(self): # works
""" """