From 6a1a448eab9a446061f5b4b163bf05db075d1985 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 20 Jan 2018 01:23:09 -0600 Subject: [PATCH] fixed pgp generation, work on onion --- onionr/api.py | 2 ++ onionr/communicator.py | 12 ++++++++++++ onionr/core.py | 12 +++++++++--- onionr/onionr.py | 5 ++++- onionr/onionrutils.py | 17 +++++++++++++++-- readme.md | 4 ++++ 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index c9980cae..6933caa2 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -125,6 +125,8 @@ class API: resp = Response("pong!") elif action == 'setHMAC': pass + elif action == 'getPGP': + ascii_armored_public_keys = gpg.export_keys('') return resp diff --git a/onionr/communicator.py b/onionr/communicator.py index 9262aebd..3e81a472 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -28,6 +28,18 @@ class OnionrCommunicate: This class handles communication with nodes in the Onionr network. ''' self._core = core.Core() + if debug: + print('Communicator debugging enabled') + torID = open('data/hs/hostname').read() + + # get our own PGP fingerprint + fingerprintFile = 'data/own-fingerprint.txt' + if not os.path.exists(fingerprintFile): + self._core.generateMainPGP(torID) + with open(fingerprintFile,'r') as f: + self.pgpOwnFingerprint = f.read() + print('My PGP fingerprint is ' + self.pgpOwnFingerprint) + while True: command = self._core.daemonQueue() if debug: diff --git a/onionr/core.py b/onionr/core.py index 819ef0af..4f9d8c54 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -29,17 +29,23 @@ class Core: ''' self.queueDB = 'data/queue.db' self.peerDB = 'data/peers.db' + self.ownPGPID = '' #self.daemonQueue() # Call to create the DB if it doesn't exist return - def generateMainPGP(self): + def generateMainPGP(self, myID): ''' Generate the main PGP key for our client. Should not be done often. Uses own PGP home folder in the data/ directory. ''' # Generate main pgp key - gpg = gnupg.GPG(homedir='data/pgp/') - input_data = gpg.gen_key_input(key_type="RSA", key_length=2048, name_real='anon', name_comment='Onionr key', name_email='anon@onionr') + gpg = gnupg.GPG(gnupghome='./data/pgp/') + input_data = gpg.gen_key_input(key_type="RSA", key_length=2048, name_real=myID, name_email='anon@onionr') + #input_data = gpg.gen_key_input(key_type="RSA", key_length=1024) key = gpg.gen_key(input_data) + # Write the key + myFingerpintFile = open('data/own-fingerprint.txt', 'w') + myFingerpintFile.write(key.fingerprint) + myFingerpintFile.close() return def addPeer(self, peerID, name=''): diff --git a/onionr/onionr.py b/onionr/onionr.py index d43b895c..4fa8c19c 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -78,7 +78,10 @@ class Onionr: if self.debug: randomPort = 8080 else: - randomPort = random.randint(1024, 65535) + while True: + randomPort = random.randint(1024, 65535) + if self.onionrUtils.checkPort(randomPort): + break self.config['CLIENT'] = {'CLIENT HMAC': base64.b64encode(os.urandom(32)).decode('utf-8'), 'PORT': randomPort, 'API VERSION': '0.0.0'} with open('data/config.ini', 'w') as configfile: self.config.write(configfile) diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 57c7e2d3..7a2f5413 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' # Misc functions that do not fit in the main api, but are useful -import getpass, sys, requests, configparser, os +import getpass, sys, requests, configparser, os, socket class OnionrUtils(): '''Various useful functions''' def __init__(self): @@ -47,4 +47,17 @@ class OnionrUtils(): input() else: break - return pass1 \ No newline at end of file + return pass1 + def checkPort(self, port): + '''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)) + except OSError as e: + if e.errno is 98: + retVal = True + finally: + sock.close() + return retVal \ No newline at end of file diff --git a/readme.md b/readme.md index 23ae19c1..730d1270 100644 --- a/readme.md +++ b/readme.md @@ -12,3 +12,7 @@ Major work in progress. # Development This software is in heavy development. If for some reason you want to get involved, get in touch first. + +## Disclaimer + +The Tor Project, I2P developers, and anyone else do not own, create, or endorse this project, and are not otherwise involved.