Merge pull request #8 from db0/find

Feat: Find instance per API key
pull/11/head
Divided by Zer0 2023-06-26 00:35:40 +02:00 committed by GitHub
commit bff47d51bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 5 deletions

View File

@ -16,8 +16,8 @@ class Models:
'total_users': fields.Integer(description="The total amount of users registered in that instance"), 'total_users': fields.Integer(description="The total amount of users registered in that instance"),
'active_users_monthly': fields.Integer(description="The amount of active users monthly."), 'active_users_monthly': fields.Integer(description="The amount of active users monthly."),
'signup': fields.Boolean(default=False,description="True when subscriptions are open, else False"), 'signup': fields.Boolean(default=False,description="True when subscriptions are open, else False"),
'activity_suspicion': fields.Float(description="Local Comments+Posts per User. Higher is worse"), 'activity_suspicion': fields.Float(description="Local Comments+Posts per user. Higher is worse"),
'activity_suspicion': fields.Float(description="Local Comments+Posts per User. Higher is worse"), 'active_users_suspicion': fields.Float(description="Monthly active users per user. Higher is worse"),
}) })
self.response_model_model_Suspicions_get = api.model('SuspiciousInstances', { self.response_model_model_Suspicions_get = api.model('SuspiciousInstances', {
'instances': fields.List(fields.Nested(self.response_model_suspicious_instances)), 'instances': fields.List(fields.Nested(self.response_model_suspicious_instances)),
@ -28,6 +28,7 @@ class Models:
'id': fields.Integer(description="The instance id", example=1), 'id': fields.Integer(description="The instance id", example=1),
'domain': fields.String(description="The instance domain", example="lemmy.dbzer0.com"), 'domain': fields.String(description="The instance domain", example="lemmy.dbzer0.com"),
'software': fields.String(description="The fediverse software running in this instance", example="lemmy"), 'software': fields.String(description="The fediverse software running in this instance", example="lemmy"),
'claimed': fields.Integer(description="How many admins from this instance has claimed it."),
'open_registrations': fields.Boolean(description="The instance uptime pct. 100% and thousand of users is unlikely"), 'open_registrations': fields.Boolean(description="The instance uptime pct. 100% and thousand of users is unlikely"),
'email_verify': fields.Boolean(description="The amount of local posts in that instance"), 'email_verify': fields.Boolean(description="The amount of local posts in that instance"),
'approvals': fields.Integer(description="The amount of endorsements this instance has given out"), 'approvals': fields.Integer(description="The amount of endorsements this instance has given out"),

View File

@ -3,9 +3,11 @@ import fediseer.apis.v1.whitelist as whitelist
import fediseer.apis.v1.endorsements as endorsements import fediseer.apis.v1.endorsements as endorsements
import fediseer.apis.v1.guarantees as guarantees import fediseer.apis.v1.guarantees as guarantees
import fediseer.apis.v1.activitypub as activitypub import fediseer.apis.v1.activitypub as activitypub
import fediseer.apis.v1.find as find
from fediseer.apis.v1.base import api from fediseer.apis.v1.base import api
api.add_resource(base.Suspicions, "/instances") api.add_resource(base.Suspicions, "/instances")
api.add_resource(find.FindInstance, "/find_instance")
api.add_resource(activitypub.User, "/user/<string:username>") api.add_resource(activitypub.User, "/user/<string:username>")
api.add_resource(activitypub.Inbox, "/inbox/<string:username>") api.add_resource(activitypub.Inbox, "/inbox/<string:username>")
api.add_resource(whitelist.Whitelist, "/whitelist") api.add_resource(whitelist.Whitelist, "/whitelist")

View File

@ -34,6 +34,7 @@ class Suspicions(Resource):
get_parser = reqparse.RequestParser() get_parser = reqparse.RequestParser()
get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers")
get_parser.add_argument("activity_suspicion", required=False, default=20, type=int, help="How many users per local post+comment to consider suspicious", location="args") get_parser.add_argument("activity_suspicion", required=False, default=20, type=int, help="How many users per local post+comment to consider suspicious", location="args")
get_parser.add_argument("active_suspicion", required=False, default=500, type=int, help="How many users per active users to consider suspicious", location="args")
get_parser.add_argument("csv", required=False, type=bool, help="Set to true to return just the domains as a csv. Mutually exclusive with domains", location="args") get_parser.add_argument("csv", required=False, type=bool, help="Set to true to return just the domains as a csv. Mutually exclusive with domains", location="args")
get_parser.add_argument("domains", required=False, type=bool, help="Set to true to return just the domains as a list. Mutually exclusive with csv", location="args") get_parser.add_argument("domains", required=False, type=bool, help="Set to true to return just the domains as a list. Mutually exclusive with csv", location="args")
@ -45,7 +46,7 @@ class Suspicions(Resource):
'''A List with the details of all suspicious instances '''A List with the details of all suspicious instances
''' '''
self.args = self.get_parser.parse_args() self.args = self.get_parser.parse_args()
sus_instances = retrieve_suspicious_instances(self.args.activity_suspicion) sus_instances = retrieve_suspicious_instances(self.args.activity_suspicion, self.args.active_suspicion)
if self.args.csv: if self.args.csv:
return {"csv": ",".join([instance["domain"] for instance in sus_instances])},200 return {"csv": ",".join([instance["domain"] for instance in sus_instances])},200
if self.args.domains: if self.args.domains:

View File

@ -0,0 +1,24 @@
from fediseer.apis.v1.base import *
class FindInstance(Resource):
get_parser = reqparse.RequestParser()
get_parser.add_argument("apikey", type=str, required=True, help="The sending instance's API key.", location='headers')
get_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers")
@api.expect(get_parser)
@api.marshal_with(models.response_model_instances, code=200, description='Instance', skip_none=True)
@api.response(401, 'Invalid API Key', models.response_model_error)
def get(self):
'''Retrieve instance information via API Key
'''
self.args = self.get_parser.parse_args()
if not self.args.apikey:
raise e.Unauthorized("You must provide the API key that was PM'd admin account")
user = database.find_user_by_api_key(self.args.apikey)
if not user:
raise e.NotFound("API key not found. Please Claim an instance first and use the API key that is PM'd to you")
instance = database.find_instance_by_user(user)
return instance.get_details(),200

View File

@ -79,6 +79,7 @@ class Instance(db.Model):
"id": self.id, "id": self.id,
"domain": self.domain, "domain": self.domain,
"software": self.software, "software": self.software,
"claimed": len(self.admins),
"open_registrations": self.open_registrations, "open_registrations": self.open_registrations,
"email_verify": self.email_verify, "email_verify": self.email_verify,
"endorsements": len(self.endorsements), "endorsements": len(self.endorsements),

View File

@ -69,7 +69,11 @@ def retrieve_suspicious_instances(activity_suspicion = 20, active_suspicious = 5
# print(node) # print(node)
# check active users (monthly is a lot lower than total users) # check active users (monthly is a lot lower than total users)
if node["total_users"] / node["active_users_monthly"] > active_suspicious: local_active_monthly_users = node["active_users_monthly"]
# Avoid division by 0
if local_active_monthly_users == 0:
local_active_monthly_users= 0.5
if node["total_users"] / local_active_monthly_users > active_suspicious:
is_bad = True is_bad = True
# print(node) # print(node)
@ -83,6 +87,7 @@ def retrieve_suspicious_instances(activity_suspicion = 20, active_suspicious = 5
"active_users_monthly": node["active_users_monthly"], "active_users_monthly": node["active_users_monthly"],
"signup": node["signup"], "signup": node["signup"],
"activity_suspicion": node["total_users"] / local_activity, "activity_suspicion": node["total_users"] / local_activity,
"active_users_suspicion": node["total_users"] / local_active_monthly_users,
} }
bad_nodes.append(bad_node) bad_nodes.append(bad_node)
return bad_nodes return bad_nodes