2022-02-25 07:02:04 +00:00
|
|
|
from time import sleep
|
2022-02-24 07:03:50 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .. import Peer
|
|
|
|
|
2022-02-25 07:02:04 +00:00
|
|
|
import logger
|
2022-02-24 07:03:50 +00:00
|
|
|
from ..commands import GossipCommands, command_to_byte
|
|
|
|
import onionrplugins
|
|
|
|
|
|
|
|
|
|
|
|
def do_announce(peer_set):
|
|
|
|
"Announce with N peers of each identified transport"
|
|
|
|
def _announce(announce_peer: 'Peer', our_transport_address: str):
|
2022-02-25 07:02:04 +00:00
|
|
|
try:
|
|
|
|
our_transport_address = our_transport_address.encode('utf-8')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2022-02-24 07:03:50 +00:00
|
|
|
sock = announce_peer.get_socket()
|
|
|
|
sock.send(
|
|
|
|
command_to_byte(GossipCommands.ANNOUNCE) + our_transport_address)
|
2022-02-25 07:02:04 +00:00
|
|
|
if int.from_bytes(sock.recv(1), 'big') != 1:
|
|
|
|
logger.warn(
|
|
|
|
f"Could not announce with {announce_peer.transport_address}")
|
|
|
|
sock.close()
|
2022-02-24 07:03:50 +00:00
|
|
|
|
2022-02-25 07:02:04 +00:00
|
|
|
while not len(peer_set):
|
|
|
|
sleep(1)
|
2022-02-24 07:03:50 +00:00
|
|
|
|
|
|
|
per_transport = 3
|
|
|
|
peer_types = {}
|
|
|
|
count_for_peer = 0
|
|
|
|
for peer in peer_set:
|
|
|
|
try:
|
2022-02-25 07:02:04 +00:00
|
|
|
count_for_peer = peer_types[peer.__class__]
|
2022-02-24 07:03:50 +00:00
|
|
|
except KeyError:
|
2022-02-25 07:02:04 +00:00
|
|
|
count_for_peer = peer_types[peer.__class__] = 0
|
2022-02-24 07:03:50 +00:00
|
|
|
|
|
|
|
if count_for_peer == per_transport:
|
|
|
|
continue
|
|
|
|
|
2022-02-25 07:02:04 +00:00
|
|
|
# Plugin for the transport associated with the peer will call _announce
|
|
|
|
# with the peer and *our* transport address
|
2022-02-24 07:03:50 +00:00
|
|
|
onionrplugins.events.event(
|
|
|
|
'get_our_transport',
|
2022-02-25 07:02:04 +00:00
|
|
|
data={'callback': _announce, 'peer': peer},
|
|
|
|
threaded=True)
|
2022-02-24 07:03:50 +00:00
|
|
|
|
2022-02-25 07:02:04 +00:00
|
|
|
peer_types[peer.__class__] += 1
|