2020-01-27 08:09:28 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2018-06-10 08:00:01 +00:00
|
|
|
|
2020-01-27 08:09:28 +00:00
|
|
|
This file contains both the OnionrCommunicate class for
|
|
|
|
communcating with peers and code to operate as a daemon,
|
|
|
|
getting commands from the command queue database
|
|
|
|
(see core.Core.daemonQueue)
|
2019-12-20 08:59:27 +00:00
|
|
|
"""
|
2019-11-30 08:42:49 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
import config
|
|
|
|
import logger
|
|
|
|
import onionrpeers
|
|
|
|
import onionrplugins as plugins
|
|
|
|
from . import onlinepeers, uploadqueue
|
|
|
|
from communicatorutils import servicecreator
|
|
|
|
from communicatorutils import onionrcommunicatortimers
|
|
|
|
from communicatorutils import downloadblocks
|
|
|
|
from communicatorutils import lookupblocks
|
|
|
|
from communicatorutils import lookupadders
|
|
|
|
from communicatorutils import connectnewpeers
|
|
|
|
from communicatorutils import uploadblocks
|
|
|
|
from communicatorutils import announcenode, deniableinserts
|
|
|
|
from communicatorutils import cooldownpeer
|
|
|
|
from communicatorutils import housekeeping
|
|
|
|
from communicatorutils import netcheck
|
|
|
|
from onionrutils import localcommand
|
|
|
|
from onionrutils import epoch
|
|
|
|
from etc import humanreadabletime
|
|
|
|
import onionrservices
|
|
|
|
import filepaths
|
|
|
|
from onionrblocks import storagecounter
|
|
|
|
from coredb import dbfiles
|
|
|
|
from netcontroller import NetController
|
2019-12-20 08:59:27 +00:00
|
|
|
from . import bootstrappeers
|
2020-01-03 10:17:00 +00:00
|
|
|
from . import daemoneventhooks
|
2019-12-20 08:59:27 +00:00
|
|
|
"""
|
2018-06-10 08:00:01 +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/>.
|
2019-12-20 08:59:27 +00:00
|
|
|
"""
|
2019-11-16 04:18:38 +00:00
|
|
|
|
2019-02-28 03:02:44 +00:00
|
|
|
OnionrCommunicatorTimers = onionrcommunicatortimers.OnionrCommunicatorTimers
|
|
|
|
|
2019-01-20 18:09:53 +00:00
|
|
|
config.reload()
|
2019-12-20 08:59:27 +00:00
|
|
|
|
|
|
|
|
2018-06-11 07:40:45 +00:00
|
|
|
class OnionrCommunicatorDaemon:
|
2019-11-30 08:42:49 +00:00
|
|
|
def __init__(self, shared_state, developmentMode=None):
|
|
|
|
if developmentMode is None:
|
|
|
|
developmentMode = config.get('general.dev_mode', False)
|
|
|
|
|
2018-11-11 03:25:40 +00:00
|
|
|
# configure logger and stuff
|
2019-07-20 06:02:30 +00:00
|
|
|
self.config = config
|
|
|
|
self.storage_counter = storagecounter.StorageCounter()
|
2019-11-30 08:42:49 +00:00
|
|
|
self.isOnline = True # Assume we're connected to the internet
|
|
|
|
self.shared_state = shared_state # TooManyObjects module
|
2018-08-21 20:01:50 +00:00
|
|
|
|
2019-12-18 10:05:37 +00:00
|
|
|
if config.get('general.offline_mode', False):
|
|
|
|
self.isOnline = False
|
|
|
|
|
2018-07-01 21:01:19 +00:00
|
|
|
# list of timer instances
|
2018-06-12 23:32:33 +00:00
|
|
|
self.timers = []
|
2018-07-01 21:01:19 +00:00
|
|
|
|
2019-02-12 19:18:08 +00:00
|
|
|
# initialize core with Tor socks port being 3rd argument
|
2019-08-04 04:52:57 +00:00
|
|
|
self.proxyPort = shared_state.get(NetController).socksPort
|
2018-07-01 21:01:19 +00:00
|
|
|
|
2019-09-16 07:50:13 +00:00
|
|
|
# Upload information, list of blocks to upload
|
2018-11-13 17:07:46 +00:00
|
|
|
self.blocksToUpload = []
|
2019-12-20 08:59:27 +00:00
|
|
|
self.upload_session_manager = self.shared_state.get(
|
|
|
|
uploadblocks.sessionmanager.BlockUploadSessionManager)
|
2019-09-17 01:16:06 +00:00
|
|
|
self.shared_state.share_object()
|
2018-07-01 21:01:19 +00:00
|
|
|
|
|
|
|
# loop time.sleep delay in seconds
|
2018-06-12 23:32:33 +00:00
|
|
|
self.delay = 1
|
2018-07-01 21:01:19 +00:00
|
|
|
|
|
|
|
# lists of connected peers and peers we know we can't reach currently
|
2018-06-13 22:22:48 +00:00
|
|
|
self.onlinePeers = []
|
2018-06-16 20:54:56 +00:00
|
|
|
self.offlinePeers = []
|
2018-08-31 22:53:48 +00:00
|
|
|
self.cooldownPeer = {}
|
|
|
|
self.connectTimes = {}
|
2019-11-30 08:42:49 +00:00
|
|
|
# list of peer's profiles (onionrpeers.PeerProfile instances)
|
|
|
|
self.peerProfiles = []
|
|
|
|
# Peers merged to us. Don't add to db until we know they're reachable
|
|
|
|
self.newPeers = []
|
2019-06-13 02:35:30 +00:00
|
|
|
self.announceProgress = {}
|
|
|
|
self.announceCache = {}
|
2018-06-13 22:22:48 +00:00
|
|
|
|
2019-08-13 22:28:53 +00:00
|
|
|
self.generating_blocks = []
|
|
|
|
|
2018-07-01 21:01:19 +00:00
|
|
|
# amount of threads running by name, used to prevent too many
|
2018-06-13 07:33:37 +00:00
|
|
|
self.threadCounts = {}
|
2018-07-06 04:27:12 +00:00
|
|
|
|
2019-02-12 19:18:08 +00:00
|
|
|
# set true when shutdown command received
|
2018-06-13 03:43:39 +00:00
|
|
|
self.shutdown = False
|
2018-06-15 05:45:07 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# list of new blocks to download
|
|
|
|
# added to when new block lists are fetched from peers
|
2019-01-22 17:40:27 +00:00
|
|
|
self.blockQueue = {}
|
2018-07-06 04:27:12 +00:00
|
|
|
|
2018-07-13 21:02:41 +00:00
|
|
|
# list of blocks currently downloading, avoid s
|
|
|
|
self.currentDownloading = []
|
|
|
|
|
2018-12-09 17:29:39 +00:00
|
|
|
# timestamp when the last online node was seen
|
|
|
|
self.lastNodeSeen = None
|
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Dict of time stamps for peer's block list lookup times,
|
|
|
|
# to avoid downloading full lists all the time
|
2019-01-16 05:57:47 +00:00
|
|
|
self.dbTimestamps = {}
|
|
|
|
|
2018-06-13 07:33:37 +00:00
|
|
|
# Loads in and starts the enabled plugins
|
|
|
|
plugins.reload()
|
|
|
|
|
2019-01-20 18:09:53 +00:00
|
|
|
# time app started running for info/statistics purposes
|
2019-06-25 23:07:35 +00:00
|
|
|
self.startTime = epoch.get_epoch()
|
2018-09-15 01:05:25 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# extends our upload list and saves our list when Onionr exits
|
|
|
|
uploadqueue.UploadQueue(self)
|
2019-08-12 03:32:58 +00:00
|
|
|
|
2019-01-20 18:09:53 +00:00
|
|
|
if developmentMode:
|
2018-10-30 22:22:06 +00:00
|
|
|
OnionrCommunicatorTimers(self, self.heartbeat, 30)
|
2018-07-06 04:27:12 +00:00
|
|
|
|
2018-07-02 04:04:14 +00:00
|
|
|
# Set timers, function reference, seconds
|
2019-12-20 08:59:27 +00:00
|
|
|
# requires_peer True means the timer function won't fire if we
|
|
|
|
# have no connected peers
|
|
|
|
peerPoolTimer = OnionrCommunicatorTimers(
|
|
|
|
self, onlinepeers.get_online_peers, 60, max_threads=1,
|
|
|
|
my_args=[self])
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Timers to periodically lookup new blocks and download them
|
2019-12-20 08:59:27 +00:00
|
|
|
lookup_blocks_timer = OnionrCommunicatorTimers(
|
|
|
|
self,
|
|
|
|
lookupblocks.lookup_blocks_from_communicator,
|
|
|
|
config.get('timers.lookupBlocks', 25),
|
|
|
|
my_args=[self], requires_peer=True, max_threads=1)
|
|
|
|
|
|
|
|
"""The block download timer is accessed by the block lookup function
|
|
|
|
to trigger faster download starts"""
|
|
|
|
self.download_blocks_timer = OnionrCommunicatorTimers(
|
|
|
|
self, self.getBlocks, config.get('timers.getBlocks', 10),
|
|
|
|
requires_peer=True, max_threads=5)
|
|
|
|
|
|
|
|
# Timer to reset the longest offline peer
|
|
|
|
# so contact can be attempted again
|
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, onlinepeers.clear_offline_peer, 58, my_args=[self])
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Timer to cleanup old blocks
|
2019-12-20 08:59:27 +00:00
|
|
|
blockCleanupTimer = OnionrCommunicatorTimers(
|
|
|
|
self, housekeeping.clean_old_blocks, 20, my_args=[self])
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Timer to discover new peers
|
2019-12-20 08:59:27 +00:00
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, lookupadders.lookup_new_peer_transports_with_communicator,
|
|
|
|
60, requires_peer=True, my_args=[self], max_threads=2)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Timer for adjusting which peers
|
|
|
|
# we actively communicate to at any given time,
|
|
|
|
# to avoid over-using peers
|
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, cooldownpeer.cooldown_peer, 30,
|
|
|
|
my_args=[self], requires_peer=True)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Timer to read the upload queue and upload the entries to peers
|
2019-12-20 08:59:27 +00:00
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, uploadblocks.upload_blocks_from_communicator,
|
|
|
|
5, my_args=[self], requires_peer=True, max_threads=1)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Setup direct connections
|
2019-03-24 02:56:46 +00:00
|
|
|
if config.get('general.socket_servers', False):
|
2019-07-18 17:40:48 +00:00
|
|
|
self.services = onionrservices.OnionrServices()
|
2019-03-24 02:56:46 +00:00
|
|
|
self.active_services = []
|
2019-03-26 04:25:46 +00:00
|
|
|
self.service_greenlets = []
|
2019-12-20 08:59:27 +00:00
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, servicecreator.service_creator, 5,
|
|
|
|
max_threads=50, my_args=[self])
|
2019-03-24 02:56:46 +00:00
|
|
|
else:
|
|
|
|
self.services = None
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2019-08-16 20:41:56 +00:00
|
|
|
# {peer_pubkey: ephemeral_address}, the address to reach them
|
|
|
|
self.direct_connection_clients = {}
|
2019-12-20 08:59:27 +00:00
|
|
|
|
|
|
|
# This timer creates deniable blocks,
|
|
|
|
# in an attempt to further obfuscate block insertion metadata
|
2019-06-13 02:35:30 +00:00
|
|
|
if config.get('general.insert_deniable_blocks', True):
|
2019-12-20 08:59:27 +00:00
|
|
|
deniableBlockTimer = OnionrCommunicatorTimers(
|
|
|
|
self, deniableinserts.insert_deniable_block,
|
|
|
|
180, my_args=[self], requires_peer=True, max_threads=1)
|
2019-06-13 02:35:30 +00:00
|
|
|
deniableBlockTimer.count = (deniableBlockTimer.frequency - 175)
|
2018-12-09 17:29:39 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Timer to check for connectivity,
|
|
|
|
# through Tor to various high-profile onion services
|
2019-12-18 10:06:52 +00:00
|
|
|
OnionrCommunicatorTimers(self, netcheck.net_check, 500,
|
|
|
|
my_args=[self], max_threads=1)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Announce the public API server transport address
|
|
|
|
# to other nodes if security level allows
|
|
|
|
if config.get('general.security_level', 1) == 0 \
|
|
|
|
and config.get('general.announce_node', True):
|
2019-05-11 18:32:56 +00:00
|
|
|
# Default to high security level incase config breaks
|
2019-12-20 08:59:27 +00:00
|
|
|
announceTimer = OnionrCommunicatorTimers(
|
|
|
|
self,
|
|
|
|
announcenode.announce_node,
|
|
|
|
3600, my_args=[self], requires_peer=True, max_threads=1)
|
2020-01-20 03:10:01 +00:00
|
|
|
announceTimer.count = (announceTimer.frequency - 60)
|
2018-12-09 17:29:39 +00:00
|
|
|
else:
|
|
|
|
logger.debug('Will not announce node.')
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2019-05-11 18:32:56 +00:00
|
|
|
# Timer to delete malfunctioning or long-dead peers
|
2019-12-20 08:59:27 +00:00
|
|
|
cleanupTimer = OnionrCommunicatorTimers(
|
|
|
|
self, self.peerCleanup, 300, requires_peer=True)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Timer to cleanup dead ephemeral forward secrecy keys
|
|
|
|
OnionrCommunicatorTimers(
|
|
|
|
self, housekeeping.clean_keys, 15, my_args=[self], max_threads=1)
|
2018-07-09 07:02:33 +00:00
|
|
|
|
2019-05-11 18:32:56 +00:00
|
|
|
# Adjust initial timer triggers
|
2018-07-23 07:43:10 +00:00
|
|
|
peerPoolTimer.count = (peerPoolTimer.frequency - 1)
|
2018-08-03 06:28:26 +00:00
|
|
|
cleanupTimer.count = (cleanupTimer.frequency - 60)
|
2019-08-18 05:54:52 +00:00
|
|
|
blockCleanupTimer.count = (blockCleanupTimer.frequency - 2)
|
2019-09-05 09:40:31 +00:00
|
|
|
lookup_blocks_timer = (lookup_blocks_timer.frequency - 2)
|
2018-06-13 03:43:39 +00:00
|
|
|
|
2019-08-04 04:52:57 +00:00
|
|
|
shared_state.add(self)
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2020-01-20 03:10:01 +00:00
|
|
|
if config.get('general.use_bootstrap_list', True):
|
2019-12-20 08:59:27 +00:00
|
|
|
bootstrappeers.add_bootstrap_list_to_peer_list(
|
|
|
|
self, [], db_only=True)
|
2019-08-04 04:52:57 +00:00
|
|
|
|
2020-01-03 10:17:00 +00:00
|
|
|
daemoneventhooks.daemon_event_handlers(shared_state)
|
|
|
|
|
2019-11-30 08:42:49 +00:00
|
|
|
if not config.get('onboarding.done', True):
|
2019-12-20 08:59:27 +00:00
|
|
|
logger.info(
|
|
|
|
'First run detected. Run openhome to get setup.',
|
|
|
|
terminal=True)
|
2019-11-30 08:42:49 +00:00
|
|
|
|
|
|
|
while not config.get('onboarding.done', True):
|
|
|
|
time.sleep(5)
|
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
# Main daemon loop, mainly for calling timers,
|
|
|
|
# don't do any complex operations here to avoid locking
|
2018-07-08 00:26:01 +00:00
|
|
|
try:
|
|
|
|
while not self.shutdown:
|
|
|
|
for i in self.timers:
|
|
|
|
if self.shutdown:
|
|
|
|
break
|
|
|
|
i.processTimer()
|
|
|
|
time.sleep(self.delay)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.shutdown = True
|
2018-07-06 04:27:12 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
logger.info(
|
|
|
|
'Goodbye. (Onionr is cleaning up, and will exit)', terminal=True)
|
2019-03-27 18:55:43 +00:00
|
|
|
try:
|
|
|
|
self.service_greenlets
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
2019-11-07 00:03:38 +00:00
|
|
|
# Stop onionr direct connection services
|
2019-03-27 18:55:43 +00:00
|
|
|
for server in self.service_greenlets:
|
|
|
|
server.stop()
|
2019-08-10 01:04:56 +00:00
|
|
|
try:
|
|
|
|
time.sleep(0.5)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2018-07-23 07:43:10 +00:00
|
|
|
|
2018-06-15 05:45:07 +00:00
|
|
|
def getBlocks(self):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""Download new blocks in queue."""
|
2019-05-09 05:27:15 +00:00
|
|
|
downloadblocks.download_blocks_from_communicator(self)
|
|
|
|
|
|
|
|
def decrementThreadCount(self, threadName):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""Decrement amount of a thread name if more than zero.
|
|
|
|
|
|
|
|
called when a function meant to be run in a thread ends
|
|
|
|
"""
|
2019-05-09 05:27:15 +00:00
|
|
|
try:
|
|
|
|
if self.threadCounts[threadName] > 0:
|
|
|
|
self.threadCounts[threadName] -= 1
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-06-15 05:45:07 +00:00
|
|
|
|
2018-07-03 08:18:07 +00:00
|
|
|
def connectNewPeer(self, peer='', useBootstrap=False):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""Adds a new random online peer to self.onlinePeers"""
|
|
|
|
connectnewpeers.connect_new_peer_to_communicator(
|
|
|
|
self, peer, useBootstrap)
|
2018-07-06 04:27:12 +00:00
|
|
|
|
2018-08-02 07:28:26 +00:00
|
|
|
def peerCleanup(self):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""This just calls onionrpeers.cleanupPeers.
|
|
|
|
|
|
|
|
Remove dead or bad peers (offline too long, too slow)"""
|
2019-08-02 23:00:04 +00:00
|
|
|
onionrpeers.peer_cleanup()
|
2018-08-03 06:28:26 +00:00
|
|
|
self.decrementThreadCount('peerCleanup')
|
2018-08-02 07:28:26 +00:00
|
|
|
|
2018-07-30 22:48:29 +00:00
|
|
|
def getPeerProfileInstance(self, peer):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""Gets a peer profile instance from the list of profiles"""
|
2018-07-30 22:48:29 +00:00
|
|
|
for i in self.peerProfiles:
|
|
|
|
# if the peer's profile is already loaded, return that
|
|
|
|
if i.address == peer:
|
|
|
|
retData = i
|
|
|
|
break
|
|
|
|
else:
|
2019-12-20 08:59:27 +00:00
|
|
|
# if the peer's profile is not loaded, return a new one.
|
|
|
|
# connectNewPeer also adds it to the list on connect
|
2019-07-18 17:40:48 +00:00
|
|
|
retData = onionrpeers.PeerProfiles(peer)
|
2019-08-06 21:19:26 +00:00
|
|
|
self.peerProfiles.append(retData)
|
2018-06-15 05:45:07 +00:00
|
|
|
return retData
|
|
|
|
|
2019-01-20 22:54:04 +00:00
|
|
|
def getUptime(self):
|
2019-06-25 23:07:35 +00:00
|
|
|
return epoch.get_epoch() - self.startTime
|
2019-01-20 22:54:04 +00:00
|
|
|
|
2018-06-12 23:32:33 +00:00
|
|
|
def heartbeat(self):
|
2019-12-20 08:59:27 +00:00
|
|
|
"""Show a heartbeat debug message."""
|
|
|
|
logger.debug('Heartbeat. Node running for %s.' %
|
|
|
|
humanreadabletime.human_readable_time(self.getUptime()))
|
2018-06-13 22:22:48 +00:00
|
|
|
self.decrementThreadCount('heartbeat')
|
2018-06-13 07:33:37 +00:00
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2019-08-04 04:52:57 +00:00
|
|
|
def startCommunicator(shared_state):
|
|
|
|
OnionrCommunicatorDaemon(shared_state)
|
2019-06-13 06:58:17 +00:00
|
|
|
|