2018-01-09 22:58:12 +00:00
|
|
|
'''
|
|
|
|
Onionr - P2P Microblogging Platform & Social network
|
2018-01-14 08:48:23 +00:00
|
|
|
|
|
|
|
OnionrUtils offers various useful functions to Onionr. Relatively misc.
|
|
|
|
'''
|
|
|
|
'''
|
2018-01-09 22:58:12 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
|
|
|
# Misc functions that do not fit in the main api, but are useful
|
2018-01-29 02:52:48 +00:00
|
|
|
import getpass, sys, requests, configparser, os, socket, gnupg, hashlib, logger, sqlite3
|
2018-01-26 06:28:11 +00:00
|
|
|
if sys.version_info < (3, 6):
|
|
|
|
try:
|
|
|
|
import sha3
|
|
|
|
except ModuleNotFoundError:
|
2018-01-26 07:22:48 +00:00
|
|
|
logger.fatal('On Python 3 versions prior to 3.6.x, you need the sha3 module')
|
2018-01-26 06:28:11 +00:00
|
|
|
sys.exit(1)
|
|
|
|
class OnionrUtils:
|
2018-01-14 08:48:23 +00:00
|
|
|
'''Various useful functions'''
|
2018-01-26 06:28:11 +00:00
|
|
|
def __init__(self, coreInstance):
|
2018-01-20 17:59:44 +00:00
|
|
|
self.fingerprintFile = 'data/own-fingerprint.txt'
|
2018-01-26 06:28:11 +00:00
|
|
|
self._core = coreInstance
|
2018-01-09 22:58:12 +00:00
|
|
|
return
|
2018-01-14 08:48:23 +00:00
|
|
|
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()
|
|
|
|
if os.path.exists('data/config.ini'):
|
|
|
|
config.read('data/config.ini')
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
requests.get('http://' + open('data/host.txt', 'r').read() + ':' + str(config['CLIENT']['PORT']) + '/client/?action=' + command + '&token=' + config['CLIENT']['CLIENT HMAC'])
|
2018-01-27 00:52:20 +00:00
|
|
|
def getPassword(self, message='Enter password: ', confirm = True):
|
2018-01-14 08:48:23 +00:00
|
|
|
'''Get a password without showing the users typing and confirm the input'''
|
2018-01-09 22:58:12 +00:00
|
|
|
# Get a password safely with confirmation and return it
|
|
|
|
while True:
|
|
|
|
print(message)
|
|
|
|
pass1 = getpass.getpass()
|
2018-01-27 00:52:20 +00:00
|
|
|
if confirm:
|
|
|
|
print('Confirm password: ')
|
|
|
|
pass2 = getpass.getpass()
|
|
|
|
if pass1 != pass2:
|
|
|
|
logger.error("Passwords do not match.")
|
2018-01-29 06:01:36 +00:00
|
|
|
logger.readline()
|
2018-01-27 00:52:20 +00:00
|
|
|
else:
|
|
|
|
break
|
2018-01-09 22:58:12 +00:00
|
|
|
else:
|
|
|
|
break
|
2018-01-20 07:23:09 +00:00
|
|
|
return pass1
|
2018-01-27 00:52:20 +00:00
|
|
|
def checkPort(self, port, host = ''):
|
2018-01-20 07:23:09 +00:00
|
|
|
'''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:
|
2018-01-27 00:52:20 +00:00
|
|
|
sock.bind((host, port))
|
2018-01-20 07:23:09 +00:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno is 98:
|
|
|
|
retVal = True
|
|
|
|
finally:
|
|
|
|
sock.close()
|
2018-01-20 17:59:44 +00:00
|
|
|
return retVal
|
2018-01-27 00:10:38 +00:00
|
|
|
def checkIsIP(self, ip):
|
2018-01-27 01:16:15 +00:00
|
|
|
'''Check if a string is a valid ipv4 address'''
|
2018-01-27 00:10:38 +00:00
|
|
|
try:
|
|
|
|
socket.inet_aton(ip)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2018-01-20 17:59:44 +00:00
|
|
|
def exportMyPubkey(self):
|
|
|
|
'''Export our PGP key if it exists'''
|
|
|
|
if not os.path.exists(self.fingerprintFile):
|
|
|
|
raise Exception("No fingerprint found, cannot export our PGP key.")
|
2018-01-27 01:45:01 +00:00
|
|
|
gpg = gnupg.GPG(homedir='./data/pgp/')
|
2018-01-20 17:59:44 +00:00
|
|
|
with open(self.fingerprintFile,'r') as f:
|
|
|
|
fingerprint = f.read()
|
|
|
|
ascii_armored_public_keys = gpg.export_keys(fingerprint)
|
2018-01-26 06:28:11 +00:00
|
|
|
return ascii_armored_public_keys
|
|
|
|
|
|
|
|
def getBlockDBHash(self):
|
|
|
|
'''Return a sha3_256 hash of the blocks DB'''
|
|
|
|
with open(self._core.blockDB, 'rb') as data:
|
|
|
|
data = data.read()
|
|
|
|
hasher = hashlib.sha3_256()
|
|
|
|
hasher.update(data)
|
|
|
|
dataHash = hasher.hexdigest()
|
|
|
|
return dataHash
|
|
|
|
|
2018-01-29 02:52:48 +00:00
|
|
|
def hasBlock(self, hash):
|
|
|
|
'''detect if we have a block in the list or not'''
|
|
|
|
conn = sqlite3.connect(self._core.blockDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
if not self.validateHash(hash):
|
|
|
|
raise Exception("Invalid hash")
|
|
|
|
for result in c.execute("SELECT COUNT() FROM hashes where hash='" + hash + "'"):
|
|
|
|
if result[0] >= 1:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
|
2018-01-26 06:28:11 +00:00
|
|
|
def validateHash(self, data, length=64):
|
2018-01-27 00:52:20 +00:00
|
|
|
'''Validate if a string is a valid hex formatted hash'''
|
2018-01-26 06:28:11 +00:00
|
|
|
retVal = True
|
2018-01-28 22:38:10 +00:00
|
|
|
if data == False or data == True:
|
2018-01-28 21:59:13 +00:00
|
|
|
return False
|
2018-01-28 22:21:51 +00:00
|
|
|
data = data.strip()
|
2018-01-26 06:28:11 +00:00
|
|
|
if len(data) != length:
|
|
|
|
retVal = False
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
int(data, 16)
|
|
|
|
except ValueError:
|
|
|
|
retVal = False
|
2018-01-26 07:22:48 +00:00
|
|
|
return retVal
|
2018-01-26 09:46:21 +00:00
|
|
|
def validateID(self, id):
|
|
|
|
'''validate if a user ID is a valid tor or i2p hidden service'''
|
|
|
|
idLength = len(id)
|
|
|
|
retVal = True
|
|
|
|
idNoDomain = ''
|
2018-01-27 00:10:38 +00:00
|
|
|
peerType = ''
|
|
|
|
# i2p b32 addresses are 60 characters long (including .b32.i2p)
|
2018-01-26 09:46:21 +00:00
|
|
|
if idLength == 60:
|
2018-01-27 00:10:38 +00:00
|
|
|
peerType = 'i2p'
|
|
|
|
if not id.endswith('.b32.i2p'):
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
|
|
|
else:
|
|
|
|
idNoDomain = id.split('.b32.i2p')[0]
|
2018-01-27 00:10:38 +00:00
|
|
|
# Onion v2's are 22 (including .onion), v3's are 62 with .onion
|
2018-01-26 09:46:21 +00:00
|
|
|
elif idLength == 22 or idLength == 62:
|
2018-01-27 00:10:38 +00:00
|
|
|
peerType = 'onion'
|
|
|
|
if not id.endswith('.onion'):
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
|
|
|
else:
|
|
|
|
idNoDomain = id.split('.onion')[0]
|
|
|
|
else:
|
|
|
|
retVal = False
|
|
|
|
if retVal:
|
2018-01-27 00:10:38 +00:00
|
|
|
if peerType == 'i2p':
|
2018-01-26 09:46:21 +00:00
|
|
|
try:
|
2018-01-27 00:10:38 +00:00
|
|
|
id.split('.b32.i2p')[2]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
2018-01-27 00:10:38 +00:00
|
|
|
elif peerType == 'onion':
|
|
|
|
try:
|
|
|
|
id.split('.onion')[2]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
2018-01-27 00:10:38 +00:00
|
|
|
if not idNoDomain.isalnum():
|
|
|
|
retVal = False
|
2018-01-26 09:46:21 +00:00
|
|
|
return retVal
|