2023-06-22 00:04:45 +00:00
import os
2023-06-20 17:47:56 +00:00
from flask import request
from flask_restx import Namespace , Resource , reqparse
2023-06-24 00:23:53 +00:00
from fediseer . flask import cache , db
from fediseer . observer import retrieve_suspicious_instances
2023-06-20 17:47:56 +00:00
from loguru import logger
2023-06-24 00:23:53 +00:00
from fediseer . classes . instance import Instance
from fediseer . database import functions as database
from fediseer import exceptions as e
from fediseer . utils import hash_api_key
from fediseer . messaging import activitypub_pm
2023-09-24 16:15:52 +00:00
from fediseer . fediverse import InstanceInfo
2023-09-17 21:42:11 +00:00
from fediseer . limiter import limiter
2023-12-29 15:44:57 +00:00
from fediseer import consts
2023-06-20 17:47:56 +00:00
api = Namespace ( ' v1 ' , ' API Version 1 ' )
2023-06-24 00:23:53 +00:00
from fediseer . apis . models . v1 import Models
2023-06-20 17:47:56 +00:00
models = Models ( api )
2023-06-22 00:04:45 +00:00
handle_bad_request = api . errorhandler ( e . BadRequest ) ( e . handle_bad_requests )
handle_forbidden = api . errorhandler ( e . Forbidden ) ( e . handle_bad_requests )
2023-06-22 00:32:08 +00:00
handle_unauthorized = api . errorhandler ( e . Unauthorized ) ( e . handle_bad_requests )
handle_not_found = api . errorhandler ( e . NotFound ) ( e . handle_bad_requests )
2023-09-17 21:42:11 +00:00
handle_too_many_requests = api . errorhandler ( e . TooManyRequests ) ( e . handle_bad_requests )
2023-07-09 15:54:48 +00:00
handle_internal_server_error = api . errorhandler ( e . InternalServerError ) ( e . handle_bad_requests )
handle_service_unavailable = api . errorhandler ( e . ServiceUnavailable ) ( e . handle_bad_requests )
2023-06-22 00:04:45 +00:00
2023-06-20 17:47:56 +00:00
# Used to for the flask limiter, to limit requests per url paths
def get_request_path ( ) :
# logger.info(dir(request))
return f " { request . remote_addr } @ { request . method } @ { request . path } "
2023-06-21 17:37:34 +00:00
class Suspicions ( Resource ) :
2023-06-20 17:47:56 +00:00
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 " )
2023-06-20 23:57:30 +00:00
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 " )
2023-06-25 22:32:41 +00:00
get_parser . add_argument ( " active_suspicion " , required = False , default = 500 , type = int , help = " How many users per active users to consider suspicious " , location = " args " )
2023-06-20 22:41:13 +00:00
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 " )
2023-06-20 17:47:56 +00:00
@api.expect ( get_parser )
@cache.cached ( timeout = 10 , query_string = True )
2023-06-21 17:37:34 +00:00
@api.marshal_with ( models . response_model_model_Suspicions_get , code = 200 , description = ' Suspicious Instances ' , skip_none = True )
2023-06-20 17:47:56 +00:00
def get ( self ) :
''' A List with the details of all suspicious instances
'''
self . args = self . get_parser . parse_args ( )
2023-07-09 15:54:48 +00:00
try :
sus_instances = retrieve_suspicious_instances ( self . args . activity_suspicion , self . args . active_suspicion )
except Exception as err :
raise e . ServiceUnavailable ( " Could not retrieve list from Fediseer Observer. Please try again later " )
2023-06-20 22:33:37 +00:00
if self . args . csv :
return { " csv " : " , " . join ( [ instance [ " domain " ] for instance in sus_instances ] ) } , 200
if self . args . domains :
return { " domains " : [ instance [ " domain " ] for instance in sus_instances ] } , 200
return { " instances " : sus_instances } , 200
2023-06-21 17:37:34 +00:00
2023-12-29 15:44:57 +00:00
class Config ( Resource ) :
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 " )
@api.expect ( get_parser )
@cache.cached ( timeout = 600 )
@api.marshal_with ( models . response_model_model_Config_get , code = 200 , description = ' Fediseer config ' )
def get ( self ) :
''' The current Fediseer configuration options
'''
self . args = self . get_parser . parse_args ( )
return {
' max_guarantees ' : consts . MAX_GUARANTEES ,
' max_guarantors ' : consts . MAX_GUARANTORS ,
' max_tags ' : consts . MAX_TAGS ,
' max_config_actions_per_min ' : consts . MAX_CONFIG_ACTIONS_PER_MIN ,
} , 200
2023-09-24 16:15:52 +00:00
# Debug
# from fediseer.flask import OVERSEER
# with OVERSEER.app_context():
# logger.debug(ensure_instance_registered("lemmings.world"))
# import sys
# sys.exit()