Onionr/src/lan/client/__init__.py

59 lines
1.9 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-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([])
def _lan_work(peer: LANIP):
identified_port = lambda: None
identified_port.port = 0
def find_port(start=1024, max=0):
for i in range(start, 65535):
if i > max and max > 0:
break
if identified_port.port != 0:
break
2020-06-17 05:37:11 +00:00
try:
2020-06-17 05:42:40 +00:00
if requests.get(f'http://{peer}:{i}/ping', timeout=1) == 'onionr!':
print("Found", peer, i)
2020-06-17 05:37:11 +00:00
identified_port.port = i
break
except requests.exceptions.ConnectionError:
pass
2020-06-17 05:00:52 +00:00
2020-06-17 05:53:33 +00:00
#Thread(target=find_port, args=[1024, 32767], daemon=True).start()
#Thread(target=find_port, args=[32767, 65535], daemon=True).start()
#while identified_port.port == 0:
# better_sleep(1)
i = 1337
print(requests.get(f'http://{peer}:{i}/ping'))
2020-06-17 05:00:52 +00:00
def connect_peer(peer: LANIP):
Thread(target=_lan_work, args=[peer], daemon=True).start()
connected_lan_peers.add(peer)