Add input functions

This commit is contained in:
Arinerron 2018-01-26 16:52:20 -08:00
parent 197d47eb7d
commit 583c5290d4
No known key found for this signature in database
GPG Key ID: 99383627861C62F0
7 changed files with 68 additions and 17 deletions

View File

@ -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]

1
onionr/data/host.txt Normal file
View File

@ -0,0 +1 @@
127.0.0.1

1
onionr/data/hs/hostname Normal file
View File

@ -0,0 +1 @@
aaron-optiplex

4
onionr/data/torrc Normal file
View File

@ -0,0 +1,4 @@
SocksPort 16712
HiddenServiceDir data/hs/
HiddenServicePort 80 127.0.0.1:1337

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,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

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()