mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Address comments from 01db53dac6.
This commit is contained in:
parent
dc504f0395
commit
9f1e4f60e9
4 changed files with 15 additions and 13 deletions
|
|
@ -188,7 +188,7 @@ class SlowRetrievalError(DownloadError):
|
|||
|
||||
def __init__(self, cumulative_moving_average_of_speed):
|
||||
self.__cumulative_moving_average_of_speed = \
|
||||
cumulative_moving_average_of_speed#bytes/second
|
||||
cumulative_moving_average_of_speed #bytes/second
|
||||
|
||||
def __str__(self):
|
||||
return "Cumulative moving average of download speed: "+\
|
||||
|
|
|
|||
|
|
@ -39,16 +39,16 @@
|
|||
|
||||
# Since the timestamp role does not have signed metadata about itself, we set a
|
||||
# default but sane upper bound for the number of bytes required to download it.
|
||||
DEFAULT_TIMESTAMP_REQUIRED_LENGTH = 2048#bytes
|
||||
DEFAULT_TIMESTAMP_REQUIRED_LENGTH = 2048 #bytes
|
||||
|
||||
# Set a timeout value in seconds (float) for non-blocking socket operations.
|
||||
SOCKET_TIMEOUT = 1#seconds
|
||||
SOCKET_TIMEOUT = 1 #seconds
|
||||
|
||||
# The maximum chunk of data, in bytes, we would download in every round.
|
||||
CHUNK_SIZE = 8192#bytes
|
||||
CHUNK_SIZE = 8192 #bytes
|
||||
|
||||
# The minimum cumulative moving average of download speed (bytes/second) that
|
||||
# must be met to avoid being considered as a slow retrieval attack.
|
||||
MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED = CHUNK_SIZE#bytes/second
|
||||
MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED = CHUNK_SIZE #bytes/second
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -107,13 +107,14 @@ def __start_clock(self):
|
|||
|
||||
# We must have reset the clock before this.
|
||||
assert self.__start_time is None
|
||||
# We are using wall time, so it will be imprecise sometimes.
|
||||
self.__start_time = time.time()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def __stop_clock(self, data_length):
|
||||
def __stop_clock_and_check_speed(self, data_length):
|
||||
"""
|
||||
<Purpose>
|
||||
Stop the clock and try to detect slow retrieval.
|
||||
|
|
@ -134,9 +135,10 @@ def __stop_clock(self, data_length):
|
|||
|
||||
"""
|
||||
|
||||
# We are using wall time, so it will be imprecise sometimes.
|
||||
stop_time = time.time()
|
||||
# We must have already started the clock.
|
||||
assert self.__start_time > 0, self.__start_time
|
||||
assert self.__start_time > 0
|
||||
time_delta = stop_time-self.__start_time
|
||||
# Reset the clock.
|
||||
self.__start_time = None
|
||||
|
|
@ -153,8 +155,9 @@ def __stop_clock(self, data_length):
|
|||
|
||||
# If the cumulative moving average of the download speed is below a certain
|
||||
# threshold, we flag this as a possible slow-retrieval attack. This
|
||||
# threshold will determine our bias: if it is too slow, we will have more
|
||||
# false negatives; if it is too high, we will have more false positives.
|
||||
# threshold will determine our bias: if it is too low, we will have more
|
||||
# false positives; if it is too high, we will have more false negatives.
|
||||
# Presently, we know that this will punish a server with a slow start.
|
||||
if self.__cumulative_moving_average_of_speed < tuf.conf.MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED:
|
||||
raise tuf.SlowRetrievalError(self.__cumulative_moving_average_of_speed)
|
||||
logger.debug('Cumulative moving average of download speed: '+\
|
||||
|
|
@ -229,15 +232,15 @@ def read(self, size):
|
|||
self.__start_clock()
|
||||
data = self._sock.recv(left)
|
||||
except socket.timeout:
|
||||
self.__stop_clock(0)
|
||||
self.__stop_clock_and_check_speed(0)
|
||||
continue
|
||||
except socket.error, e:
|
||||
if e.args[0] == EINTR:
|
||||
self.__stop_clock(0)
|
||||
self.__stop_clock_and_check_speed(0)
|
||||
continue
|
||||
raise
|
||||
else:
|
||||
self.__stop_clock(len(data))
|
||||
self.__stop_clock_and_check_speed(len(data))
|
||||
if not data:
|
||||
break
|
||||
n = len(data)
|
||||
|
|
|
|||
|
|
@ -89,7 +89,6 @@ def get_random_port():
|
|||
def run(port, test_mode):
|
||||
server_address = ('localhost', port)
|
||||
httpd = HTTPServer_Test(server_address, Handler, test_mode)
|
||||
print('Slow server is active on port: '+str(port)+' ...')
|
||||
httpd.handle_request()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue