Minor bug fixes, and more refactoring

This commit is contained in:
Arinerron 2018-03-02 23:35:13 -08:00
parent d5daeae532
commit c2db59ba8b
No known key found for this signature in database
GPG Key ID: 99383627861C62F0
2 changed files with 19 additions and 3 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ onionr/*.log
onionr/data/hs/hostname onionr/data/hs/hostname
onionr/data/* onionr/data/*
onionr/gnupg/* onionr/gnupg/*
run.sh

View File

@ -17,11 +17,14 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import subprocess, os, random, sys, logger, time, signal import subprocess, os, random, sys, logger, time, signal
class NetController: class NetController:
''' '''
This class handles hidden service setup on Tor and I2P This class handles hidden service setup on Tor and I2P
''' '''
def __init__(self, hsPort): def __init__(self, hsPort):
self.torConfigLocation = 'data/torrc' self.torConfigLocation = 'data/torrc'
self.readyState = False self.readyState = False
@ -36,13 +39,14 @@ class NetController:
os.remove(self.torConfigLocation) os.remove(self.torConfigLocation)
torrc.close() torrc.close()
''' '''
return return
def generateTorrc(self): def generateTorrc(self):
''' '''
Generate a torrc file for our tor instance Generate a torrc file for our tor instance
''' '''
if os.path.exists(self.torConfigLocation): if os.path.exists(self.torConfigLocation):
os.remove(self.torConfigLocation) os.remove(self.torConfigLocation)
torrcData = '''SocksPort ''' + str(self.socksPort) + ''' torrcData = '''SocksPort ''' + str(self.socksPort) + '''
@ -59,11 +63,16 @@ HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + '''
''' '''
Start Tor with onion service on port 80 & socks proxy on random port Start Tor with onion service on port 80 & socks proxy on random port
''' '''
self.generateTorrc() self.generateTorrc()
if os.path.exists('./tor'): if os.path.exists('./tor'):
torBinary = './tor' torBinary = './tor'
elif os.path.exists('/usr/bin/tor'):
torBinary = '/usr/bin/tor'
else: else:
torBinary = 'tor' torBinary = 'tor'
try: try:
tor = subprocess.Popen([torBinary, '-f', self.torConfigLocation], stdout=subprocess.PIPE, stderr=subprocess.PIPE) tor = subprocess.Popen([torBinary, '-f', self.torConfigLocation], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError: except FileNotFoundError:
@ -83,15 +92,18 @@ HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + '''
if 'Bootstrapped 100%: Done' in line.decode(): if 'Bootstrapped 100%: Done' in line.decode():
break break
elif 'Opening Socks listener' in line.decode(): elif 'Opening Socks listener' in line.decode():
logger.debug(line.decode()) logger.debug(line.decode().replace('\n', ''))
else: else:
logger.fatal('Failed to start Tor. Try killing any other Tor processes owned by this user.') logger.fatal('Failed to start Tor. Try killing any other Tor processes owned by this user.')
return False return False
logger.info('Finished starting Tor') logger.info('Finished starting Tor')
self.readyState = True self.readyState = True
myID = open('data/hs/hostname', 'r') myID = open('data/hs/hostname', 'r')
self.myID = myID.read() self.myID = myID.read().replace('\n', '')
myID.close() myID.close()
torPidFile = open('data/torPid.txt', 'w') torPidFile = open('data/torPid.txt', 'w')
torPidFile.write(str(tor.pid)) torPidFile.write(str(tor.pid))
torPidFile.close() torPidFile.close()
@ -102,16 +114,19 @@ HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + '''
''' '''
Properly kill tor based on pid saved to file Properly kill tor based on pid saved to file
''' '''
try: try:
pid = open('data/torPid.txt', 'r') pid = open('data/torPid.txt', 'r')
pidN = pid.read() pidN = pid.read()
pid.close() pid.close()
except FileNotFoundError: except FileNotFoundError:
return return
try: try:
int(pidN) int(pidN)
except: except:
return return
try: try:
os.kill(int(pidN), signal.SIGTERM) os.kill(int(pidN), signal.SIGTERM)
os.remove('data/torPid.txt') os.remove('data/torPid.txt')