2019-05-09 05:27:15 +00:00
|
|
|
'''
|
2019-06-12 00:05:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
Connect a new peer to our communicator instance. Does so randomly if no peer is specified
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
|
|
|
'''
|
2019-07-10 22:38:20 +00:00
|
|
|
import time, sys, secrets
|
2019-05-09 05:27:15 +00:00
|
|
|
import onionrexceptions, logger, onionrpeers
|
|
|
|
from utils import networkmerger
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import stringvalidators, epoch
|
2019-07-11 07:39:35 +00:00
|
|
|
from communicator import peeraction, bootstrappeers
|
2019-07-18 17:40:48 +00:00
|
|
|
from coredb import keydb
|
2019-05-09 05:27:15 +00:00
|
|
|
def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False):
|
2019-07-18 17:40:48 +00:00
|
|
|
config = comm_inst.config
|
2019-05-09 05:27:15 +00:00
|
|
|
retData = False
|
|
|
|
tried = comm_inst.offlinePeers
|
|
|
|
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:
|
|
|
|
raise onionrexceptions.InvalidAddress('Will not attempt connection test to invalid address')
|
|
|
|
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
|
|
|
|
2019-06-12 06:44:15 +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 = []
|
|
|
|
for x in comm_inst.newPeers:
|
|
|
|
if x not in peerList:
|
|
|
|
peerList.append(x)
|
|
|
|
tryingNew.append(x)
|
|
|
|
for i in tryingNew:
|
|
|
|
comm_inst.newPeers.remove(i)
|
|
|
|
|
|
|
|
if len(peerList) == 0 or useBootstrap:
|
|
|
|
# Avoid duplicating bootstrap addresses in peerList
|
2019-07-11 07:39:35 +00:00
|
|
|
bootstrappeers.add_bootstrap_list_to_peer_list(comm_inst, peerList)
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
for address in peerList:
|
|
|
|
if not config.get('tor.v3onions') and len(address) == 62:
|
|
|
|
continue
|
2019-06-12 06:44:15 +00:00
|
|
|
# Don't connect to our own address
|
2019-07-18 17:40:48 +00:00
|
|
|
if address == comm_inst.hsAddress:
|
2019-05-09 05:27:15 +00:00
|
|
|
continue
|
2019-06-12 06:44:15 +00:00
|
|
|
# Don't connect to invalid address or if its already been tried/connected, or if its cooled down
|
2019-05-09 05:27:15 +00:00
|
|
|
if len(address) == 0 or address in tried or address in comm_inst.onlinePeers or address in comm_inst.cooldownPeer:
|
|
|
|
continue
|
|
|
|
if comm_inst.shutdown:
|
|
|
|
return
|
2019-06-12 06:44:15 +00:00
|
|
|
# Ping a peer,
|
2019-07-10 22:38:20 +00:00
|
|
|
if peeraction.peer_action(comm_inst, address, 'ping') == 'pong!':
|
2019-05-09 05:27:15 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
if address not in mainPeerList:
|
2019-06-12 06:44:15 +00:00
|
|
|
# Add a peer to our list if it isn't already since it successfully connected
|
2019-07-18 17:40:48 +00:00
|
|
|
networkmerger.mergeAdders(address)
|
2019-05-09 05:27:15 +00:00
|
|
|
if address not in comm_inst.onlinePeers:
|
2019-06-19 20:29:27 +00:00
|
|
|
logger.info('Connected to ' + address, terminal=True)
|
2019-05-09 05:27:15 +00:00
|
|
|
comm_inst.onlinePeers.append(address)
|
2019-06-25 23:07:35 +00:00
|
|
|
comm_inst.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
|
|
|
|
for profile in comm_inst.peerProfiles:
|
|
|
|
if profile.address == address:
|
|
|
|
break
|
|
|
|
else:
|
2019-07-18 17:40:48 +00:00
|
|
|
comm_inst.peerProfiles.append(onionrpeers.PeerProfiles(address))
|
2019-05-09 05:27:15 +00:00
|
|
|
break
|
|
|
|
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)
|
|
|
|
logger.debug('Failed to connect to ' + address)
|
|
|
|
return retData
|