From ede3201f23d64cf939d5d23d4d3ab83aa8f0a039 Mon Sep 17 00:00:00 2001 From: notfire Date: Tue, 15 Oct 2024 10:37:23 -0400 Subject: [PATCH] tidy up instance blocks script, also add a way to clear --- .gitignore | 3 ++- instance-blocks.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2eea525..624bbfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +blocked.json \ No newline at end of file diff --git a/instance-blocks.py b/instance-blocks.py index 2e62e91..1865d53 100644 --- a/instance-blocks.py +++ b/instance-blocks.py @@ -8,6 +8,7 @@ parser = argparse.ArgumentParser() sp = parser.add_subparsers(dest="list_or_push") list_sp = sp.add_parser("list") push_sp = sp.add_parser("push") +clear_sp = sp.add_parser("clear") list_sp.add_argument("-o", "--output") push_sp.add_argument("input") @@ -20,6 +21,10 @@ push_grp = push_sp.add_mutually_exclusive_group() push_grp.add_argument("-a", "--allowed", action="store_true") push_grp.add_argument("-b", "--blocked", action="store_true") +clear_grp = clear_sp.add_mutually_exclusive_group() +clear_grp.add_argument("-a", "--allowed", action="store_true") +clear_grp.add_argument("-b", "--blocked", action="store_true") + args = parser.parse_args() if args.list_or_push == "list": @@ -73,8 +78,40 @@ elif args.list_or_push == "push": x_json = json.loads(x_data) headers = {"authorization": auth_token} + ctr = 0 for instance in x_json: + ctr += 1 + print(f"({ctr}/{len(x_json)}) {ab}ing {instance["host"]} for {instance["reason"]}") instance["imported"] = instance.pop("isImported") url = instance_full_url + instance["host"] + "/" + ab instance.pop("host") - req = requests.post(url, headers=headers, data=instance) \ No newline at end of file + req = requests.post(url, headers=headers, data=instance) +elif args.list_or_push == "clear": + instance_base_proto = os.getenv("INSTANCE_BASE_PROTO") + instance_base_url = os.getenv("INSTANCE_BASE_URL") + instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/admin/instances/" + auth_token = os.getenv("AUTH_TOKEN") + + if args.blocked: + instance_full_url += "blocked" + ab = "blocked" + ab2 = "unblock" + elif args.allowed: + instance_full_url += "allowed" + ab = "allowed" + ab2 = "disallow" + else: + print("specify -a or -b") + exit() + + headers = {"authorization": auth_token} + req = requests.get(instance_full_url, headers=headers) + + json_dumped = json.loads(req.text) + + ctr = 0 + for instance in json_dumped: + ctr += 1 + print(f"({ctr}/{len(json_dumped)}) {ab2}ing {instance["host"]}") + instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/admin/instances/" + instance["host"] + "/" + ab2 + req = requests.post(instance_full_url, headers=headers) \ No newline at end of file