linting refactoring communicator(utils) and reduced TOCTOU issus with online peer picking

This commit is contained in:
Kevin Froman 2019-12-20 01:24:38 -06:00
parent 9fbee668aa
commit e5f3866f9e
8 changed files with 88 additions and 45 deletions

View file

@ -1,9 +1,13 @@
'''
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
clear offline peer in a communicator instance
'''
'''
clear offline peer in a communicator instance
"""
from typing import TYPE_CHECKING
import logger
if TYPE_CHECKING:
from communicator import OnionrCommunicatorDaemon
"""
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
@ -16,14 +20,16 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import logger
def clear_offline_peer(comm_inst):
'''Removes the longest offline peer to retry later'''
"""
def clear_offline_peer(comm_inst: 'OnionrCommunicatorDaemon'):
"""Remove the longest offline peer to retry later."""
try:
removed = comm_inst.offlinePeers.pop(0)
except IndexError:
pass
else:
logger.debug('Removed ' + removed + ' from offline list, will try them again.')
comm_inst.decrementThreadCount('clear_offline_peer')
logger.debug('Removed ' + removed +
' from offline list, will try them again.')
comm_inst.decrementThreadCount('clear_offline_peer')

View file

@ -1,11 +1,14 @@
"""
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
get online peers in a communicator instance
get online peers in a communicator instance
"""
import time
from typing import TYPE_CHECKING
from etc import humanreadabletime
import logger
if TYPE_CHECKING:
from communicator import OnionrCommunicatorDaemon
"""
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
@ -22,25 +25,25 @@ import logger
"""
def get_online_peers(comm_inst):
"""
Manages the comm_inst.onlinePeers attribute list,
]connects to more peers if we have none connected
def get_online_peers(comm_inst: 'OnionrCommunicatorDaemon'):
"""Manage the comm_inst.onlinePeers attribute list.
Connect to more peers if we have none connected
"""
config = comm_inst.config
if config.get('general.offline_mode', False):
comm_inst.decrementThreadCount('get_online_peers')
return
logger.debug('Refreshing peer pool...')
maxPeers = int(config.get('peers.max_connect', 10))
needed = maxPeers - len(comm_inst.onlinePeers)
max_peers = int(config.get('peers.max_connect', 10))
needed = max_peers - len(comm_inst.onlinePeers)
last_seen = 'never'
if not isinstance(comm_inst.lastNodeSeen, type(None)):
last_seen = humanreadabletime.human_readable_time(
comm_inst.lastNodeSeen)
for i in range(needed):
for _ in range(needed):
if len(comm_inst.onlinePeers) == 0:
comm_inst.connectNewPeer(useBootstrap=True)
else:
@ -50,8 +53,7 @@ def get_online_peers(comm_inst):
break
else:
if len(comm_inst.onlinePeers) == 0:
logger.debug
('Couldn\'t connect to any peers.' +
logger.debug('Couldn\'t connect to any peers.' +
f' Last node seen {last_seen} ago.')
else:
comm_inst.lastNodeSeen = time.time()

View file

@ -1,9 +1,12 @@
'''
Onionr - Private P2P Communication
"""
Onionr - Private P2P Communication.
pick online peers in a communicator instance
'''
'''
pick online peers in a communicator instance
"""
import secrets
import onionrexceptions
"""
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
@ -16,17 +19,22 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import secrets
"""
def pick_online_peer(comm_inst):
'''randomly picks peer from pool without bias (using secrets module)'''
"""Randomly picks peer from pool without bias (using secrets module)."""
ret_data = ''
peer_length = len(comm_inst.onlinePeers)
if peer_length <= 0:
raise onionrexceptions.OnlinePeerNeeded
while True:
peer_length = len(comm_inst.onlinePeers)
if peer_length <= 0:
break
try:
# get a random online peer, securely. May get stuck in loop if network is lost or if all peers in pool magically disconnect at once
# Get a random online peer, securely.
# May get stuck in loop if network is lost or if all peers in pool magically disconnect at once
ret_data = comm_inst.onlinePeers[secrets.randbelow(peer_length)]
except IndexError:
pass