Onionr/onionr/communicatorutils/onionrdaemontools.py

214 lines
8.6 KiB
Python
Raw Permalink Normal View History

'''
Onionr - P2P Anonymous Storage Network
Contains the CommunicatorUtils class which contains useful functions for the communicator daemon
'''
'''
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/>.
'''
2018-11-17 07:23:10 +00:00
2019-02-17 05:20:47 +00:00
import onionrexceptions, onionrpeers, onionrproofs, logger
import base64, sqlite3, os
2018-09-01 00:51:14 +00:00
from dependencies import secrets
from utils import netutils
2019-02-17 05:20:47 +00:00
from onionrusers import onionrusers
class DaemonTools:
2018-12-09 17:29:39 +00:00
'''
Class intended for use by Onionr Communicator
'''
def __init__(self, daemon):
self.daemon = daemon
2019-03-04 22:29:44 +00:00
self.announceProgress = {}
self.announceCache = {}
def announceNode(self):
'''Announce our node to our peers'''
retData = False
2018-12-09 17:29:39 +00:00
announceFail = False
2019-03-04 22:29:44 +00:00
# Do not let announceCache get too large
if len(self.announceCache) >= 10000:
self.announceCache.popitem()
2019-01-28 22:49:04 +00:00
if self.daemon._core.config.get('general.security_level', 0) == 0:
# Announce to random online peers
for i in self.daemon.onlinePeers:
2019-03-04 22:29:44 +00:00
if not i in self.announceCache and not i in self.announceProgress:
peer = i
break
else:
peer = self.daemon.pickOnlinePeer()
2019-02-02 23:10:04 +00:00
for x in range(1):
if x == 1 and self.daemon._core.config.get('i2p.host'):
ourID = self.daemon._core.config.get('i2p.own_addr').strip()
else:
ourID = self.daemon._core.hsAddress.strip()
2019-02-02 23:10:04 +00:00
url = 'http://' + peer + '/announce'
data = {'node': ourID}
2019-02-02 23:10:04 +00:00
combinedNodes = ourID + peer
if ourID != 1:
#TODO: Extend existingRand for i2p
existingRand = self.daemon._core.getAddressInfo(peer, 'powValue')
if type(existingRand) is type(None):
existingRand = ''
2019-02-02 23:10:04 +00:00
if peer in self.announceCache:
data['random'] = self.announceCache[peer]
elif len(existingRand) > 0:
data['random'] = existingRand
else:
2019-03-04 22:29:44 +00:00
self.announceProgress[peer] = True
2019-02-02 23:10:04 +00:00
proof = onionrproofs.DataPOW(combinedNodes, forceDifficulty=4)
2019-03-04 22:29:44 +00:00
del self.announceProgress[peer]
2019-02-02 23:10:04 +00:00
try:
data['random'] = base64.b64encode(proof.waitForResult()[1])
except TypeError:
# Happens when we failed to produce a proof
logger.error("Failed to produce a pow for announcing to " + peer)
announceFail = True
else:
self.announceCache[peer] = data['random']
if not announceFail:
logger.info('Announcing node to ' + url)
if self.daemon._core._utils.doPostRequest(url, data) == 'Success':
logger.info('Successfully introduced node to ' + peer)
retData = True
self.daemon._core.setAddressInfo(peer, 'introduced', 1)
self.daemon._core.setAddressInfo(peer, 'powValue', data['random'])
2019-01-20 02:23:26 +00:00
self.daemon.decrementThreadCount('announceNode')
2018-08-21 20:01:50 +00:00
return retData
def netCheck(self):
'''Check if we are connected to the internet or not when we can't connect to any peers'''
2018-11-09 05:22:43 +00:00
if len(self.daemon.onlinePeers) == 0:
if not netutils.checkNetwork(self.daemon._core._utils, torPort=self.daemon.proxyPort):
2019-02-22 21:04:03 +00:00
if not self.daemon.shutdown:
logger.warn('Network check failed, are you connected to the internet?')
2018-08-21 20:01:50 +00:00
self.daemon.isOnline = False
2018-12-09 17:29:39 +00:00
else:
self.daemon.isOnline = True
self.daemon.decrementThreadCount('netCheck')
2018-09-24 23:48:00 +00:00
def cleanOldBlocks(self):
2018-09-30 04:42:31 +00:00
'''Delete old blocks if our disk allocation is full/near full, and also expired blocks'''
2019-03-15 16:48:06 +00:00
# Delete expired blocks
for bHash in self.daemon._core.getExpiredBlocks():
self.daemon._core._blacklist.addToDB(bHash)
self.daemon._core.removeBlock(bHash)
logger.info('Deleted block: %s' % (bHash,))
while self.daemon._core._utils.storageCounter.isFull():
oldest = self.daemon._core.getBlockList()[0]
self.daemon._core._blacklist.addToDB(oldest)
self.daemon._core.removeBlock(oldest)
2018-09-24 23:48:00 +00:00
logger.info('Deleted block: %s' % (oldest,))
2018-08-31 22:53:48 +00:00
self.daemon.decrementThreadCount('cleanOldBlocks')
2018-11-09 19:07:26 +00:00
def cleanKeys(self):
'''Delete expired forward secrecy keys'''
conn = sqlite3.connect(self.daemon._core.peerDB, timeout=10)
c = conn.cursor()
time = self.daemon._core._utils.getEpoch()
deleteKeys = []
2018-11-17 07:23:10 +00:00
for entry in c.execute("SELECT * FROM forwardKeys WHERE expire <= ?", (time,)):
2018-12-09 17:29:39 +00:00
logger.debug('Forward key: %s' % entry[1])
2018-11-09 19:07:26 +00:00
deleteKeys.append(entry[1])
2018-11-09 19:07:26 +00:00
for key in deleteKeys:
2018-12-09 17:29:39 +00:00
logger.debug('Deleting forward key %s' % key)
2018-11-09 19:07:26 +00:00
c.execute("DELETE from forwardKeys where forwardKey = ?", (key,))
conn.commit()
conn.close()
onionrusers.deleteExpiredKeys(self.daemon._core)
self.daemon.decrementThreadCount('cleanKeys')
2018-08-31 22:53:48 +00:00
def cooldownPeer(self):
'''Randomly add an online peer to cooldown, so we can connect a new one'''
onlinePeerAmount = len(self.daemon.onlinePeers)
minTime = 300
cooldownTime = 600
toCool = ''
tempConnectTimes = dict(self.daemon.connectTimes)
# Remove peers from cooldown that have been there long enough
tempCooldown = dict(self.daemon.cooldownPeer)
for peer in tempCooldown:
if (self.daemon._core._utils.getEpoch() - tempCooldown[peer]) >= cooldownTime:
del self.daemon.cooldownPeer[peer]
# Cool down a peer, if we have max connections alive for long enough
2018-12-09 17:29:39 +00:00
if onlinePeerAmount >= self.daemon._core.config.get('peers.max_connect', 10, save = True):
2018-08-31 22:53:48 +00:00
finding = True
2018-11-17 07:23:10 +00:00
2018-08-31 22:53:48 +00:00
while finding:
try:
toCool = min(tempConnectTimes, key=tempConnectTimes.get)
if (self.daemon._core._utils.getEpoch() - tempConnectTimes[toCool]) < minTime:
del tempConnectTimes[toCool]
else:
finding = False
except ValueError:
break
else:
self.daemon.removeOnlinePeer(toCool)
self.daemon.cooldownPeer[toCool] = self.daemon._core._utils.getEpoch()
2018-11-17 07:23:10 +00:00
2018-09-24 23:48:00 +00:00
self.daemon.decrementThreadCount('cooldownPeer')
def runCheck(self):
2018-12-13 04:35:01 +00:00
if os.path.isfile(self.daemon._core.dataDir + '.runcheck'):
os.remove(self.daemon._core.dataDir + '.runcheck')
return True
return False
def humanReadableTime(self, seconds):
build = ''
units = {
'year' : 31557600,
'month' : (31557600 / 12),
'day' : 86400,
'hour' : 3600,
'minute' : 60,
'second' : 1
}
for unit in units:
amnt_unit = int(seconds / units[unit])
if amnt_unit >= 1:
seconds -= amnt_unit * units[unit]
build += '%s %s' % (amnt_unit, unit) + ('s' if amnt_unit != 1 else '') + ' '
return build.strip()
2018-12-09 17:29:39 +00:00
def insertDeniableBlock(self):
'''Insert a fake block in order to make it more difficult to track real blocks'''
fakePeer = ''
2018-12-09 17:29:39 +00:00
chance = 10
if secrets.randbelow(chance) == (chance - 1):
fakePeer = 'OVPCZLOXD6DC5JHX4EQ3PSOGAZ3T24F75HQLIUZSDSMYPEOXCPFA===='
2018-12-09 17:29:39 +00:00
data = secrets.token_hex(secrets.randbelow(500) + 1)
self.daemon._core.insertBlock(data, header='pm', encryptType='asym', asymPeer=fakePeer, meta={'subject': 'foo'})
2018-12-09 17:29:39 +00:00
self.daemon.decrementThreadCount('insertDeniableBlock')
return