chg: Improve recaptures (WiP)

pull/923/head
Raphaël Vinot 2024-06-25 12:41:18 +02:00
parent c794e17122
commit 7324c8b085
4 changed files with 25 additions and 16 deletions

View File

@ -28,7 +28,7 @@ from pyipasnhistory import IPASNHistory # type: ignore[attr-defined]
from redis import Redis
from .context import Context
from .helpers import get_captures_dir, is_locked
from .helpers import get_captures_dir, is_locked, load_capture_settings
from .indexing import Indexing
from .default import LookylooException, try_make_file, get_config
from .exceptions import MissingCaptureDirectory, NoValidHarFile, MissingUUID, TreeNeedsRebuild
@ -413,12 +413,7 @@ class CapturesIndex(Mapping): # type: ignore[type-arg]
raise MissingCaptureDirectory(f'Unable to find the UUID file in {capture_dir}.')
# Get capture settings as they were submitted
capture_settings_file = capture_dir / 'capture_settings.json'
if capture_settings_file.exists():
with capture_settings_file.open() as f:
capture_settings = json.load(f)
else:
capture_settings = {}
capture_settings = load_capture_settings(capture_dir)
logger = LookylooCacheLogAdapter(self.logger, {'uuid': uuid})
try:
@ -435,7 +430,7 @@ class CapturesIndex(Mapping): # type: ignore[type-arg]
tree = None
cache: dict[str, str | int] = {'uuid': uuid, 'capture_dir': capture_dir_str}
if capture_settings.get('url'):
if capture_settings.get('url') and capture_settings['url'] is not None:
cache['url'] = capture_settings['url']
if (capture_dir / 'error.txt').exists():

View File

@ -82,6 +82,15 @@ def get_email_template() -> str:
return f.read()
@lru_cache(256)
def load_capture_settings(capture_dir: Path) -> CaptureSettings:
capture_settings_file = capture_dir / 'capture_settings.json'
if capture_settings_file.exists():
with capture_settings_file.open() as f:
return json.load(f)
return {}
@lru_cache
def load_takedown_filters() -> tuple[re.Pattern[str], re.Pattern[str], dict[str, list[str]]]:
filter_ini_file = get_homedir() / 'config' / 'takedown_filters.ini'

View File

@ -41,7 +41,9 @@ from werkzeug.wrappers.response import Response as WerkzeugResponse
from lookyloo import Lookyloo, CaptureSettings
from lookyloo.default import get_config
from lookyloo.exceptions import MissingUUID, NoValidHarFile, LacusUnreachable
from lookyloo.helpers import get_taxonomies, UserAgents, load_cookies, UserCaptureSettings, load_user_config
from lookyloo.helpers import (get_taxonomies, UserAgents, load_cookies,
UserCaptureSettings, load_user_config,
load_capture_settings)
if sys.version_info < (3, 9):
from pytz import all_timezones_set
@ -1424,14 +1426,14 @@ def search() -> str | Response | WerkzeugResponse:
return render_template('search.html')
def _prepare_capture_template(user_ua: str | None, predefined_url: str | None=None, *,
def _prepare_capture_template(user_ua: str | None, predefined_settings: CaptureSettings | None=None, *,
user_config: UserCaptureSettings | None=None) -> str:
return render_template('capture.html', user_agents=user_agents.user_agents,
default=user_agents.default,
personal_ua=user_ua,
default_public=get_config('generic', 'default_public'),
devices=lookyloo.get_playwright_devices(),
predefined_url_to_capture=predefined_url if predefined_url else '',
predefined_settings=predefined_settings if predefined_settings else {},
user_config=user_config,
show_project_page=get_config('generic', 'show_project_page'),
version=pkg_version,
@ -1441,9 +1443,10 @@ def _prepare_capture_template(user_ua: str | None, predefined_url: str | None=No
@app.route('/recapture/<string:tree_uuid>', methods=['GET'])
def recapture(tree_uuid: str) -> str | Response | WerkzeugResponse:
cache = lookyloo.capture_cache(tree_uuid)
if cache and hasattr(cache, 'url'):
if cache and hasattr(cache, 'capture_dir'):
capture_settings = load_capture_settings(cache.capture_dir)
return _prepare_capture_template(user_ua=request.headers.get('User-Agent'),
predefined_url=cache.url)
predefined_settings=capture_settings)
flash(f'Unable to find the capture {tree_uuid} in the cache.', 'error')
return _prepare_capture_template(user_ua=request.headers.get('User-Agent'))

View File

@ -82,7 +82,8 @@
<div class="row mb-3">
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="listing" name="listing" {% if default_public %}checked="checked"{% endif %}>
<input class="form-check-input" type="checkbox" id="listing" name="listing"
{% if default_public or predefined_settings.get('listing') is true %}checked="checked"{% endif %}>
<label for="listing" class="form-check-label">Display results on public page</label>
</div>
</div>
@ -102,7 +103,7 @@
<div class="row input-group mb-3">
<label for="url" class="col-sm-1 col-form-label">URL:</label>
<input type="text" class="form-control col-auto" name="url" id=singleCaptureField
placeholder="URL to capture" value="{{predefined_url_to_capture}}" required>
placeholder="URL to capture" value="{{predefined_settings.get('url', '')}}" required>
<textarea class="form-control col-auto d-none" placeholder="URLs to capture, one per line"
name="urls" id=multipleCapturesField></textarea>
@ -280,7 +281,8 @@
<label for="allow_tracking" class="col-sm-2 col-form-check-label">Allow tracking:</label>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="allow_tracking" name="allow_tracking" aria-describedby="allow_tracking_help">
<input class="form-check-input" type="checkbox" id="allow_tracking" name="allow_tracking" aria-describedby="allow_tracking_help"
{% if predefined_settings.get('allow_tracking') is true %}checked="checked"{% endif %}>
<div id="allow_tracking_help" class="form-text">We'll attempt to click on the button allowing the website captured to violate your privacy.</div>
</div>
</div>