From 16cd128767124bf3190632b8358d534da3434904 Mon Sep 17 00:00:00 2001 From: db0 Date: Thu, 22 Jun 2023 19:37:03 +0200 Subject: [PATCH] support other software --- overseer/apis/v1/whitelist.py | 30 ++++++++++++++++++++++-------- overseer/utils.py | 11 ++++++++++- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/overseer/apis/v1/whitelist.py b/overseer/apis/v1/whitelist.py index 4339585..8eb9741 100644 --- a/overseer/apis/v1/whitelist.py +++ b/overseer/apis/v1/whitelist.py @@ -1,4 +1,5 @@ from overseer.apis.v1.base import * +from overseer.utils import get_nodeinfo class Whitelist(Resource): get_parser = reqparse.RequestParser() @@ -68,21 +69,34 @@ class WhitelistDomain(Resource): if domain.endswith("test.dbzer0.com"): requested_lemmy = Lemmy(f"https://{domain}") requested_lemmy._requestor.nodeinfo = {"software":{"name":"lemmy"}} - site = {"site_view":{"local_site":{"require_email_verification": True,"registration_mode":"open"}}} + open_registrations = False + email_verify = True + software = "lemmy" else: - requested_lemmy = Lemmy(f"https://{domain}") - site = requested_lemmy.site.get() - if not site: - raise e.BadRequest(f"Error encountered while polling domain {domain}. Please check it's running correctly") + nodeinfo = get_nodeinfo(domain) + if not nodeinfo: + raise e.BadRequest(f"Error encountered while polling domain {domain}. Please check it's running correctly") + software = nodeinfo["software"]["name"] + if software == "lemmy": + requested_lemmy = Lemmy(f"https://{domain}") + site = requested_lemmy.site.get() + if not site: + raise e.BadRequest(f"Error encountered while polling lemmy domain {domain}. Please check it's running correctly") + open_registrations = site["site_view"]["local_site"]["registration_mode"] == "open" + email_verify = site["site_view"]["local_site"]["require_email_verification"] + software = software + else: + open_registrations = nodeinfo["openRegistrations"] + email_verify = False api_key = pm_new_api_key(domain) if not api_key: raise e.BadRequest("Failed to generate API Key") new_instance = Instance( domain=domain, api_key=hash_api_key(api_key), - open_registrations=site["site_view"]["local_site"]["registration_mode"] == "open", - email_verify=site["site_view"]["local_site"]["require_email_verification"], - software=requested_lemmy.nodeinfo['software']['name'], + open_registrations=open_registrations, + email_verify=email_verify, + software=software, ) new_instance.create() if guarantor_instance: diff --git a/overseer/utils.py b/overseer/utils.py index 6fca757..06c879e 100644 --- a/overseer/utils.py +++ b/overseer/utils.py @@ -10,6 +10,7 @@ from datetime import datetime import dateutil.relativedelta from loguru import logger from overseer.flask import SQLITE_MODE +import requests random.seed(random.SystemRandom().randint(0, 2**32 - 1)) @@ -100,4 +101,12 @@ def validate_regex(regex_string): re.compile(regex_string, re.IGNORECASE) except: return False - return True \ No newline at end of file + return True + +def get_nodeinfo(domain): + try: + wellknown = requests.get(f"https://{domain}/.well-known/nodeinfo", timeout=2).json() + nodeinfo = requests.get(wellknown['links'][0]['href'], timeout=2).json() + return nodeinfo + except Exception as err: + return None \ No newline at end of file