Reduce the number of times a ResourceAccessor is created, and use all handlers.

This commit is contained in:
Mike Auty
2017-11-24 22:27:36 +00:00
parent 30059766b6
commit 917829d87f
2 changed files with 12 additions and 2 deletions
+10 -1
View File
@@ -10,6 +10,8 @@ import urllib.request
import zipfile
from urllib import request
from volatility import framework
try:
import magic
@@ -28,12 +30,19 @@ class ResourceAccessor(object):
"""Object for openning URLs as files (downloading locally first if necessary)"""
def __init__(self, progress_callback = None, context = None):
"""Creates a resource accessor
Note: context is an SSL context, not a volatility context
"""
self._progress_callback = progress_callback
self._context = context
self._handlers = list(framework.class_subclasses(request.BaseHandler))
vollog.log(constants.LOGLEVEL_VVV,
"Available URL handlers: {}".format(", ".join([x.__name__ for x in self._handlers])))
def open(self, url, mode = "rb"):
"""Returns a file-like object for a particular URL opened in mode"""
urllib.request.install_opener(urllib.request.build_opener(JarHandler))
urllib.request.install_opener(urllib.request.build_opener(*self._handlers))
with contextlib.closing(urllib.request.urlopen(url, context = self._context)) as fp:
# Cache the file locally
+2 -1
View File
@@ -59,6 +59,7 @@ class FileLayer(interfaces.layers.DataLayerInterface):
super().__init__(context, config_path, name)
self._location = self.config["location"]
self._accessor = layers.ResourceAccessor()
self._file_ = None
self._size = None
# Instantiate the file to throw exceptions if the file doesn't open
@@ -75,7 +76,7 @@ class FileLayer(interfaces.layers.DataLayerInterface):
# FIXME: Add "+" to the mode once we've determined whether write mode is enabled
mode = "rb"
if not self._file_:
self._file_ = layers.ResourceAccessor().open(self._location, mode)
self._file_ = self._accessor.open(self._location, mode)
return self._file_
@property