From 583c5290d4223ae17b8bbe9703731dfc99b2600b Mon Sep 17 00:00:00 2001 From: Arinerron Date: Fri, 26 Jan 2018 16:52:20 -0800 Subject: [PATCH] Add input functions --- onionr/communicator.py | 2 +- onionr/data/host.txt | 1 + onionr/data/hs/hostname | 1 + onionr/data/torrc | 4 ++++ onionr/logger.py | 47 ++++++++++++++++++++++++++++++++++++++++- onionr/onionrutils.py | 24 ++++++++++----------- onionr/tests.py | 6 +++--- 7 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 onionr/data/host.txt create mode 100644 onionr/data/hs/hostname create mode 100644 onionr/data/torrc diff --git a/onionr/communicator.py b/onionr/communicator.py index febbd103..6c1e1906 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -98,7 +98,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] diff --git a/onionr/data/host.txt b/onionr/data/host.txt new file mode 100644 index 00000000..e56ea71e --- /dev/null +++ b/onionr/data/host.txt @@ -0,0 +1 @@ +127.0.0.1 \ No newline at end of file diff --git a/onionr/data/hs/hostname b/onionr/data/hs/hostname new file mode 100644 index 00000000..5172f43b --- /dev/null +++ b/onionr/data/hs/hostname @@ -0,0 +1 @@ +aaron-optiplex diff --git a/onionr/data/torrc b/onionr/data/torrc new file mode 100644 index 00000000..bc783e55 --- /dev/null +++ b/onionr/data/torrc @@ -0,0 +1,4 @@ +SocksPort 16712 +HiddenServiceDir data/hs/ +HiddenServicePort 80 127.0.0.1:1337 + \ No newline at end of file diff --git a/onionr/logger.py b/onionr/logger.py index fad5e95e..f147f44b 100644 --- a/onionr/logger.py +++ b/onionr/logger.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' -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: diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index a1213341..b36dbff4 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -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,27 +39,30 @@ 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() - print('Confirm password: ') - pass2 = getpass.getpass() - if pass1 != pass2: - logger.error("Passwords do not match.") - input() + if confirm: + print('Confirm password: ') + pass2 = getpass.getpass() + if pass1 != pass2: + logger.error("Passwords do not match.") + 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 @@ -92,7 +92,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 diff --git a/onionr/tests.py b/onionr/tests.py index 93c5ba38..8735a1a4 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -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()