added custom torrc support

master
Kevin Froman 2019-12-10 04:46:36 -06:00
parent 15872f8f7c
commit 6b0c88aff9
5 changed files with 89 additions and 2 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
"""
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

View File

@ -22,8 +22,10 @@ import multiprocessing
import platform # For windows sigkill workaround import platform # For windows sigkill workaround
import config, logger import config, logger
from . import getopenport from . import getopenport
from utils import identifyhome
from . import watchdog from . import watchdog
from . import customtorrc
from utils import identifyhome
config.reload() config.reload()
TOR_KILL_WAIT = 3 TOR_KILL_WAIT = 3
@ -107,6 +109,8 @@ HiddenServicePort 80 ''' + self.apiServerIP + ''':''' + str(self.hsPort)
torrcData = add_bridges(torrcData) torrcData = add_bridges(torrcData)
torrcData += customtorrc.get_custom_torrc()
torrc = open(self.torConfigLocation, 'w') torrc = open(self.torConfigLocation, 'w')
torrc.write(torrcData) torrc.write(torrcData)
torrc.close() torrc.close()

View File

@ -5,6 +5,7 @@
""" """
import time import time
import os
import psutil import psutil
@ -27,6 +28,12 @@ import psutil
def watchdog(parent_proc, child_proc): def watchdog(parent_proc, child_proc):
"""watch for proc1 to die, then kill proc2""" """watch for proc1 to die, then kill proc2"""
try:
if os.forkpty() != 0:
return
except AttributeError:
pass
parent_proc = psutil.Process(parent_proc) parent_proc = psutil.Process(parent_proc)
child_proc = psutil.Process(child_proc) child_proc = psutil.Process(child_proc)

View File

@ -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()

View File

@ -7,7 +7,7 @@ import unittest, uuid
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR) print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR os.environ["ONIONR_HOME"] = TEST_DIR
from utils import networkmerger, createdirs from utils import networkmerger
from coredb import keydb from coredb import keydb
import onionrsetup as setup import onionrsetup as setup
from utils import createdirs from utils import createdirs