Merge remote-tracking branch 'origin/loggerdev' into mergeloggerdev

This commit is contained in:
Kevin Froman 2018-01-26 19:17:22 -06:00
commit 1c8f8a6632
5 changed files with 64 additions and 17 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ onionr/data/*.db
onionr/data-old/* onionr/data-old/*
onionr/*.pyc onionr/*.pyc
onionr/*.log onionr/*.log
onionr/data/hs/hostname
onionr/data/*

View File

@ -99,7 +99,7 @@ class OnionrCommunicate:
return return
def performGet(self, action, peer, data=None, type='tor'): def performGet(self, action, peer, data=None, type='tor'):
'''performs a request to a peer through Tor or i2p (currently only tor)''' '''Performs a request to a peer through Tor or i2p (currently only tor)'''
if not peer.endswith('.onion') and not peer.endswith('.onion/'): if not peer.endswith('.onion') and not peer.endswith('.onion/'):
raise PeerError('Currently only Tor .onion peers are supported. You must manually specify .onion') raise PeerError('Currently only Tor .onion peers are supported. You must manually specify .onion')
socksPort = sys.argv[2] socksPort = sys.argv[2]

View File

@ -18,7 +18,7 @@
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 re import re, sys
class colors: class colors:
''' '''
@ -127,6 +127,51 @@ def log(prefix, data, color = ''):
raw(output) raw(output)
'''
Takes in input from the console, not stored in logs
message: The message to display before taking input
'''
def input(message = 'Enter input: '):
color = colors.fg.green + colors.bold
output = colors.reset + str(color) + '... ' + colors.reset + str(message) + colors.reset
if not get_settings() & USE_ANSI:
output = colors.filter(output)
sys.stdout.write(output)
return raw_input()
'''
Displays an "Are you sure" message, returns True for Y and False for N
message: The confirmation message, use %s for (y/n)
default: which to prefer-- y or n
'''
def confirm(default = 'y', message = 'Are you sure %s? '):
color = colors.fg.green + colors.bold
default = default.lower()
confirm = colors.bold
if default.startswith('y'):
confirm += '(Y/n)'
else:
confirm += '(y/N)'
confirm += colors.reset + color
output = colors.reset + str(color) + '... ' + colors.reset + str(message) + colors.reset
if not get_settings() & USE_ANSI:
output = colors.filter(output)
sys.stdout.write(output.replace('%s', confirm))
inp = raw_input().lower()
if 'y' in inp:
return True
if 'n' in inp:
return False
else:
return default == 'y'
# debug: when there is info that could be useful for debugging purposes only # debug: when there is info that could be useful for debugging purposes only
def debug(data): def debug(data):
if get_level() <= LEVEL_DEBUG: if get_level() <= LEVEL_DEBUG:

View File

@ -31,9 +31,6 @@ class OnionrUtils:
self.fingerprintFile = 'data/own-fingerprint.txt' self.fingerprintFile = 'data/own-fingerprint.txt'
self._core = coreInstance self._core = coreInstance
return return
def printErr(self, text='an error occured'):
'''Print an error message to stderr with a new line'''
logger.error(text)
def localCommand(self, command): def localCommand(self, command):
'''Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.''' '''Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.'''
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -42,27 +39,30 @@ class OnionrUtils:
else: else:
return return
requests.get('http://' + open('data/host.txt', 'r').read() + ':' + str(config['CLIENT']['PORT']) + '/client/?action=' + command + '&token=' + config['CLIENT']['CLIENT HMAC']) requests.get('http://' + open('data/host.txt', 'r').read() + ':' + str(config['CLIENT']['PORT']) + '/client/?action=' + command + '&token=' + config['CLIENT']['CLIENT HMAC'])
def getPassword(self, message='Enter password: '): def getPassword(self, message='Enter password: ', confirm = True):
'''Get a password without showing the users typing and confirm the input''' '''Get a password without showing the users typing and confirm the input'''
# Get a password safely with confirmation and return it # Get a password safely with confirmation and return it
while True: while True:
print(message) print(message)
pass1 = getpass.getpass() pass1 = getpass.getpass()
print('Confirm password: ') if confirm:
pass2 = getpass.getpass() print('Confirm password: ')
if pass1 != pass2: pass2 = getpass.getpass()
logger.error("Passwords do not match.") if pass1 != pass2:
input() logger.error("Passwords do not match.")
input()
else:
break
else: else:
break break
return pass1 return pass1
def checkPort(self, port): def checkPort(self, port, host = ''):
'''Checks if a port is available, returns bool''' '''Checks if a port is available, returns bool'''
# inspired by https://www.reddit.com/r/learnpython/comments/2i4qrj/how_to_write_a_python_script_that_checks_to_see/ckzarux/ # inspired by https://www.reddit.com/r/learnpython/comments/2i4qrj/how_to_write_a_python_script_that_checks_to_see/ckzarux/
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
retVal = False retVal = False
try: try:
sock.bind(('', port)) sock.bind((host, port))
except OSError as e: except OSError as e:
if e.errno is 98: if e.errno is 98:
retVal = True retVal = True
@ -100,7 +100,7 @@ class OnionrUtils:
return dataHash return dataHash
def validateHash(self, data, length=64): def validateHash(self, data, length=64):
'''validate if a string is a valid hex formatted hash''' '''Validate if a string is a valid hex formatted hash'''
retVal = True retVal = True
if len(data) != length: if len(data) != length:
retVal = False retVal = False

View File

@ -115,13 +115,13 @@ class OnionrTests(unittest.TestCase):
while True: while True:
command = myCore.daemonQueue() command = myCore.daemonQueue()
if command == False: if command == False:
print('The queue is empty (false)') logger.debug('The queue is empty (false)')
break break
else: else:
print(command[0]) logger.debug(command[0])
myCore.daemonQueueAdd('testCommand', 'testData') myCore.daemonQueueAdd('testCommand', 'testData')
command = myCore.daemonQueue() command = myCore.daemonQueue()
if command[0] == 'testCommand': if command[0] == 'testCommand':
if myCore.daemonQueue() == False: if myCore.daemonQueue() == False:
print('Succesfully added and read command') logger.info('Succesfully added and read command')
unittest.main() unittest.main()