Lookup nodeinfo url with /.well-known/nodeinfo (#367)

Fixes #364
This commit is contained in:
Michael Manfre 2023-01-07 12:36:36 -05:00 committed by GitHub
parent 35b14066a5
commit db186fcd73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View file

@ -1,3 +1,4 @@
from asgiref.sync import async_to_sync
from django.contrib import admin from django.contrib import admin
from django.db import models from django.db import models
from django.utils import formats from django.utils import formats
@ -30,7 +31,12 @@ class DomainAdmin(admin.ModelAdmin):
] ]
list_filter = ("local", "blocked") list_filter = ("local", "blocked")
search_fields = ("domain", "service_domain") search_fields = ("domain", "service_domain")
actions = ["force_outdated", "force_updated", "force_connection_issue"] actions = [
"force_outdated",
"force_updated",
"force_connection_issue",
"fetch_nodeinfo",
]
@admin.action(description="Force State: outdated") @admin.action(description="Force State: outdated")
def force_outdated(self, request, queryset): def force_outdated(self, request, queryset):
@ -47,6 +53,14 @@ class DomainAdmin(admin.ModelAdmin):
for instance in queryset: for instance in queryset:
instance.transition_perform("connection_issue") instance.transition_perform("connection_issue")
@admin.action(description="Fetch nodeinfo")
def fetch_nodeinfo(self, request, queryset):
for instance in queryset:
info = async_to_sync(instance.fetch_nodeinfo)()
if info:
instance.nodeinfo = info.dict()
instance.save()
@admin.display(description="Software") @admin.display(description="Software")
def software(self, instance): def software(self, instance):
if instance.nodeinfo: if instance.nodeinfo:

View file

@ -150,13 +150,35 @@ class Domain(StatorModel):
""" """
Fetch the /NodeInfo/2.0 for the domain Fetch the /NodeInfo/2.0 for the domain
""" """
nodeinfo20_url = f"https://{self.domain}/nodeinfo/2.0"
async with httpx.AsyncClient( async with httpx.AsyncClient(
timeout=settings.SETUP.REMOTE_TIMEOUT, timeout=settings.SETUP.REMOTE_TIMEOUT,
headers={"User-Agent": settings.TAKAHE_USER_AGENT}, headers={"User-Agent": settings.TAKAHE_USER_AGENT},
) as client: ) as client:
try: try:
response = await client.get( response = await client.get(
f"https://{self.domain}/nodeinfo/2.0", f"https://{self.domain}/.well-known/nodeinfo",
follow_redirects=True,
headers={"Accept": "application/json"},
)
except httpx.HTTPError:
pass
else:
try:
for link in response.json().get("links", []):
if (
link.get("rel")
== "http://nodeinfo.diaspora.software/ns/schema/2.0"
):
nodeinfo20_url = link.get("href", nodeinfo20_url)
break
except json.JSONDecodeError:
pass
try:
response = await client.get(
nodeinfo20_url,
follow_redirects=True, follow_redirects=True,
headers={"Accept": "application/json"}, headers={"Accept": "application/json"},
) )