+ Add pijion support

* Fix shutdown
This commit is contained in:
Kevin F 2022-01-30 23:59:34 -06:00
parent 1d22b43ef9
commit fd93f6151e
8 changed files with 22 additions and 166 deletions

View File

@ -1,105 +0,0 @@
from typing import NamedTuple, List, Set
from collections import namedtuple
from hashlib import sha3_256
from secrets import randbelow
from random import SystemRandom
from threading import Thread
from time import sleep
import uuid
WeightedEdge = namedtuple("WeightedEdge", ["node", "weight"])
message_pool: Set["Message"] = set() # This is just to save memory for the simulation
def get_message(hash_id):
for m in message_pool:
if hash_id == m.hash_id:
return m
raise ValueError
class Node:
def __init__(self, hidden_node: bool,
edges: List[WeightedEdge], graph_ids=["main"]):
self.hidden_node = hidden_node
self.edges = edges
self.node_id = str(uuid.uuid4())
self._messages: List[str] = list() # hashes point to message_pool
self._hidden_messages: List[str] = list()
def sharing_messages(self):
share = list(self._messages)
for m in self._hidden_messages:
share.remove(m)
return share
def add_message(self, hash_id):
if hash_id not in self._messages:
self._messages.append(hash_id)
def pick_edge(self):
selected = self.edges[0]
alt = []
for weighted_edge in self.edges[1:]:
if weighted_edge.weight[0] > selected.weight:
selected = weighted_edge
elif weighted_edge.weight[0] == selected.weight:
alt.append(selected)
alt.append(selected)
SystemRandom().shuffle(alt)
return alt[0]
def share_to_edges(self, message_id):
edge = self.pick_edge()
edge.node.add_message(message_id)
edge.weight[0] += 1
def lookup_messages(self):
edge = self.pick_edge()
for message in edge.node.sharing_messages():
if message.hash_id not in self._messages:
self._messages.append(message.hash_id)
print(self.node_id, "found", message.hash_id)
edge.weight[0] += 1
def make_new_connections(self):
edge = self.pick_edge()
for new_edge in edge.node.edges:
if new_edge not in self.edges:
self.edges.append(WeightedEdge(new_edge.node, [0]))
print(self.node_id, "added peer", new_edge.node.node_id)
edge.weight[0] += 1
class Message:
def __init__(self, data):
def _do_hash(data):
h = sha3_256()
h.update(data)
return h.digest()
self.hash_id = _do_hash(data)
self.data = data
bootstrap_node = Node(False, [])
graph = [WeightedEdge(bootstrap_node, [0])]
for i in range(10):
graph.append(WeightedEdge(Node(False, [WeightedEdge(bootstrap_node, [0])]), [0]))
m = Message(b"hello world")
message_pool.add(m)
bootstrap_node.add_message(m.hash_id)
def node_driver(node):
while True:
node.make_new_connections()
node.lookup_messages()
sleep(1)
for edge in graph:
Thread(target=node_driver, args=[edge.node], daemon=True).start()
input()

View File

@ -5,7 +5,17 @@ This file initializes Onionr when ran to be a daemon or with commands
Run with 'help' for usage. Run with 'help' for usage.
""" """
# Enable pyjion if we can because why not
pyjion_enabled = False
try:
import pyjion
pyjion.enable()
pyjion_enabled = True
except ImportError:
pass
import sys import sys
try: try:
import sqlite3 import sqlite3
except ModuleNotFoundError: except ModuleNotFoundError:

View File

@ -51,6 +51,6 @@ def register_private_blueprints(private_api, app):
app.register_blueprint( app.register_blueprint(
private_api._too_many.get_by_string('DaemonEventsBP').flask_bp) private_api._too_many.get_by_string('DaemonEventsBP').flask_bp)
Thread(target=_add_events_bp).start() Thread(target=_add_events_bp, daemon=True).start()
return app return app

View File

@ -58,7 +58,9 @@ def block_exec(event, info):
'apport/__init__.py', 'apport/__init__.py',
'apport/report.py' 'apport/report.py'
] ]
whitelisted_source = [] whitelisted_source = [
'ZABaAGQBZAJsAW0CWgIBAHoGZAFkA2wDWgRXAG4LBABlBXkZAQABAAEAZARaBlkAbgN3AGQFWgZkAWQGbAdtCFoIAQBkAWQDbAlaCWQBZAdsCm0LWgwBAAkAZQmgDWQIZAWhAnM1ZARaBmUIgwBkCRcAWg5kEmQMZQ9kDWUPZgRkDmQPhAVaEGQTZBBkEYQBWhFkA1MA'
]
home = identifyhome.identify_home() home = identifyhome.identify_home()
code_b64 = base64.b64encode(info[0].co_code).decode() code_b64 = base64.b64encode(info[0].co_code).decode()

View File

@ -24,7 +24,6 @@ shutdown_bp = Blueprint('shutdown', __name__)
def shutdown(client_api_inst): def shutdown(client_api_inst):
try: try:
client_api_inst.publicAPI.httpServer.stop()
client_api_inst.httpServer.stop() client_api_inst.httpServer.stop()
except AttributeError: except AttributeError:
pass pass

View File

@ -30,7 +30,6 @@ from utils import hastor
import runtests import runtests
from httpapi import daemoneventsapi from httpapi import daemoneventsapi
from .. import version from .. import version
from .getapihost import get_api_host_until_available
from utils.bettersleep import better_sleep from utils.bettersleep import better_sleep
from .killdaemon import kill_daemon # noqa from .killdaemon import kill_daemon # noqa
from .showlogo import show_logo from .showlogo import show_logo
@ -109,31 +108,19 @@ def daemon():
shared_state.get(daemoneventsapi.DaemonEventsBP) shared_state.get(daemoneventsapi.DaemonEventsBP)
Thread(target=shared_state.get(apiservers.ClientAPI).start,
daemon=True, name='client HTTP API').start()
# Init run time tester # Init run time tester
# (ensures Onionr is running right, for testing purposes) # (ensures Onionr is running right, for testing purposes)
# Run time tests are not normally run # Run time tests are not normally run
shared_state.get(runtests.OnionrRunTestManager) shared_state.get(runtests.OnionrRunTestManager)
# initialize clientAPI but dont start it yet
shared_state.get(apiservers.ClientAPI)
shared_state.share_object() # share the parent object to the threads shared_state.share_object() # share the parent object to the threads
show_logo() show_logo()
# since we randomize loopback API server hostname to protect against attacks,
# we have to wait for it to become set
apiHost = ''
if not offline_mode:
apiHost = get_api_host_until_available()
security_level = config.get('general.security_level', 1) security_level = config.get('general.security_level', 1)
use_existing_tor = config.get('tor.use_existing_tor', False)
_show_info_messages() _show_info_messages()
logger.info( logger.info(
@ -141,8 +128,11 @@ def daemon():
events.event('init', threaded=False) events.event('init', threaded=False)
events.event('daemon_start') events.event('daemon_start')
try:
shared_state.get(apiservers.ClientAPI).start()
except KeyboardInterrupt:
pass
better_sleep(5)
cleanup.delete_run_files() cleanup.delete_run_files()
if security_level >= 2: if security_level >= 2:

View File

@ -1,38 +0,0 @@
"""Onionr - Private P2P Communication.
Wait for the api host to be available in the public api host file.
returns string of ip for the local public host interface
"""
from time import sleep
from filepaths import public_API_host_file
"""
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/>.
"""
def get_api_host_until_available() -> str:
"""Wait for the api host to be available in the public api host file.
returns string of ip for the local public host interface
"""
api_host = ''
while api_host == '':
try:
with open(public_API_host_file, 'r') as file:
api_host = file.read()
except FileNotFoundError:
pass
sleep(0.5)
return api_host

View File

@ -45,13 +45,11 @@ def kill_daemon():
try: try:
spawn( spawn(
localcommand.local_command, localcommand.local_command,
'/shutdownclean' '/shutdown'
).get(timeout=5) ).get(timeout=5)
except sqlite3.OperationalError: except sqlite3.OperationalError:
pass pass
net.killTor()
kill_daemon.onionr_help = "Gracefully stops the " # type: ignore kill_daemon.onionr_help = "Gracefully stops the " # type: ignore
kill_daemon.onionr_help += "Onionr API servers" # type: ignore kill_daemon.onionr_help += "Onionr API servers" # type: ignore