Onionr/src/lan/client/__init__.py

68 lines
2.2 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
LAN transport client thread
"""
2020-06-17 05:00:52 +00:00
import requests
2020-03-23 05:37:49 +00:00
2020-06-17 05:00:52 +00:00
from typing import Set
2020-04-06 13:51:20 +00:00
2020-06-17 05:00:52 +00:00
from onionrtypes import LANIP
2020-03-23 05:37:49 +00:00
from utils.bettersleep import better_sleep
2020-06-19 05:55:13 +00:00
import logger
from coredb.blockmetadb import get_block_list
from onionrblocks.blockimporter import import_block_from_data
from ..server import ports
2020-06-17 05:00:52 +00:00
2020-04-06 13:51:20 +00:00
from threading import Thread
"""
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/>.
"""
2020-06-17 05:00:52 +00:00
connected_lan_peers: Set[LANIP] = set([])
2020-04-03 06:44:26 +00:00
2020-06-17 05:00:52 +00:00
def _lan_work(peer: LANIP):
2020-06-19 05:55:13 +00:00
def _sync_peer(url):
our_blocks = get_block_list()
blocks = requests.get(url + 'blist/0').text.splitlines()
for block in blocks:
if block not in our_blocks:
import_block_from_data(requests.get(url + f'get/{block}', stream=True).raw.read(6000000))
2020-04-03 06:44:26 +00:00
2020-06-19 05:55:13 +00:00
for port in ports:
try:
2020-06-19 05:55:13 +00:00
root = f'http://{peer}:{port}/'
if requests.get(f'{root}ping').text != 'onionr!':
connected_lan_peers.remove(peer)
else:
logger.info(f'[LAN] Connected to {peer}:{port}', terminal=True)
while True:
try:
_sync_peer(root)
except requests.exceptions.ConnectionError:
break
2020-06-17 05:00:52 +00:00
break
2020-06-19 05:55:13 +00:00
except requests.exceptions.ConnectionError:
pass
else:
connected_lan_peers.remove(peer)
2020-06-17 05:00:52 +00:00
def connect_peer(peer: LANIP):
2020-06-19 05:55:13 +00:00
if peer not in connected_lan_peers:
connected_lan_peers.add(peer)
Thread(target=_lan_work, args=[peer], daemon=True).start()