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/*.pyc
onionr/*.log
onionr/data/hs/hostname
onionr/data/*

View File

@ -99,7 +99,7 @@ class OnionrCommunicate:
return
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/'):
raise PeerError('Currently only Tor .onion peers are supported. You must manually specify .onion')
socksPort = sys.argv[2]

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import re
import re, sys
class colors:
'''
@ -127,6 +127,51 @@ def log(prefix, data, color = ''):
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
def debug(data):
if get_level() <= LEVEL_DEBUG:

View File

@ -31,9 +31,6 @@ class OnionrUtils:
self.fingerprintFile = 'data/own-fingerprint.txt'
self._core = coreInstance
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):
'''Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.'''
config = configparser.ConfigParser()
@ -42,12 +39,13 @@ class OnionrUtils:
else:
return
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 safely with confirmation and return it
while True:
print(message)
pass1 = getpass.getpass()
if confirm:
print('Confirm password: ')
pass2 = getpass.getpass()
if pass1 != pass2:
@ -55,14 +53,16 @@ class OnionrUtils:
input()
else:
break
else:
break
return pass1
def checkPort(self, port):
def checkPort(self, port, host = ''):
'''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/
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
retVal = False
try:
sock.bind(('', port))
sock.bind((host, port))
except OSError as e:
if e.errno is 98:
retVal = True
@ -100,7 +100,7 @@ class OnionrUtils:
return dataHash
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
if len(data) != length:
retVal = False

View File

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