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 onionrplugins as plugins
|
|
|
|
from communicatorutils import uploadblocks
|
2021-01-17 00:40:04 +00:00
|
|
|
from . import uploadqueue
|
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-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)
|
|
|
|
|
2021-01-16 02:08:36 +00:00
|
|
|
get_url()
|
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
|
|
|
|
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
|
|
|
|
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)
|