2020-06-28 09:17:45 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-05-09 05:27:15 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
Connect a new peer to our communicator instance.
|
|
|
|
Does so randomly if no peer is specified
|
2020-06-28 09:17:45 +00:00
|
|
|
"""
|
2020-07-07 13:37:23 +00:00
|
|
|
import time
|
|
|
|
import secrets
|
|
|
|
|
|
|
|
import onionrexceptions
|
|
|
|
import logger
|
|
|
|
import onionrpeers
|
2020-06-28 09:17:45 +00:00
|
|
|
from utils import networkmerger, gettransports
|
|
|
|
from onionrutils import stringvalidators, epoch
|
|
|
|
from communicator import peeraction, bootstrappeers
|
|
|
|
from coredb import keydb
|
2020-07-31 01:15:36 +00:00
|
|
|
import config
|
2020-06-28 09:17:45 +00:00
|
|
|
"""
|
2019-05-09 05:27:15 +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-06-28 09:17:45 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
def connect_new_peer_to_communicator(shared_state, peer='', useBootstrap=False):
|
2019-05-09 05:27:15 +00:00
|
|
|
retData = False
|
2020-07-31 01:15:36 +00:00
|
|
|
kv: "DeadSimpleKV" = shared_state.get_by_string("DeadSimpleKV")
|
2020-07-27 00:12:52 +00:00
|
|
|
tried = kv.get('offlinePeers')
|
2019-07-24 16:32:23 +00:00
|
|
|
transports = gettransports.get()
|
2019-05-09 05:27:15 +00:00
|
|
|
if peer != '':
|
2019-06-23 17:41:07 +00:00
|
|
|
if stringvalidators.validate_transport(peer):
|
2019-05-09 05:27:15 +00:00
|
|
|
peerList = [peer]
|
|
|
|
else:
|
2020-07-07 13:37:23 +00:00
|
|
|
raise onionrexceptions.InvalidAddress(
|
|
|
|
'Will not attempt connection test to invalid address')
|
2019-05-09 05:27:15 +00:00
|
|
|
else:
|
2019-07-18 17:40:48 +00:00
|
|
|
peerList = keydb.listkeys.list_adders()
|
2019-05-09 05:27:15 +00:00
|
|
|
|
2019-07-18 17:40:48 +00:00
|
|
|
mainPeerList = keydb.listkeys.list_adders()
|
|
|
|
peerList = onionrpeers.get_score_sorted_peer_list()
|
2019-05-09 05:27:15 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
|
|
|
If we don't have enough peers connected or random chance,
|
|
|
|
select new peers to try
|
|
|
|
"""
|
2019-05-09 05:27:15 +00:00
|
|
|
if len(peerList) < 8 or secrets.randbelow(4) == 3:
|
|
|
|
tryingNew = []
|
2020-07-26 20:49:34 +00:00
|
|
|
for x in kv.get('newPeers'):
|
2019-05-09 05:27:15 +00:00
|
|
|
if x not in peerList:
|
|
|
|
peerList.append(x)
|
|
|
|
tryingNew.append(x)
|
|
|
|
for i in tryingNew:
|
2020-07-26 20:49:34 +00:00
|
|
|
kv.get('newPeers').remove(i)
|
2020-03-26 08:48:57 +00:00
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
if len(peerList) == 0 or useBootstrap:
|
|
|
|
# Avoid duplicating bootstrap addresses in peerList
|
2019-09-22 20:48:56 +00:00
|
|
|
if config.get('general.use_bootstrap_list', True):
|
2020-07-31 01:15:36 +00:00
|
|
|
bootstrappeers.add_bootstrap_list_to_peer_list(kv, peerList)
|
2019-08-05 19:01:53 +00:00
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
for address in peerList:
|
2019-10-25 06:52:26 +00:00
|
|
|
address = address.strip()
|
|
|
|
|
2019-06-12 06:44:15 +00:00
|
|
|
# Don't connect to our own address
|
2019-07-24 17:22:19 +00:00
|
|
|
if address in transports:
|
2019-05-09 05:27:15 +00:00
|
|
|
continue
|
2020-07-07 13:37:23 +00:00
|
|
|
"""Don't connect to invalid address or
|
|
|
|
if its already been tried/connected, or if its cooled down
|
|
|
|
"""
|
|
|
|
if len(address) == 0 or address in tried \
|
2020-07-26 03:28:32 +00:00
|
|
|
or address in kv.get('onlinePeers') \
|
2020-07-27 00:15:26 +00:00
|
|
|
or address in kv.get('cooldownPeer'):
|
2019-05-09 05:27:15 +00:00
|
|
|
continue
|
2020-07-26 02:36:48 +00:00
|
|
|
if kv.get('shutdown'):
|
2019-05-09 05:27:15 +00:00
|
|
|
return
|
2019-06-12 06:44:15 +00:00
|
|
|
# Ping a peer,
|
2020-07-31 01:15:36 +00:00
|
|
|
ret = peeraction.peer_action(shared_state, address, 'ping')
|
2019-07-22 05:24:42 +00:00
|
|
|
if ret == 'pong!':
|
2019-05-09 05:27:15 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
if address not in mainPeerList:
|
2020-07-07 13:37:23 +00:00
|
|
|
# Add a peer to our list if it isn't already since it connected
|
2019-07-18 17:40:48 +00:00
|
|
|
networkmerger.mergeAdders(address)
|
2020-07-26 03:28:32 +00:00
|
|
|
if address not in kv.get('onlinePeers'):
|
2019-06-19 20:29:27 +00:00
|
|
|
logger.info('Connected to ' + address, terminal=True)
|
2020-07-26 03:28:32 +00:00
|
|
|
kv.get('onlinePeers').append(address)
|
2020-07-29 08:57:06 +00:00
|
|
|
kv.get('connectTimes')[address] = epoch.get_epoch()
|
2019-05-09 05:27:15 +00:00
|
|
|
retData = address
|
|
|
|
|
|
|
|
# add peer to profile list if they're not in it
|
2020-07-29 08:57:06 +00:00
|
|
|
for profile in kv.get('peerProfiles'):
|
2019-05-09 05:27:15 +00:00
|
|
|
if profile.address == address:
|
|
|
|
break
|
|
|
|
else:
|
2020-07-29 08:57:06 +00:00
|
|
|
kv.get('peerProfiles').append(
|
2020-07-07 13:37:23 +00:00
|
|
|
onionrpeers.PeerProfiles(address))
|
2020-10-10 04:26:51 +00:00
|
|
|
try:
|
|
|
|
del kv.get('plaintextDisabledPeers')[address]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if peeraction.peer_action(
|
|
|
|
shared_state, address, 'plaintext') == 'false':
|
|
|
|
kv.get('plaintextDisabledPeers')[address] = True
|
2019-05-09 05:27:15 +00:00
|
|
|
break
|
2020-10-10 04:26:51 +00:00
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
else:
|
2019-06-12 06:44:15 +00:00
|
|
|
# Mark a peer as tried if they failed to respond to ping
|
2019-05-09 05:27:15 +00:00
|
|
|
tried.append(address)
|
2019-07-22 05:24:42 +00:00
|
|
|
logger.debug('Failed to connect to %s: %s ' % (address, ret))
|
2019-10-25 06:52:26 +00:00
|
|
|
return retData
|