diff --git a/src/netcontroller/customtorrc.py b/src/netcontroller/customtorrc.py new file mode 100644 index 00000000..f12c9d3d --- /dev/null +++ b/src/netcontroller/customtorrc.py @@ -0,0 +1,45 @@ +""" + Onionr - Private P2P Communication + + Load or set custom torrc +""" +from utils import identifyhome +""" + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" + +CUSTOM_TORRC_FILE = identifyhome.identify_home() + '/torrc-custom' + + +def set_custom_torrc(torrc_data: str): + """write torrc_data to custom torrc file stored in home dir. + if set it will be used in addition to onionr's generated settings + """ + torrc_comment = f'\n# BEGIN CUSTOM TORRC FROM {CUSTOM_TORRC_FILE}\n' + torrc_data = torrc_comment + torrc_data + with open(CUSTOM_TORRC_FILE, 'w') as torrc: + torrc.write(torrc_data) + + +def get_custom_torrc() -> str: + """read torrc_data from custom torrc file stored in home dir. + if set it will be used in addition to onionr's generated settings + """ + torrc = '' + try: + with open(CUSTOM_TORRC_FILE, 'r') as torrc: + torrc = torrc.read() + except FileNotFoundError: + pass + return '\n' + torrc diff --git a/src/netcontroller/netcontrol.py b/src/netcontroller/netcontrol.py index 32834b24..d2eba18a 100644 --- a/src/netcontroller/netcontrol.py +++ b/src/netcontroller/netcontrol.py @@ -22,8 +22,10 @@ import multiprocessing import platform # For windows sigkill workaround import config, logger from . import getopenport -from utils import identifyhome from . import watchdog +from . import customtorrc +from utils import identifyhome + config.reload() TOR_KILL_WAIT = 3 @@ -107,6 +109,8 @@ HiddenServicePort 80 ''' + self.apiServerIP + ''':''' + str(self.hsPort) torrcData = add_bridges(torrcData) + torrcData += customtorrc.get_custom_torrc() + torrc = open(self.torConfigLocation, 'w') torrc.write(torrcData) torrc.close() diff --git a/src/netcontroller/watchdog.py b/src/netcontroller/watchdog.py index 9ccbc771..2d6e67f1 100644 --- a/src/netcontroller/watchdog.py +++ b/src/netcontroller/watchdog.py @@ -5,6 +5,7 @@ """ import time +import os import psutil @@ -27,6 +28,12 @@ import psutil def watchdog(parent_proc, child_proc): """watch for proc1 to die, then kill proc2""" + try: + if os.forkpty() != 0: + return + except AttributeError: + pass + parent_proc = psutil.Process(parent_proc) child_proc = psutil.Process(child_proc) diff --git a/tests/test_custom_torrc.py b/tests/test_custom_torrc.py new file mode 100644 index 00000000..02b33cdd --- /dev/null +++ b/tests/test_custom_torrc.py @@ -0,0 +1,31 @@ +import sys, os +sys.path.append(".") +sys.path.append("src/") +import unittest, uuid + +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR +from utils import createdirs, identifyhome +import onionrsetup as setup +from netcontroller import customtorrc +createdirs.create_dirs() +setup.setup_config() + +class TestCustomTorrc(unittest.TestCase): + def test_torrc_get(self): + torrc = identifyhome.identify_home() + '/torrc-custom' + self.assertEqual(customtorrc.get_custom_torrc(), '\n') + with open(torrc, 'w') as torrc_file: + torrc_file.write('test') + self.assertEqual(customtorrc.get_custom_torrc(), '\ntest') + os.remove(torrc) + + def test_torrc_set(self): + torrc = identifyhome.identify_home() + '/torrc-custom' + customtorrc.set_custom_torrc('test2') + with open(torrc, 'r') as torrc_file: + self.assertEqual(torrc_file.read().splitlines()[2], 'test2') + os.remove(torrc) + +unittest.main() diff --git a/tests/test_networkmerger.py b/tests/test_networkmerger.py index bb6643c0..755d2e7b 100644 --- a/tests/test_networkmerger.py +++ b/tests/test_networkmerger.py @@ -7,7 +7,7 @@ import unittest, uuid TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' print("Test directory:", TEST_DIR) os.environ["ONIONR_HOME"] = TEST_DIR -from utils import networkmerger, createdirs +from utils import networkmerger from coredb import keydb import onionrsetup as setup from utils import createdirs