From d2d60fe6ce9972299145a2c790331aaa3fe14db9 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 19 Jan 2018 15:28:34 -0600 Subject: [PATCH] netcontroller now gets the tor hs id --- onionr/netcontroller.py | 24 ++++++++++++++++-------- onionr/onionr.py | 8 ++++---- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index 5fbfba4e..bbf97067 100644 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import subprocess, os +import subprocess, os, random, sys class NetController: '''NetController This class handles hidden service setup on Tor and I2P @@ -25,8 +25,9 @@ class NetController: def __init__(self, hsPort): self.torConfigLocation = 'data/torrc' self.readyState = False - self.socksPort = socksPort + self.socksPort = random.randint(1024, 65535) self.hsPort = hsPort + self.myID = '' if os.path.exists(self.torConfigLocation): torrc = open(self.torConfigLocation, 'r') if not self.hsPort in torrc.read(): @@ -35,9 +36,9 @@ class NetController: def generateTorrc(self): if os.path.exists(self.torConfigLocation): os.remove(self.torConfigLocation) - torrcData = '''SOCKSPORT ''' + self.socksPort + ''' -HiddenServiceData data/hs/ -HiddenServicePort 80 127.0.0.1:''' + self.hsPort + ''' + torrcData = '''SOCKSPORT ''' + str(self.socksPort) + ''' +HiddenServiceDir data/hs/ +HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + ''' ''' torrc = open(self.torConfigLocation, 'w') torrc.write(torrcData) @@ -45,7 +46,14 @@ HiddenServicePort 80 127.0.0.1:''' + self.hsPort + ''' return def startTor(self): - if not os.path.exists(self.torConfigLocation): - self.generateTorrc() - subprocess.Popen(['tor', '-f ' + self.torConfigLocation]) + self.generateTorrc() + tor = subprocess.Popen(['tor', '-f', self.torConfigLocation], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + for line in iter(tor.stdout.readline, b''): + if 'Bootstrapped 100%: Done' in line.decode(): + break + print('Finished starting Tor') + self.readyState = True + myID = open('data/hs/hostname', 'r') + self.myID = myID.read() + myID.close() return \ No newline at end of file diff --git a/onionr/onionr.py b/onionr/onionr.py index 749c85e9..9e852bb4 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -20,7 +20,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sys, os, configparser, base64, random, getpass, shutil, subprocess, requests +import sys, os, configparser, base64, random, getpass, shutil, subprocess, requests, time import gui, api, colors, core from onionrutils import OnionrUtils from colors import Colors @@ -119,10 +119,10 @@ class Onionr: ''' Start the Onionr communication daemon ''' if not os.environ.get("WERKZEUG_RUN_MAIN") == "true": - net = NetController(self.config['PORT']) + net = NetController(self.config['CLIENT']['PORT']) print('Tor is starting...') - net.startTor(self.config['CLIENT']['PORT']) - time.sleep(5) + net.startTor() + time.sleep(1) subprocess.Popen(["./communicator.py", "run"]) print('Started communicator') api.API(self.config, self.debug)