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
|
2019-12-20 08:59:27 +00:00
|
|
|
"""
|
2019-11-30 08:42:49 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import config
|
|
|
|
import logger
|
|
|
|
import onionrpeers
|
|
|
|
import onionrplugins as plugins
|
2020-07-29 08:57:06 +00:00
|
|
|
from . import onlinepeers
|
|
|
|
from . import uploadqueue
|
2019-11-30 08:42:49 +00:00
|
|
|
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
|
2020-07-30 01:23:48 +00:00
|
|
|
from onionrthreads import add_onionr_thread
|
2020-06-15 00:08:17 +00:00
|
|
|
from onionrcommands.openwebinterface import get_url
|
2019-11-30 08:42:49 +00:00
|
|
|
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-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:
|
2020-11-19 04:16:37 +00:00
|
|
|
developmentMode = config.get(
|
|
|
|
'general.dev_mode', False)
|
2019-11-30 08:42:49 +00:00
|
|
|
|
2018-11-11 03:25:40 +00:00
|
|
|
# configure logger and stuff
|
2019-07-20 06:02:30 +00:00
|
|
|
self.config = config
|
2019-11-30 08:42:49 +00:00
|
|
|
self.shared_state = shared_state # TooManyObjects module
|
2020-11-16 06:57:38 +00:00
|
|
|
shared_state.add(self)
|
2018-08-21 20:01:50 +00:00
|
|
|
|
2020-07-24 19:37:01 +00:00
|
|
|
# populate kv values
|
2020-07-26 03:28:32 +00:00
|
|
|
self.kv = self.shared_state.get_by_string('DeadSimpleKV')
|
2020-07-24 19:37:01 +00:00
|
|
|
|
2019-12-18 10:05:37 +00:00
|
|
|
if config.get('general.offline_mode', False):
|
2020-07-29 09:32:09 +00:00
|
|
|
self.kv.put('isOnline', False)
|
2019-12-18 10:05:37 +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-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
|
2020-12-03 05:20:03 +00:00
|
|
|
self.delay = 5
|
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
|
|
|
|
2018-06-13 07:33:37 +00:00
|
|
|
# Loads in and starts the enabled plugins
|
|
|
|
plugins.reload()
|
|
|
|
|
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
|
|
|
|
2020-11-13 08:17:48 +00:00
|
|
|
add_onionr_thread(
|
2019-12-20 08:59:27 +00:00
|
|
|
lookupblocks.lookup_blocks_from_communicator,
|
2020-11-13 08:17:48 +00:00
|
|
|
[self.shared_state], 25, 3)
|
|
|
|
|
2020-11-15 18:26:25 +00:00
|
|
|
add_onionr_thread(
|
|
|
|
downloadblocks.download_blocks_from_communicator,
|
|
|
|
[self.shared_state],
|
|
|
|
config.get('timers.getBlocks', 10), 1)
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2020-11-13 08:17:48 +00:00
|
|
|
add_onionr_thread(onlinepeers.clear_offline_peer, [self.kv], 58)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2020-11-15 18:26:25 +00:00
|
|
|
add_onionr_thread(
|
2020-11-16 06:57:38 +00:00
|
|
|
housekeeping.clean_old_blocks, [self.shared_state], 10, 1)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2020-11-15 18:52:52 +00:00
|
|
|
# Discover new peers
|
|
|
|
add_onionr_thread(
|
|
|
|
lookupadders.lookup_new_peer_transports_with_communicator,
|
|
|
|
[shared_state], 60, 3)
|
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
|
2020-11-16 06:57:38 +00:00
|
|
|
add_onionr_thread(
|
|
|
|
cooldownpeer.cooldown_peer, [self.shared_state], 30, 60)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
|
|
|
# Timer to read the upload queue and upload the entries to peers
|
2020-11-16 06:57:38 +00:00
|
|
|
add_onionr_thread(
|
|
|
|
uploadblocks.upload_blocks_from_communicator,
|
|
|
|
[self.shared_state], 5, 1)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
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):
|
2020-11-21 05:31:19 +00:00
|
|
|
add_onionr_thread(
|
|
|
|
deniableinserts.insert_deniable_block, [], 180, 10)
|
2018-12-09 17:29:39 +00:00
|
|
|
|
2020-10-22 12:45:19 +00:00
|
|
|
if config.get('transports.tor', True):
|
|
|
|
# Timer to check for connectivity,
|
|
|
|
# through Tor to various high-profile onion services
|
2020-11-19 04:16:37 +00:00
|
|
|
add_onionr_thread(netcheck.net_check, [shared_state], 500, 60)
|
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
|
2020-11-21 05:31:19 +00:00
|
|
|
add_onionr_thread(
|
|
|
|
announcenode.announce_node, [self.shared_state], 600, 60)
|
2018-12-09 17:29:39 +00:00
|
|
|
else:
|
|
|
|
logger.debug('Will not announce node.')
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2020-11-21 05:31:19 +00:00
|
|
|
add_onionr_thread(onionrpeers.peer_cleanup, [], 300, 300)
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2020-11-21 05:31:19 +00:00
|
|
|
add_onionr_thread(housekeeping.clean_keys, [], 15, 1)
|
2018-06-13 03:43:39 +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(
|
2020-07-31 01:15:36 +00:00
|
|
|
self.kv, [], 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)
|
2020-06-15 00:08:17 +00:00
|
|
|
get_url()
|
2019-11-30 08:42:49 +00:00
|
|
|
|
2020-02-11 22:47:30 +00:00
|
|
|
while not config.get('onboarding.done', True) and \
|
2020-07-26 02:36:48 +00:00
|
|
|
not self.shared_state.get_by_string(
|
|
|
|
'DeadSimpleKV').get('shutdown'):
|
2020-02-12 01:49:44 +00:00
|
|
|
try:
|
|
|
|
time.sleep(2)
|
|
|
|
except KeyboardInterrupt:
|
2020-07-26 02:36:48 +00:00
|
|
|
self.shared_state.get_by_string(
|
|
|
|
'DeadSimpleKV').put('shutdown', True)
|
2019-11-30 08:42:49 +00:00
|
|
|
|
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:
|
2020-07-26 02:36:48 +00:00
|
|
|
while not self.shared_state.get_by_string(
|
|
|
|
'DeadSimpleKV').get('shutdown'):
|
2018-07-08 00:26:01 +00:00
|
|
|
time.sleep(self.delay)
|
|
|
|
except KeyboardInterrupt:
|
2020-07-26 02:36:48 +00:00
|
|
|
self.shared_state.get_by_string(
|
|
|
|
'DeadSimpleKV').put('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)
|
2020-11-21 05:31:19 +00:00
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
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-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"""
|
2020-07-29 08:57:06 +00:00
|
|
|
for i in self.kv.get('peerProfiles'):
|
2018-07-30 22:48:29 +00:00
|
|
|
# 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)
|
2020-07-29 08:57:06 +00:00
|
|
|
self.kv.get('peerProfiles').append(retData)
|
2018-06-15 05:45:07 +00:00
|
|
|
return retData
|
|
|
|
|
2019-12-20 08:59:27 +00:00
|
|
|
|
2019-08-04 04:52:57 +00:00
|
|
|
def startCommunicator(shared_state):
|
|
|
|
OnionrCommunicatorDaemon(shared_state)
|