From b45cd82196aa39099335fcb0838ce53c6d3be8ac Mon Sep 17 00:00:00 2001 From: db0 Date: Wed, 21 Jun 2023 00:14:53 +0200 Subject: [PATCH] SusInstances --- overseer/apis/models/v1.py | 11 +++++++++++ overseer/apis/v1/base.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/overseer/apis/models/v1.py b/overseer/apis/models/v1.py index 5be2b19..1058cdb 100644 --- a/overseer/apis/models/v1.py +++ b/overseer/apis/models/v1.py @@ -2,6 +2,9 @@ from flask_restx import fields class Models: def __init__(self,api): + self.response_model_error = api.model('RequestError', { + 'message': fields.String(description="The error message for this status code."), + }) self.response_model_suspicious_instances = api.model('SuspiciousInstances', { 'domain': fields.String(description="The instance domain"), 'uptime_alltime': fields.Float(description="The instance uptime pct. 100% and thousand of users is unlikely"), @@ -11,3 +14,11 @@ class Models: 'signup': fields.Boolean(default=False,description="True when subscriptions are open, else False"), 'user_post_ratio': fields.Float(description="Users to Post Ratio"), }) + self.input_model_SusInstances_post = api.model('SuspiciousInstancesListInput', { + 'user_to_post_ratio': fields.Integer(default=20,description="The threshold over which to consider instances suspicious."), + 'whitelist': fields.List(fields.String(description="List of domains to avoid returning in the supicion list.")), + 'blacklist': fields.List(fields.String(description="List of domains to append to the supicion list.")), + }) + self.response_model_model_SusInstances_post = api.model('SuspiciousInstancesDomainList', { + 'domains': fields.List(fields.String(description="The domains in shit suspicious list.")), + }) diff --git a/overseer/apis/v1/base.py b/overseer/apis/v1/base.py index b746f4f..e0c8794 100644 --- a/overseer/apis/v1/base.py +++ b/overseer/apis/v1/base.py @@ -29,4 +29,33 @@ class SusInstances(Resource): '''A List with the details of all suspicious instances ''' self.args = self.get_parser.parse_args() - return retrieve_suspicious_instances(self.args.user_to_post_ratio),200 \ No newline at end of file + return retrieve_suspicious_instances(self.args.user_to_post_ratio),200 + + post_parser = reqparse.RequestParser() + post_parser.add_argument("Client-Agent", default="unknown:0:unknown", type=str, required=False, help="The client name and version.", location="headers") + post_parser.add_argument("user_to_post_ratio", required=False, default=20, type=int, help="The amount of local users / amount of local posts to consider suspicious", location="json") + post_parser.add_argument("whitelist", type=list, required=False, help="Workers to whitelist even if we think they're suspicious", location="json") + post_parser.add_argument("blacklist", type=list, required=False, help="Extra workers to blacklist.", location="json") + + @api.expect(post_parser, models.input_model_SusInstances_post, validate=True) + @logger.catch(reraise=True) + # @cache.cached(timeout=10, query_string=True) + @api.marshal_with(models.response_model_model_SusInstances_post, code=200, description='Suspicious Instances List') + @api.response(400, 'Validation Error', models.response_model_error) + def post(self): + '''A List with just the domains of all suspicious instances + This can then be easily converted into a defederation list + ''' + self.args = self.post_parser.parse_args() + logger.debug(self.args) + sus_instances = retrieve_suspicious_instances(self.args.user_to_post_ratio) + final_list = [] + if self.args.blacklist: + final_list = self.args.blacklist + whitelist = [] + if self.args.whitelist: + whitelist = self.args.whitelist + for instance in sus_instances: + if instance["domain"] not in whitelist: + final_list.append(instance["domain"]) + return {"domains": final_list},200