Browse Source

add anti ddos issue

tags/1.2.2
Dryusdan 2 years ago
parent
commit
20d7e480c1
2 changed files with 54 additions and 19 deletions
  1. +54
    -19
      bot.py
  2. +0
    -0
      limiter/README.md

+ 54
- 19
bot.py View File

@@ -7,7 +7,7 @@
from mastodon import StreamListener
from lxml import html
from logging.handlers import RotatingFileHandler
#from pprint import pprint
from pprint import pprint
from utils.config import get_parameter, init_log, init_mastodon

import requests, os, random, sys, time, json, logging, argparse, re
@@ -44,24 +44,59 @@ def cleanhtml(raw_html):

class BotListener(StreamListener):

def on_notification(self, notification):
if notification['type'] == 'mention':
log.debug("Got a mention")
id = notification['status']['id']
sender = notification['account']['acct']
visibility = notification['status']['visibility']
if visibility == 'public':
visibility = 'unlisted'
mentions = notification['status']['mentions']
text = "@" + notification['status']["account"]["acct"] + " "
for mention in mentions:
if mention["acct"] != bot_name:
text = text + "@" + mention["acct"] + " "
media_dict = post_img(mastodon, "NSFW", 1, log, config_file)
mastodon.status_post(text, in_reply_to_id=id, media_ids=[media_dict], sensitive=True, visibility=visibility, spoiler_text="#NSFW")
else:
log.debug("Nevermind")
# use only notification
def on_notification(self, notification):

# catch only mention in notification
if notification['type'] == 'mention':
log.debug("Got a mention")
sender = notification['account']['acct'] # Get sender name
sender_filename = "limiter/" + sender; # Forge file for limiter
if os.path.isfile(sender_filename): # Check if file exist
statbuf = os.stat(sender_filename)
last_edit = int(statbuf.st_mtime)
ts = int(time.time())
pprint(last_edit)
pprint(ts)
if ts - last_edit > 59: # check if file is modified 1 minute after last edition
f = open(sender_filename,'w')
f.write(str(1)) # reset counter
f.close()
can_continue = True
else:
f = open(sender_filename,'r+')
number_of_mention = int(f.read())
if number_of_mention < 5: # limit of mention per minute is 4
f.seek(0)
f.write(str(number_of_mention + 1))
can_continue = True
else:
can_continue = False # if number of mention is for, user can't receive anything
f.close()
else: # File not exist, create it and initialise it
f = open(sender_filename,"w+")
f.write(str(1))
f.close()
can_continue = True

if can_continue:
id = notification['status']['id']
visibility = notification['status']['visibility']
if visibility == 'public':
visibility = 'unlisted'
mentions = notification['status']['mentions']
text = "@" + notification['status']["account"]["acct"] + " "
for mention in mentions:
if mention["acct"] != bot_name:
text = text + "@" + mention["acct"] + " "
media_dict = post_img(mastodon, "NSFW", 1, log, config_file)
mastodon.status_post(text, in_reply_to_id=id, media_ids=[media_dict], sensitive=True, visibility=visibility, spoiler_text="#NSFW")
else:
log.debug("no picture send :(")
pass
else:
log.debug("Nevermind")


def main():


+ 0
- 0
limiter/README.md View File


Loading…
Cancel
Save