2023-01-09 07:42:36 +00:00
|
|
|
# import tomllib
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import requests
|
2023-01-12 15:22:03 +00:00
|
|
|
import os
|
2023-01-09 12:10:40 +00:00
|
|
|
import toml
|
2023-01-09 07:42:36 +00:00
|
|
|
|
2023-12-19 11:42:33 +00:00
|
|
|
from fediverse_blocklist_tool.models import Instance
|
|
|
|
from fediverse_blocklist_tool.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv, \
|
2023-07-13 07:50:42 +00:00
|
|
|
blocklist_to_json
|
2023-01-09 07:42:36 +00:00
|
|
|
|
|
|
|
|
2023-01-12 15:01:55 +00:00
|
|
|
def load_blocklist_file(filename: str) -> [Instance]:
|
2023-12-19 09:19:24 +00:00
|
|
|
if filename.endswith("json"):
|
|
|
|
with open(filename) as f:
|
|
|
|
instances = blocklist_json_to_instances(json.load(f))
|
|
|
|
else:
|
|
|
|
with open(filename, "r") as f:
|
|
|
|
data = toml.load(f)
|
|
|
|
instances = []
|
|
|
|
for instance_dict in data["instances"]:
|
|
|
|
instance = Instance(instance_dict)
|
|
|
|
instances.append(instance)
|
2023-01-09 07:42:36 +00:00
|
|
|
return instances
|
|
|
|
|
|
|
|
|
2023-01-12 15:04:56 +00:00
|
|
|
def blocklist_json_to_instances(blocklist_json: str) -> [Instance]:
|
2023-01-09 07:42:36 +00:00
|
|
|
instances = []
|
|
|
|
for i in blocklist_json:
|
|
|
|
instances.append(Instance(i))
|
|
|
|
return instances
|
|
|
|
|
|
|
|
|
2023-01-12 15:06:01 +00:00
|
|
|
def load_blocklist_from_instance(server: str, token: str) -> [Instance]:
|
2023-01-09 07:42:36 +00:00
|
|
|
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})")
|
|
|
|
|
|
|
|
|
2023-05-03 09:30:28 +00:00
|
|
|
def remove_key_from_dict(dict, key):
|
|
|
|
del dict[key]
|
|
|
|
return dict
|
|
|
|
|
|
|
|
|
|
|
|
def exporter(blocklist, output=None, format: str = "toml", private: bool = False):
|
|
|
|
if format == "toml":
|
|
|
|
exported_text = blocklist_to_toml(blocklist, private)
|
2023-05-03 13:09:12 +00:00
|
|
|
if format == "csv":
|
|
|
|
exported_text = blocklist_to_csv(blocklist, private)
|
|
|
|
if format == "markdown":
|
|
|
|
exported_text = blocklist_to_markdown(blocklist, private)
|
2023-05-03 13:40:27 +00:00
|
|
|
if format == "json":
|
|
|
|
exported_text = blocklist_to_json(blocklist, private)
|
2023-05-03 09:30:28 +00:00
|
|
|
|
|
|
|
# Output the text
|
|
|
|
if output is not None:
|
|
|
|
with open(output, "w") as f:
|
|
|
|
f.write(exported_text)
|
|
|
|
else:
|
|
|
|
print(exported_text)
|
|
|
|
|
|
|
|
|
2023-07-13 07:50:42 +00:00
|
|
|
def merge(input_file, merge_target, format: str = "toml", private: bool = False, overwrite=False):
|
2023-07-13 09:33:55 +00:00
|
|
|
"""Shows a table in the CLI comparing the local and remote blocklist"""
|
|
|
|
from rich.table import Table
|
|
|
|
from rich.console import Console
|
2023-07-13 07:50:42 +00:00
|
|
|
input_blocklist = load_blocklist_file(input_file)
|
|
|
|
merge_target_blocklist = load_blocklist_file(merge_target)
|
|
|
|
for input_instance in input_blocklist:
|
|
|
|
# If the block is already there with the same parameters we do nothing
|
|
|
|
if input_instance in merge_target_blocklist:
|
|
|
|
continue
|
|
|
|
# Check if there is a domain in the merge target where the input domain is similar
|
|
|
|
try:
|
2023-07-13 09:34:42 +00:00
|
|
|
merge_target_instance = [merge_target_instance for merge_target_instance in merge_target_blocklist if input_instance.domain == merge_target_instance.domain][0]
|
2023-07-13 07:50:42 +00:00
|
|
|
if not overwrite:
|
|
|
|
key_input = ""
|
|
|
|
while key_input not in ("i", "O"):
|
|
|
|
print(f"Different settings for {input_instance.domain} detected.")
|
2023-07-13 09:33:55 +00:00
|
|
|
Instance.show_diff(input_instance, merge_target_instance)
|
2023-07-13 07:50:42 +00:00
|
|
|
key_input = input("Keep input (i) or original (o) [i/O]")
|
|
|
|
elif key_input == "i":
|
|
|
|
merge_target_blocklist.append(merge_target_instance)
|
|
|
|
else:
|
|
|
|
merge_target_blocklist.append(input_instance)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2023-05-03 09:30:28 +00:00
|
|
|
|
2023-01-09 07:42:36 +00:00
|
|
|
def cli():
|
2023-12-19 11:42:33 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Compare, merge, export and deploy blocklist of a fediverse server')
|
2023-07-13 07:50:42 +00:00
|
|
|
parser.add_argument('action', choices=['diff', 'deploy', 'export', 'merge'],
|
2023-01-12 07:40:25 +00:00
|
|
|
help="Either use 'diff' to check the difference between local blockĺist and the blocklist on "
|
|
|
|
"the server, 'deploy' to apply the current local blocklist or 'export' to export the remote "
|
2023-07-13 07:50:42 +00:00
|
|
|
"blocklist into a local file. merge can be used to merge a blocklist (given by -i) into "
|
|
|
|
"another (-o)")
|
2023-01-09 07:42:36 +00:00
|
|
|
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")
|
2023-01-09 12:10:40 +00:00
|
|
|
parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
|
2023-01-11 19:21:01 +00:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true')
|
|
|
|
parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
|
2023-07-10 08:52:33 +00:00
|
|
|
parser.add_argument('--format', default="toml", type=str, help="Export format: toml|markdown|csv|json")
|
2023-05-03 12:45:53 +00:00
|
|
|
parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be "
|
2023-05-03 09:30:28 +00:00
|
|
|
"exported.")
|
2023-01-09 07:42:36 +00:00
|
|
|
args = parser.parse_args()
|
2023-01-11 18:29:38 +00:00
|
|
|
if args.verbose:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logging.basicConfig(level=logging.WARN)
|
2023-01-09 12:10:40 +00:00
|
|
|
|
2023-01-12 15:22:03 +00:00
|
|
|
if args.token:
|
|
|
|
token = args.token
|
|
|
|
else:
|
|
|
|
token = os.getenv('MBD_TOKEN')
|
|
|
|
|
2023-07-13 09:35:32 +00:00
|
|
|
"""Get a remote blocklist only when necessary"""
|
2023-12-19 09:19:24 +00:00
|
|
|
if args.action in ["diff", "deploy", "export"]:
|
2023-07-13 09:35:32 +00:00
|
|
|
"""if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)"""
|
|
|
|
if args.remote_blocklist:
|
2023-12-19 09:19:24 +00:00
|
|
|
remote_blocklist = load_blocklist_file(args.remote_blocklist)
|
2023-07-13 09:35:32 +00:00
|
|
|
else:
|
|
|
|
remote_blocklist = load_blocklist_from_instance(server=args.server, token=token)
|
2023-01-09 12:10:40 +00:00
|
|
|
|
|
|
|
"""Load local blocklist only when needed"""
|
2023-07-13 07:50:42 +00:00
|
|
|
if args.action in ["diff", "deploy", "merge"]:
|
2023-01-09 12:10:40 +00:00
|
|
|
if args.input_file:
|
|
|
|
blocklist_filename = args.input_file
|
|
|
|
else:
|
2023-01-12 07:00:36 +00:00
|
|
|
blocklist_filename = "../blocklist.toml"
|
2023-01-13 07:12:29 +00:00
|
|
|
try:
|
|
|
|
local_blocklist = load_blocklist_file(blocklist_filename)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Local blocklist file was not found. Make sure to specify it's location via -i")
|
|
|
|
exit()
|
2023-01-09 12:10:40 +00:00
|
|
|
|
2023-01-09 10:48:44 +00:00
|
|
|
if args.action == "diff":
|
|
|
|
Instance.show_diffs(local_blocklist, remote_blocklist)
|
|
|
|
elif args.action == "deploy":
|
|
|
|
diffs = Instance.list_diffs(local_blocklist, remote_blocklist)
|
2023-01-12 15:22:03 +00:00
|
|
|
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
|
2023-01-09 12:10:40 +00:00
|
|
|
elif args.action == "export":
|
2023-05-03 09:30:28 +00:00
|
|
|
exporter(remote_blocklist, args.output, args.format, args.private)
|
2023-07-13 07:50:42 +00:00
|
|
|
elif args.action == "merge":
|
|
|
|
merge(args.input_file, args.output, args.format, args.private)
|
|
|
|
|
2023-01-09 11:01:39 +00:00
|
|
|
|
2023-01-09 07:42:36 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|