diff --git a/.gitignore b/.gitignore index f8b73e7..b3522a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/README.md b/README.md index d7b2373..820feb4 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,16 @@ A small tool to deploy blocklist updates to a mastodon server using its API. ## Concept -The idea is to maintain a blocklist in a simple structured file in this repository. All changes need to be deployed to the mastodon server, this is supposed to be automated with Drone CI. +The idea is to maintain a blocklist in a simple structured file in this repository. All changes need to be deployed to +the mastodon server, this is supposed to be automated with Drone CI. -In order to compare the list entries, we can read the whole blocklist using [the get endpoint](https://docs.joinmastodon.org/methods/admin/domain_blocks/#get). At the same time we read the whole file in the repository, make a comparision and [remove](https://docs.joinmastodon.org/methods/admin/domain_blocks/#delete) unblocked domains from the blocklist and [add](https://docs.joinmastodon.org/methods/admin/domain_blocks/#create) newly added. +In order to compare the list entries, we can read the whole blocklist +using [the get endpoint](https://docs.joinmastodon.org/methods/admin/domain_blocks/#get). At the same time we read the +whole file in the repository, make a comparision +and [remove](https://docs.joinmastodon.org/methods/admin/domain_blocks/#delete) unblocked domains from the blocklist +and [add](https://docs.joinmastodon.org/methods/admin/domain_blocks/#create) newly added. -Since we have several attributes for a domain blog, a simple `.txt` file might not be sufficient. We probably want to set the severity, reject_media, reject_reports and comments. This means we need a human readable, easily python-readable and structured file format. Since Python 3.11 got native support for [toml](https://toml.io/) and it supports [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables), I'd prefer to use this. +Since we have several attributes for a domain blog, a simple `.txt` file might not be sufficient. We probably want to +set the severity, reject_media, reject_reports and comments. This means we need a human readable, easily python-readable +and structured file format. Since Python 3.11 got native support for [toml](https://toml.io/) and it +supports [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables), I'd prefer to use this. diff --git a/blocklist.toml b/blocklist.toml new file mode 100644 index 0000000..32beb6f --- /dev/null +++ b/blocklist.toml @@ -0,0 +1,22 @@ +[[instances]] +name = "beta_birdsite_live" +domain = "beta.birdsite.live" +severity = "silence" +reject_media = true +reject_reports = false +public_comment = "Crossposter" +private_comment = "Crossposter" + +[[instances]] +name = "truth_social" +domain = "truth.social" +severity = "limit" +public_comment = "Right-Wing extremists" +private_comment = "Trump shit" + +[[instances]] +name = "aethy_com" +domain = "aethy.com" +severity = "suspend" +public_comment = "Lollicon" +private_comment = "Übernommen von chaos.social" \ No newline at end of file diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..952e310 --- /dev/null +++ b/cli.py @@ -0,0 +1,99 @@ +# import tomllib +import argparse +import json +import logging +import requests + +import toml + +from models import Instance + + +def load_local_blocklist(filename: str) -> [Instance]: + with open(filename, "r") as f: + data = toml.load(f) + instances = [] + for instance_dict in data["instances"]: + instance = Instance(instance_dict) + instances.append(instance) + for instance in instances: + print(instance) + return instances + + +def export_blocklist_toml(blocklist: [Instance], filname: str): + toml_str = "" + for instance in blocklist: + toml_str += f''' +[[instances]] +name = "{instance.domain}" +domain = "{instance.domain}" +severity = "{instance.severity}" +reject_media = {str(instance.reject_media).lower()} +reject_reports = {str(instance.reject_reports).lower()} +public_comment = "{instance.public_comment}" +private_comment = "{instance.private_comment}" + ''' + with open(filname, "w") as f: + f.write(toml_str) + + +def blocklist_json_to_instances(blocklist_json: str): + instances = [] + for i in blocklist_json: + instances.append(Instance(i)) + return instances + + +def load_remote_blocklist(server: str, token: str): + headers = { + f'Authorization': f'Bearer {token}', + } + + response = requests.get(f'https://{server}/api/v1/admin/domain_blocks', headers=headers) + if response.status_code == 200: + blocklist_json = json.loads(response.content) + return blocklist_json_to_instances(blocklist_json) + else: + raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})") + + +def cli(): + parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server') + parser.add_argument('action', choices=['diff', 'deploy', 'export'], + help="Either use 'diff' to check the difference between current blocks and future blocks or 'deploy'.") + parser.add_argument('-s', '--server', help="The address of the server where you want to deploy (e.g. " + "mastodon.social)") + parser.add_argument('-t', '--token', help="Authorization token") + parser.add_argument('-i', '--input-file', help="The blocklist to use") + parser.add_argument('-r', '--remote-blocklist', help="The remote blocklist as json for debugging reasons") + parser.add_argument('-o', '--output', help="Filename where to export the blocklist") + args = parser.parse_args() + logging.basicConfig(level=logging.WARN) + + if args.remote_blocklist: + with open(args.remote_blocklist) as f: + remote_blocklist = blocklist_json_to_instances(json.load(f)) + else: + remote_blocklist = load_remote_blocklist(server=args.server, token=args.token) + + """Load local blocklist only when needed""" + if args.action in ["diff", "deploy"]: + if args.input_file: + blocklist_filename = args.input_file + else: + blocklist_filename = "blocklist.toml" + local_blocklist = load_local_blocklist(blocklist_filename) + + if args.action == "diff": + Instance.show_diffs(local_blocklist, remote_blocklist) + elif args.action == "deploy": + diffs = Instance.list_diffs(local_blocklist, remote_blocklist) + Instance.apply_blocks_from_diff(diffs, args.server, args.token) + elif args.action == "export": + export_blocklist_toml(remote_blocklist, args.output) + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + cli() diff --git a/models.py b/models.py new file mode 100644 index 0000000..3c8ad4d --- /dev/null +++ b/models.py @@ -0,0 +1,135 @@ +import requests + + +class Instance: + def __init__(self, instance_dict): + """If obfuscate, reject_media or reject_reports are not specified default to False""" + self.obfuscate = False + self.reject_media = False + self.reject_reports = False + self.id = None + + """Remote blocks and local blocks are parsed differently""" + try: + instance_dict["id"] + self.parse_remote_block(instance_dict) + except KeyError: + self.parse_local_block(instance_dict) + + def __str__(self): + return f"{self.domain}: {self.severity}" + + def __eq__(self, other): + return self.domain == other.domain and self.severity == other.severity and self.reject_media == other.reject_media and self.reject_reports == other.reject_reports and self.obfuscate == other.obfuscate + + def status_str(self): + return f"{self.severity}, Reject reports: {self.reject_reports}, Reject media: {self.reject_media}, Obfuscate: {self.obfuscate}" + + def parse_remote_block(self, instance_dict): + self.domain = instance_dict["domain"] + self.id = instance_dict["id"] + self.severity = instance_dict["severity"] + self.public_comment = instance_dict["public_comment"] + self.private_comment = instance_dict["private_comment"] + self.obfuscate = instance_dict["obfuscate"] + self.reject_media = instance_dict["reject_media"] + self.reject_reports = instance_dict["reject_reports"] + + def parse_local_block(self, instance_dict): + self.name = instance_dict["name"] + self.domain = instance_dict["domain"] + self.severity = instance_dict["severity"] + self.public_comment = instance_dict["public_comment"] + self.private_comment = instance_dict["private_comment"] + try: + self.obfuscate = instance_dict["obfuscate"] + except KeyError: + pass + try: + self.reject_media = instance_dict["reject_media"] + except KeyError: + pass + try: + self.reject_reports = instance_dict["reject_reports"] + except KeyError: + pass + + def apply(self, server, token, block_id=None): + headers = { + f'Authorization': f'Bearer {token}', + } + data = {"domain": self.domain, + "severity": self.severity, + "reject_media": self.reject_media, + "reject_reports": self.reject_reports, + "private_comment": self.private_comment, + "public_comment": self.public_comment, + "obfuscate": self.obfuscate} + """If no id is given add a new block, else update the existing block""" + if block_id is None: + response = requests.post(f'https://{server}/api/v1/admin/domain_blocks', data=data, headers=headers) + else: + response = requests.put(f'https://{server}/api/v1/admin/domain_blocks/{block_id}', data=data, headers=headers) + if response.status_code != 200: + raise ConnectionError(f"Could not apply block ({response.status_code}: {response.reason})") + def delete(self, server: str, token: str): + headers = { + f'Authorization': f'Bearer {token}', + } + response = requests.delete(f'https://{server}/api/v1/admin/domain_blocks/{self.id}', headers=headers) + if response.status_code != 200: + raise ConnectionError(f"Could not apply block ({response.status_code}: {response.reason})") + + + @staticmethod + def list_diffs(local_blocklist, remote_blocklist): + diffs = [] + for local_instance in local_blocklist: + instance_found = False + for idx, remote_instance in enumerate(remote_blocklist): + if local_instance.domain == remote_instance.domain: + instance_found = True + if local_instance == remote_instance: + pass + else: + """If the local block is different from the remote block, add it to the diff""" + diffs.append({"local": local_instance, "remote": remote_instance}) + """Remove the remote instance from the list so we later have a list of remote instances we don't + have locally""" + del remote_blocklist[idx] + """If the local instance is not in the remote blocklist, add it to the diff""" + if not instance_found: + diffs.append({"local": local_instance, "remote": None}) + for remote_instance in remote_blocklist: + diffs.append({"local": None, "remote": remote_instance}) + return diffs + + @staticmethod + def apply_blocks_from_diff(diffs, server, token): + for diff in diffs: + if diff["local"] is None: + pass + elif diff["remote"] is None: + diff["local"].apply(server, token) + else: + diff["local"].apply(server, token, block_id=diff["remote"].id) + + @staticmethod + def show_diffs(local_blocklist, remote_blocklist): + from rich.table import Table + from rich.console import Console + table = Table(title="Differences", expand=True, show_lines=True) + + table.add_column("Domain", style="cyan") + table.add_column("Current remote status", style="magenta") + table.add_column("Local status", style="green") + diffs = Instance.list_diffs(local_blocklist, remote_blocklist) + for diff in diffs: + if diff["local"] is None: + table.add_row(diff["remote"].domain, None, diff["remote"].status_str()) + elif diff["remote"] is None: + table.add_row(diff["local"].domain, diff["local"].status_str(), None) + else: + table.add_row(diff["local"].domain, diff["local"].status_str(), diff["remote"].status_str()) + console = Console() + console.print(table) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5ad0955 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,18 @@ +[tool.poetry] +name = "mastodon-blocklist-deploy" +version = "0.1.0" +description = "A small tool to deploy blocklist updates to a mastodon server using its API." +authors = ["Georg Krause ", "Julian-Samuel Gebühr "] +readme = "README.md" +packages = [{include = "mastodon_blocklist_deploy"}] + +[tool.poetry.dependencies] +python = "^3.10" +requests = "^2.28.1" +rich = "^13.0.1" +toml = "^0.10.2" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api"