Improved torcontrol formatting and efficency

master
Kevin Froman 2020-08-08 21:18:56 -05:00
parent e14955cb6b
commit 3473c5e36a
3 changed files with 24 additions and 23 deletions

View File

@ -7,7 +7,6 @@ import subprocess
import signal import signal
import time import time
import multiprocessing import multiprocessing
import platform # For windows sigkill workaround
from onionrtypes import BooleanSuccessState from onionrtypes import BooleanSuccessState
import logger import logger
@ -129,28 +128,26 @@ class NetController:
return True return True
def killTor(self): def killTor(self):
""" """Properly kill tor based on pid saved to file."""
Properly kill tor based on pid saved to file
"""
try: try:
pid = open(self.dataDir + 'torPid.txt', 'r') with open(self.dataDir + 'torPid.txt', 'r') as torPid:
pidN = pid.read() pidN = torPid.read()
pid.close()
except FileNotFoundError: except FileNotFoundError:
return return
try:
int(pidN)
except ValueError:
return
try: try:
try: try:
# Extra int()
os.kill(int(pidN), signal.SIGTERM) os.kill(int(pidN), signal.SIGTERM)
except PermissionError: except PermissionError:
# seems to happen on win 10 # seems to happen on win 10
pass pass
except ValueError:
# Happens if int() check is not valid
logger.error("torPid.txt contained invalid integer. " +
"This indicates corruption " +
"and should not be bypassed for security reasons")
return
os.remove(self.dataDir + 'torPid.txt') os.remove(self.dataDir + 'torPid.txt')
except ProcessLookupError: except ProcessLookupError:
pass pass
@ -162,10 +159,6 @@ class NetController:
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
if 'windows' == platform.system().lower():
os.system(f'taskkill /PID {pidN} /F')
time.sleep(0.5)
return
try: try:
os.kill(int(pidN), signal.SIGKILL) os.kill(int(pidN), signal.SIGKILL)
except (ProcessLookupError, PermissionError): except (ProcessLookupError, PermissionError):

View File

@ -5,6 +5,7 @@ Generate a generate a torrc file for our Onionr instance
import base64 import base64
import os import os
import subprocess import subprocess
from typing import TYPE_CHECKING
from .. import getopenport from .. import getopenport
from . import customtorrc from . import customtorrc
@ -12,6 +13,10 @@ from . import addbridges
from . import torbinary from . import torbinary
from utils import identifyhome from utils import identifyhome
import config import config
if TYPE_CHECKING:
from netcontroller import NetController
from onionrtypes import LoopBackIP
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -30,7 +35,8 @@ import config
add_bridges = addbridges.add_bridges add_bridges = addbridges.add_bridges
def generate_torrc(net_controller, api_server_ip): def generate_torrc(net_controller: 'NetController',
api_server_ip: 'LoopBackIP'):
"""Generate a torrc file for our tor instance.""" """Generate a torrc file for our tor instance."""
socks_port = net_controller.socksPort socks_port = net_controller.socksPort
hs_port = net_controller.hsPort hs_port = net_controller.hsPort
@ -43,13 +49,14 @@ def generate_torrc(net_controller, api_server_ip):
Set the Tor control password. Set the Tor control password.
Meant to make it harder to manipulate our Tor instance Meant to make it harder to manipulate our Tor instance
""" """
plaintext = base64.b85encode(os.urandom(50)).decode() plaintext = base64.b85encode(
os.urandom(50)).decode()
config.set('tor.controlpassword', plaintext, savefile=True) config.set('tor.controlpassword', plaintext, savefile=True)
config.set('tor.socksport', socks_port, savefile=True) config.set('tor.socksport', socks_port, savefile=True)
controlPort = getopenport.get_open_port() control_port = getopenport.get_open_port()
config.set('tor.controlPort', controlPort, savefile=True) config.set('tor.controlPort', control_port, savefile=True)
hashedPassword = subprocess.Popen([torbinary.tor_binary(), hashedPassword = subprocess.Popen([torbinary.tor_binary(),
'--hash-password', '--hash-password',
@ -66,7 +73,7 @@ DataDirectory """ + home_dir + """tordata/
CookieAuthentication 1 CookieAuthentication 1
KeepalivePeriod 40 KeepalivePeriod 40
CircuitsAvailableTimeout 86400 CircuitsAvailableTimeout 86400
ControlPort """ + str(controlPort) + """ ControlPort """ + str(control_port) + """
HashedControlPassword """ + str(password) + """ HashedControlPassword """ + str(password) + """
""" """
if config.get('general.security_level', 1) == 0: if config.get('general.security_level', 1) == 0:

View File

@ -3,7 +3,8 @@ from typing import NewType
UserID = NewType('UserID', str) UserID = NewType('UserID', str)
UserIDSecretKey = NewType('UserIDSecretKey', str) UserIDSecretKey = NewType('UserIDSecretKey', str)
LANIP = NewType('LANIP', 'str') LANIP = NewType('LANIP', str)
LoopBackIP = NewType('LoopBackIP', str)
DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str) DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str)