-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from checkmate.checker.url.allow_rules import AllowRules | ||
from checkmate.checker.url.custom_rules import CustomRules | ||
from checkmate.checker.url.custom_rules import CustomRules, BlocklistParser | ||
from checkmate.checker.url.url_haus import URLHaus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from checkmate.checker import url | ||
from checkmate.models import Reason, CustomRule | ||
|
||
|
||
class CustomRuleService: | ||
def __init__(self, db): | ||
self._db = db | ||
|
||
def set_block_list(self, text: str) -> list[str]: | ||
rules, errors = self._parse_text(text) | ||
if not errors: | ||
self._set_custom_rules(rules) | ||
return errors | ||
|
||
def get_block_list(self) -> str: | ||
lines = [self._render_line(rule) for rule in self._db.query(CustomRule).all()] | ||
lines.sort() | ||
return "\n".join(lines) | ||
|
||
@staticmethod | ||
def _render_line(rule: CustomRule) -> str: | ||
return "{} {}".format(rule.rule, ",".join(tag.value for tag in rule.reasons)) | ||
|
||
def _set_custom_rules(self, rules: list[CustomRule]) -> None: | ||
self._db.query(CustomRule).delete() | ||
CustomRule.bulk_upsert(self._db, values=rules) | ||
|
||
def _parse_text(self, text: str) -> tuple[list[CustomRule], list[str]]: | ||
rules, errors = [], [] | ||
for line in text.split("\n"): | ||
line = line.strip() | ||
if not line or line.startswith("#"): | ||
continue | ||
|
||
try: | ||
domain, reason = self._parse_line(line) | ||
except ValueError as e: | ||
errors.append(str(e)) | ||
continue | ||
|
||
try: | ||
rules.append(url.CustomRules.value_from_domain(domain, reason)) | ||
except ValueError as e: | ||
errors.append(str(e)) | ||
|
||
return rules, errors | ||
|
||
@staticmethod | ||
def _parse_line(line: str) -> tuple[str, Reason] | None: | ||
match = url.BlocklistParser.LINE_PATTERN.match(line) | ||
if not match: | ||
raise ValueError(f"Cannot parse blocklist line: {line!r}") | ||
|
||
raw_rule, reason = match.group(1), match.group(2) | ||
return raw_rule, Reason.parse(reason) | ||
|
||
|
||
def factory(_context, request): | ||
return CustomRuleService(request.db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{% extends "checkmate:templates/admin/base.html.jinja2" %} | ||
{% block content %} | ||
|
||
<section> | ||
<div class="container"> | ||
{% if errors %} | ||
<article class="message is-danger"> | ||
<div class="message-body"> | ||
{% for error in errors %} | ||
<p>{{ error }}</p> | ||
{% endfor %} | ||
</div> | ||
</article> | ||
{% endif %} | ||
|
||
{% if message %} | ||
<article class="message"> | ||
<div class="message-body"> | ||
<p>{{ message }}</p> | ||
</div> | ||
</article> | ||
{% endif %} | ||
</div> | ||
</section> | ||
|
||
<section> | ||
<div class="container"> | ||
<fieldset class="box mt-6"> | ||
<legend class="label has-text-centered">Update Block List</legend> | ||
<form action="{{ request.route_url("admin.block_list") }}" method="POST"> | ||
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}"> | ||
|
||
<div class="field is-horizontal"> | ||
<div class="field-label is-normal"> | ||
<label class="label">Block List</label> | ||
</div> | ||
<div class="field-body"> | ||
<div class="field"> | ||
<div class="control is-expanded"> | ||
<textarea class="textarea" name="block-list" rows="20">{{ block_list }}</textarea> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="field is-horizontal"> | ||
<div class="field-label is-normal"> | ||
<label class="label"></label> | ||
</div> | ||
<div class="field-body"> | ||
<div class="field"> | ||
<div class="control is-expanded"> | ||
<input type="submit" class="button is-info" value="Update"> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</form> | ||
</fieldset> | ||
</div> | ||
</section> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters