2020-07-26 20:49:34 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-05-11 18:32:56 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
Lookup new peer transport addresses using the communicator
|
|
|
|
"""
|
2020-07-26 20:49:34 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2020-07-07 13:37:23 +00:00
|
|
|
import logger
|
|
|
|
from onionrutils import stringvalidators
|
|
|
|
from communicator import peeraction, onlinepeers
|
|
|
|
from utils import gettransports
|
|
|
|
import onionrexceptions
|
2020-07-26 20:49:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from deadsimplekv import DeadSimpleKV
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
2019-05-11 18:32:56 +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/>.
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
def lookup_new_peer_transports_with_communicator(shared_state):
|
2019-07-02 06:32:26 +00:00
|
|
|
logger.info('Looking up new addresses...')
|
2019-05-11 18:32:56 +00:00
|
|
|
tryAmount = 1
|
|
|
|
newPeers = []
|
2019-07-24 17:22:19 +00:00
|
|
|
transports = gettransports.get()
|
2020-07-31 01:15:36 +00:00
|
|
|
kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
|
2019-07-24 17:22:19 +00:00
|
|
|
|
2019-05-11 18:32:56 +00:00
|
|
|
for i in range(tryAmount):
|
|
|
|
# Download new peer address list from random online peers
|
|
|
|
if len(newPeers) > 10000:
|
2019-06-12 06:44:15 +00:00
|
|
|
# Don't get new peers if we have too many queued up
|
2019-05-11 18:32:56 +00:00
|
|
|
break
|
2019-12-20 07:24:38 +00:00
|
|
|
try:
|
2020-08-08 17:38:14 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(kv)
|
|
|
|
newAdders = peeraction.peer_action(shared_state, peer, action='pex')
|
2019-12-20 07:24:38 +00:00
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
|
|
|
continue
|
2019-05-11 18:32:56 +00:00
|
|
|
try:
|
|
|
|
newPeers = newAdders.split(',')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Validate new peers are good format and not already in queue
|
|
|
|
invalid = []
|
|
|
|
for x in newPeers:
|
|
|
|
x = x.strip()
|
2020-07-07 13:37:23 +00:00
|
|
|
if not stringvalidators.validate_transport(x) \
|
2020-07-26 20:49:34 +00:00
|
|
|
or x in kv.get('newPeers') or x in transports:
|
2019-05-11 18:32:56 +00:00
|
|
|
# avoid adding if its our address
|
|
|
|
invalid.append(x)
|
|
|
|
for x in invalid:
|
2019-09-12 06:28:20 +00:00
|
|
|
try:
|
|
|
|
newPeers.remove(x)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-11-15 18:52:52 +00:00
|
|
|
kv.get('newPeers').extend(newPeers)
|