2018-08-07 07:31:53 +00:00
|
|
|
'''
|
2018-08-27 03:44:32 +00:00
|
|
|
Onionr - P2P Anonymous Storage Network
|
2018-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
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-09-01 00:51:14 +00:00
|
|
|
import onionrexceptions, onionrpeers, onionrproofs, base64, logger
|
|
|
|
from dependencies import secrets
|
2018-08-07 07:31:53 +00:00
|
|
|
class DaemonTools:
|
|
|
|
def __init__(self, daemon):
|
|
|
|
self.daemon = daemon
|
2018-08-08 19:26:02 +00:00
|
|
|
self.announceCache = {}
|
2018-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
def announceNode(self):
|
|
|
|
'''Announce our node to our peers'''
|
2018-08-08 19:26:02 +00:00
|
|
|
retData = False
|
|
|
|
|
|
|
|
# Announce to random online peers
|
|
|
|
for i in self.daemon.onlinePeers:
|
|
|
|
if not i in self.announceCache:
|
|
|
|
peer = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
peer = self.daemon.pickOnlinePeer()
|
|
|
|
|
|
|
|
ourID = self.daemon._core.hsAddress.strip()
|
|
|
|
|
|
|
|
url = 'http://' + peer + '/public/announce/'
|
|
|
|
data = {'node': ourID}
|
|
|
|
|
|
|
|
combinedNodes = ourID + peer
|
|
|
|
|
|
|
|
if peer in self.announceCache:
|
|
|
|
data['random'] = self.announceCache[peer]
|
|
|
|
else:
|
|
|
|
proof = onionrproofs.DataPOW(combinedNodes, forceDifficulty=4)
|
|
|
|
data['random'] = base64.b64encode(proof.waitForResult()[1])
|
|
|
|
self.announceCache[peer] = data['random']
|
|
|
|
|
|
|
|
logger.info('Announcing node to ' + url)
|
|
|
|
if self.daemon._core._utils.doPostRequest(url, data) == 'Success':
|
2018-10-30 22:22:06 +00:00
|
|
|
logger.info('Successfully introduced node to ' + peer)
|
2018-08-08 19:26:02 +00:00
|
|
|
retData = True
|
|
|
|
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'''
|
|
|
|
if len(self.daemon.onlinePeers) != 0:
|
2018-08-23 19:46:23 +00:00
|
|
|
if not self.daemon._core._utils.checkNetwork(torPort=self.daemon.proxyPort):
|
2018-08-21 20:01:50 +00:00
|
|
|
logger.warn('Network check failed, are you connected to the internet?')
|
|
|
|
self.daemon.isOnline = False
|
2018-08-23 17:48:49 +00:00
|
|
|
self.daemon.decrementThreadCount('netCheck')
|
|
|
|
|
|
|
|
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'''
|
|
|
|
|
2018-08-23 18:02:48 +00:00
|
|
|
while self.daemon._core._utils.storageCounter.isFull():
|
2018-08-23 18:24:32 +00:00
|
|
|
oldest = self.daemon._core.getBlockList()[0]
|
2018-08-23 18:02:48 +00:00
|
|
|
self.daemon._core._blacklist.addToDB(oldest)
|
|
|
|
self.daemon._core.removeBlock(oldest)
|
2018-09-30 04:42:31 +00:00
|
|
|
logger.info('Deleted block: %s' % (oldest,))
|
|
|
|
# Delete expired blocks
|
|
|
|
for bHash in self.daemon._core.getExpiredBlocks():
|
|
|
|
self.daemon._core._blacklist.addToDB(bHash)
|
|
|
|
self.daemon._core.removeBlock(bHash)
|
2018-08-31 22:53:48 +00:00
|
|
|
self.daemon.decrementThreadCount('cleanOldBlocks')
|
|
|
|
|
|
|
|
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
|
|
|
|
if onlinePeerAmount >= self.daemon._core.config.get('peers.maxConnect'):
|
|
|
|
finding = True
|
|
|
|
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()
|
|
|
|
self.daemon.decrementThreadCount('cooldownPeer')
|