2022-09-27 15:51:50 +02:00
|
|
|
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
|
2023-12-17 01:00:38 +01:00
|
|
|
|
2022-11-20 02:11:51 +01:00
|
|
|
import ctypes
|
|
|
|
import traceback
|
|
|
|
import os
|
2023-12-17 01:00:38 +01:00
|
|
|
from ctypes import *
|
|
|
|
|
|
|
|
import user_data.settings
|
|
|
|
import tox_wrapper.tox
|
|
|
|
import tox_wrapper.toxcore_enums_and_consts as enums
|
|
|
|
from tox_wrapper.tests import support_testing as ts
|
|
|
|
# callbacks can be called in any thread so were being careful
|
|
|
|
# tox.py can be called by callbacks
|
|
|
|
from tox_wrapper.tests.support_testing import LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG, LOG_TRACE
|
2022-09-27 14:38:39 +02:00
|
|
|
|
2022-09-27 15:51:50 +02:00
|
|
|
global LOG
|
|
|
|
import logging
|
|
|
|
LOG = logging.getLogger('app.'+'tox_factory')
|
2022-09-27 14:38:39 +02:00
|
|
|
|
2022-11-17 16:26:55 +01:00
|
|
|
from utils import util
|
2022-11-20 02:11:51 +01:00
|
|
|
from utils import ui as util_ui
|
2022-09-27 15:51:50 +02:00
|
|
|
|
2022-11-20 02:11:51 +01:00
|
|
|
|
2022-09-27 15:51:50 +02:00
|
|
|
def tox_factory(data=None, settings=None, args=None, app=None):
|
2022-09-27 14:38:39 +02:00
|
|
|
"""
|
|
|
|
:param data: user data from .tox file. None = no saved data, create new profile
|
|
|
|
:param settings: current profile settings. None = default settings will be used
|
|
|
|
:return: new tox instance
|
|
|
|
"""
|
2022-09-27 15:51:50 +02:00
|
|
|
if not settings:
|
2022-10-12 21:51:08 +02:00
|
|
|
LOG_WARN("tox_factory using get_default_settings")
|
2022-09-27 14:38:39 +02:00
|
|
|
settings = user_data.settings.Settings.get_default_settings()
|
2022-09-27 15:51:50 +02:00
|
|
|
else:
|
|
|
|
user_data.settings.clean_settings(settings)
|
|
|
|
|
|
|
|
try:
|
2023-12-17 01:00:38 +01:00
|
|
|
tox_options = tox_wrapper.tox.Tox.options_new()
|
2022-09-27 15:51:50 +02:00
|
|
|
tox_options.contents.ipv6_enabled = settings['ipv6_enabled']
|
|
|
|
tox_options.contents.udp_enabled = settings['udp_enabled']
|
|
|
|
tox_options.contents.proxy_type = int(settings['proxy_type'])
|
|
|
|
if type(settings['proxy_host']) == str:
|
|
|
|
tox_options.contents.proxy_host = bytes(settings['proxy_host'],'UTF-8')
|
|
|
|
elif type(settings['proxy_host']) == bytes:
|
|
|
|
tox_options.contents.proxy_host = settings['proxy_host']
|
|
|
|
else:
|
|
|
|
tox_options.contents.proxy_host = b''
|
|
|
|
tox_options.contents.proxy_port = int(settings['proxy_port'])
|
|
|
|
tox_options.contents.start_port = settings['start_port']
|
|
|
|
tox_options.contents.end_port = settings['end_port']
|
|
|
|
tox_options.contents.tcp_port = settings['tcp_port']
|
|
|
|
tox_options.contents.local_discovery_enabled = settings['local_discovery_enabled']
|
|
|
|
tox_options.contents.dht_announcements_enabled = settings['dht_announcements_enabled']
|
|
|
|
tox_options.contents.hole_punching_enabled = settings['hole_punching_enabled']
|
|
|
|
if data: # load existing profile
|
|
|
|
tox_options.contents.savedata_type = enums.TOX_SAVEDATA_TYPE['TOX_SAVE']
|
|
|
|
tox_options.contents.savedata_data = ctypes.c_char_p(data)
|
|
|
|
tox_options.contents.savedata_length = len(data)
|
|
|
|
else: # create new profile
|
|
|
|
tox_options.contents.savedata_type = enums.TOX_SAVEDATA_TYPE['NONE']
|
|
|
|
tox_options.contents.savedata_data = None
|
|
|
|
tox_options.contents.savedata_length = 0
|
|
|
|
|
|
|
|
# overrides
|
|
|
|
tox_options.contents.local_discovery_enabled = False
|
|
|
|
tox_options.contents.ipv6_enabled = False
|
|
|
|
tox_options.contents.hole_punching_enabled = False
|
|
|
|
|
2023-12-17 01:00:38 +01:00
|
|
|
LOG.debug("tox_wrapper.tox.Tox settings: " +repr(settings))
|
2022-09-27 15:51:50 +02:00
|
|
|
|
2023-12-17 01:00:38 +01:00
|
|
|
if 'trace_enabled' in settings and not settings['trace_enabled']:
|
|
|
|
LOG_DEBUG("settings['trace_enabled' disabled" )
|
2024-02-03 05:34:10 +01:00
|
|
|
elif tox_options._options_pointer and \
|
|
|
|
'trace_enabled' in settings and settings['trace_enabled']:
|
|
|
|
ts.vAddLoggerCallback(tox_options)
|
2023-12-17 01:00:38 +01:00
|
|
|
LOG_INFO("c-toxcore trace_enabled enabled" )
|
2022-09-27 15:51:50 +02:00
|
|
|
else:
|
2022-10-13 15:55:56 +02:00
|
|
|
LOG_WARN("No tox_options._options_pointer to add self_logger_cb" )
|
2022-09-27 15:51:50 +02:00
|
|
|
|
2023-12-17 01:00:38 +01:00
|
|
|
retval = tox_wrapper.tox.Tox(tox_options)
|
2022-09-27 15:51:50 +02:00
|
|
|
except Exception as e:
|
|
|
|
if app and hasattr(app, '_log'):
|
2022-10-12 21:51:08 +02:00
|
|
|
pass
|
2023-12-17 01:00:38 +01:00
|
|
|
LOG_ERROR(f"tox_wrapper.tox.Tox failed: {e}")
|
2022-10-12 21:51:08 +02:00
|
|
|
LOG_WARN(traceback.format_exc())
|
2022-09-27 15:51:50 +02:00
|
|
|
raise
|
2022-09-27 14:38:39 +02:00
|
|
|
|
2022-09-27 15:51:50 +02:00
|
|
|
if app and hasattr(app, '_log'):
|
2023-12-17 01:00:38 +01:00
|
|
|
app._log("DEBUG: tox_wrapper.tox.Tox succeeded")
|
2022-09-27 15:51:50 +02:00
|
|
|
return retval
|