2023-09-10 16:43:08 +00:00
|
|
|
import os
|
2023-09-14 15:28:45 +00:00
|
|
|
import json
|
|
|
|
import sys
|
2023-09-10 16:43:08 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-06-21 00:07:49 +00:00
|
|
|
from pythorhead import Lemmy
|
2023-08-09 14:20:10 +00:00
|
|
|
from pythonseer import Fediseer
|
|
|
|
from pythonseer.types import FormatType
|
2023-06-21 00:07:49 +00:00
|
|
|
|
2023-09-10 16:43:08 +00:00
|
|
|
load_dotenv()
|
2023-06-21 00:07:49 +00:00
|
|
|
# Your own instance's domain
|
|
|
|
LEMMY_DOMAIN = "lemmy.dbzer0.com"
|
|
|
|
USERNAME = "username"
|
2023-09-10 16:43:08 +00:00
|
|
|
# You can write your password here, or add it to the LEMMY_PASSWORD env var, or add LEMMY_PASSWORD to a .env file
|
2023-06-21 00:07:49 +00:00
|
|
|
PASSWORD = "password"
|
|
|
|
# If there's this many registered users per local post+comments, this site will be considered suspicious
|
|
|
|
ACTIVITY_SUSPICION = 20
|
2023-06-25 22:53:55 +00:00
|
|
|
# If there's this many registered users per active monthly user, this site will be considered suspicious
|
|
|
|
MONTHLY_ACTIVITY_SUSPICION = 500
|
2023-06-21 00:07:49 +00:00
|
|
|
# Extra domains you can block. You can just delete the contents if you want to only block suspicious domains
|
2023-10-11 10:00:15 +00:00
|
|
|
blocklist = [
|
2023-06-21 00:07:49 +00:00
|
|
|
"truthsocial.com",
|
2023-08-09 14:20:10 +00:00
|
|
|
"threads.net",
|
2023-09-14 14:49:08 +00:00
|
|
|
]
|
2023-09-10 16:43:08 +00:00
|
|
|
# Add instances in here which want to ensure are not added in your blocklist
|
2023-09-14 14:49:08 +00:00
|
|
|
#
|
2023-10-11 10:00:15 +00:00
|
|
|
safelist = [
|
2023-09-14 14:49:08 +00:00
|
|
|
]
|
2023-09-08 12:55:51 +00:00
|
|
|
# If you (don't) want to combine your own censures, with the ones from other trusted instances, adjust the list below.
|
|
|
|
# The censures will be the combined list from your own domain and any domains specified below.
|
|
|
|
trusted_instances = [
|
2023-10-11 10:00:15 +00:00
|
|
|
"lemmy.world"
|
2023-09-08 12:55:51 +00:00
|
|
|
]
|
|
|
|
# If you want to only block based on specific filters as specified by the admins who have censured them
|
|
|
|
# You can provide them in a list below. Any instance marked with that filter from your trusted instances
|
|
|
|
# Will be added. Others will be ignored
|
|
|
|
# Sample has been provided below
|
2023-10-11 10:00:15 +00:00
|
|
|
filtered_instances = [
|
|
|
|
"lemmings.world",
|
|
|
|
]
|
|
|
|
reason_filters = [
|
|
|
|
"__all_pedos__",
|
|
|
|
"__all_bigots__",
|
|
|
|
]
|
2023-09-08 12:55:51 +00:00
|
|
|
# If you want to only censure instances which have been marked by more than 1 trusted instance, then increase the number below
|
|
|
|
min_censures = 1
|
2023-09-14 15:28:45 +00:00
|
|
|
# Change this to the number of changes over which this script will ask for manual confirmation
|
|
|
|
changes_warning_threshold = 10
|
2023-06-21 00:07:49 +00:00
|
|
|
|
2023-09-10 16:43:08 +00:00
|
|
|
password = os.getenv("LEMMY_PASSWORD", PASSWORD)
|
2023-10-24 12:02:26 +00:00
|
|
|
lemmy = Lemmy(f"https://{LEMMY_DOMAIN}", request_timeout=15)
|
2023-09-10 16:43:08 +00:00
|
|
|
if lemmy.log_in(USERNAME, password) is False:
|
2023-06-21 00:07:49 +00:00
|
|
|
raise Exception("Could not log in to lemmy")
|
|
|
|
|
2023-08-09 14:20:10 +00:00
|
|
|
fediseer = Fediseer()
|
2023-06-21 00:07:49 +00:00
|
|
|
print("Fetching suspicions")
|
2023-08-09 14:20:10 +00:00
|
|
|
sus = fediseer.suspicions.get(
|
|
|
|
activity_suspicion=ACTIVITY_SUSPICION,
|
|
|
|
active_suspicion=MONTHLY_ACTIVITY_SUSPICION,
|
|
|
|
format=FormatType.LIST
|
|
|
|
)
|
2023-10-11 10:00:15 +00:00
|
|
|
print("Fetching trusted censures")
|
2023-09-08 12:55:51 +00:00
|
|
|
trusted_instances.append(LEMMY_DOMAIN)
|
2023-10-11 10:00:15 +00:00
|
|
|
trusted_censures = fediseer.censure.get_given(
|
2023-09-08 12:55:51 +00:00
|
|
|
domain_set = set(trusted_instances),
|
2023-10-11 10:00:15 +00:00
|
|
|
min_censures = min_censures,
|
|
|
|
format = FormatType.LIST,
|
|
|
|
)
|
|
|
|
print("Fetching filtered censures")
|
|
|
|
filtered_censures = fediseer.censure.get_given(
|
|
|
|
domain_set = set(filtered_instances),
|
2023-09-08 12:55:51 +00:00
|
|
|
reasons = reason_filters,
|
|
|
|
min_censures = min_censures,
|
|
|
|
format = FormatType.LIST,
|
|
|
|
)
|
2023-10-11 10:00:15 +00:00
|
|
|
print("Fetching endorsements")
|
|
|
|
trusted_instances.append(LEMMY_DOMAIN)
|
|
|
|
endorsements = fediseer.endorsement.get_given(
|
|
|
|
domain_set = set(trusted_instances),
|
|
|
|
format = FormatType.LIST,
|
|
|
|
)
|
|
|
|
defed = (set(blocklist) | set(trusted_censures["domains"]) | set(filtered_censures["domains"]) | set(sus["domains"])) - set(safelist) - set(endorsements["domains"])
|
2023-09-14 15:28:45 +00:00
|
|
|
try:
|
|
|
|
with open("previous_defed.json", 'r') as file:
|
|
|
|
prev_defed = set(json.loads(file.read()))
|
2024-05-04 12:07:41 +00:00
|
|
|
except (FileNotFoundError,json.decoder.JSONDecodeError):
|
|
|
|
print("WARNING: Previous defed file doesn't exist. Will probably receive a warning about blocklist.")
|
2023-09-14 15:28:45 +00:00
|
|
|
prev_defed = set()
|
|
|
|
if len(defed - prev_defed) > 0:
|
|
|
|
print(f"New blocks to add {defed - prev_defed}")
|
|
|
|
if len(prev_defed - defed) > 0:
|
|
|
|
print(f"blocks that will be removed {prev_defed - defed}")
|
|
|
|
if len(defed - prev_defed) + len(prev_defed - defed) == 0:
|
|
|
|
print("No changes to do in blocklist")
|
|
|
|
sys.exit()
|
|
|
|
if len(defed - prev_defed) + len(prev_defed - defed) >= changes_warning_threshold:
|
|
|
|
if input(
|
|
|
|
f"\n### WARNING ###\n"
|
|
|
|
f"You are about to do {changes_warning_threshold} of more changes to your blocklist. "
|
|
|
|
"Proceed? (Y/n)",
|
|
|
|
) not in ["y", "Y", "", "yes"]:
|
|
|
|
print("Aborting run")
|
|
|
|
sys.exit()
|
2023-06-25 22:53:55 +00:00
|
|
|
# I need to retrieve the site info because it seems if "RequireApplication" is set
|
2023-08-09 14:20:10 +00:00
|
|
|
# We need to always re-set the application_question.
|
2023-06-25 22:53:55 +00:00
|
|
|
# So we retrieve it from the existing site, to set the same value
|
|
|
|
site = lemmy.site.get()
|
|
|
|
application_question = None
|
|
|
|
if site["site_view"]["local_site"]["registration_mode"] == "RequireApplication":
|
|
|
|
application_question = site["site_view"]["local_site"]["application_question"]
|
2023-06-21 00:07:49 +00:00
|
|
|
print("Editing Defederation list")
|
2023-06-25 22:53:55 +00:00
|
|
|
if application_question:
|
|
|
|
ret = lemmy.site.edit(
|
|
|
|
blocked_instances=list(defed),
|
|
|
|
application_question=application_question,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
ret = lemmy.site.edit(
|
|
|
|
blocked_instances=list(defed),
|
|
|
|
)
|
2023-10-24 12:02:26 +00:00
|
|
|
if ret is None:
|
|
|
|
print("Edit Failed!")
|
|
|
|
else:
|
|
|
|
print("Edit Successful.")
|
|
|
|
with open("previous_defed.json", 'w') as file:
|
|
|
|
file.write(json.dumps(list(defed),indent=4))
|
|
|
|
print("Stored previous defed list to disk file 'previous_defed.json'")
|