Use Instance.nodeinfo_href if available

This commit is contained in:
freamon 2024-05-23 17:12:04 +01:00
parent 7b7f9f4664
commit cd9da2abac
2 changed files with 39 additions and 28 deletions

View file

@ -1132,12 +1132,11 @@ def new_instance_profile_task(instance_id: int):
if 'software' in node_json: if 'software' in node_json:
instance.software = node_json['software']['name'].lower() instance.software = node_json['software']['name'].lower()
instance.version = node_json['software']['version'] instance.version = node_json['software']['version']
instance.nodeinfo_href = links['href']
db.session.commit() db.session.commit()
except: except:
# todo: update new field in Instance to indicate bad nodeinfo response
return return
except: except:
# todo: update new field in Instance to indicate bad nodeinfo response
return return

View file

@ -183,35 +183,47 @@ def register(app):
instances = Instance.query.filter(Instance.gone_forever == False, Instance.id != 1).all() instances = Instance.query.filter(Instance.gone_forever == False, Instance.id != 1).all()
HEADERS = {'User-Agent': 'PieFed/1.0', 'Accept': 'application/activity+json'} HEADERS = {'User-Agent': 'PieFed/1.0', 'Accept': 'application/activity+json'}
for instance in instances: for instance in instances:
try: if not instance.nodeinfo_href:
nodeinfo = requests.get(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS, try:
timeout=5, allow_redirects=True) nodeinfo = requests.get(f"https://{instance.domain}/.well-known/nodeinfo", headers=HEADERS,
timeout=5, allow_redirects=True)
if nodeinfo.status_code == 200: if nodeinfo.status_code == 200:
nodeinfo_json = nodeinfo.json() nodeinfo_json = nodeinfo.json()
for links in nodeinfo_json['links']: for links in nodeinfo_json['links']:
if 'rel' in links and ( if 'rel' in links and (
links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or links['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0' or
links['rel'] == 'https://nodeinfo.diaspora.software/ns/schema/2.0'): links['rel'] == 'https://nodeinfo.diaspora.software/ns/schema/2.0'):
try: instance.nodeinfo_href = links['href']
instance.failures = 0
instance.dormant = False
db.session.commit()
sleep(0.1) sleep(0.1)
node = requests.get(links['href'], headers=HEADERS, timeout=5, break
allow_redirects=True) elif node.status_code >= 400:
if node.status_code == 200: current_app.logger.info(f"{instance.domain} has no well-known/nodeinfo response")
node_json = node.json() except requests.exceptions.ReadTimeout:
if 'software' in node_json: instance.failures += 1
instance.software = node_json['software']['name'].lower() except requests.exceptions.ConnectionError:
instance.version = node_json['software']['version'] instance.failures += 1
instance.failures = 0 except requests.exceptions.RequestException:
instance.dormant = False pass
elif node.status_code >= 400:
instance.failures += 1 if instance.nodeinfo_href:
except: try:
instance.failures += 1 node = requests.get(instance.nodeinfo_href, headers=HEADERS, timeout=5,
elif nodeinfo.status_code >= 400: allow_redirects=True)
if node.status_code == 200:
node_json = node.json()
if 'software' in node_json:
instance.software = node_json['software']['name'].lower()
instance.version = node_json['software']['version']
instance.failures = 0
instance.dormant = False
elif node.status_code >= 400:
instance.failures += 1
except requests.exceptions.RequestException:
instance.failures += 1 instance.failures += 1
except:
instance.failures += 1
if instance.failures > 7 and instance.dormant == True: if instance.failures > 7 and instance.dormant == True:
instance.gone_forever = True instance.gone_forever = True
elif instance.failures > 2 and instance.dormant == False: elif instance.failures > 2 and instance.dormant == False: