Merge remote-tracking branch 'origin/main'

This commit is contained in:
rimu 2024-05-24 16:32:24 +12:00
commit b97867a0b9
4 changed files with 72 additions and 28 deletions

View file

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

View file

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

View file

@ -68,6 +68,7 @@ class Instance(db.Model):
ip_address = db.Column(db.String(50))
trusted = db.Column(db.Boolean, default=False)
posting_warning = db.Column(db.String(512))
nodeinfo_href = db.Column(db.String(100))
posts = db.relationship('Post', backref='instance', lazy='dynamic')
post_replies = db.relationship('PostReply', backref='instance', lazy='dynamic')

View file

@ -0,0 +1,32 @@
"""instance nodeinfo href
Revision ID: dfba54bdaeb2
Revises: a32b474a40dd
Create Date: 2024-05-23 14:14:11.293845
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'dfba54bdaeb2'
down_revision = 'a32b474a40dd'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('instance', schema=None) as batch_op:
batch_op.add_column(sa.Column('nodeinfo_href', sa.String(length=100), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('instance', schema=None) as batch_op:
batch_op.drop_column('nodeinfo_href')
# ### end Alembic commands ###