262 lines
4.5 KiB
Python
262 lines
4.5 KiB
Python
import re
|
|
import logging as logger
|
|
|
|
from datetime import datetime
|
|
from ipaddress import ip_address as address
|
|
from urllib.parse import urlparse
|
|
|
|
from .log import logging
|
|
from .functions import format_urls
|
|
from .database import get, put, bool_check
|
|
from . import messages
|
|
|
|
|
|
def get_instance_data():
|
|
newcutoff = 60*60*24*2
|
|
current_time = datetime.timestamp(datetime.now())
|
|
instances = []
|
|
|
|
for inbox in get.inbox('all'):
|
|
retries = get.retries({'inbox': inbox['inbox']})
|
|
tag = ''
|
|
|
|
if retries:
|
|
tag = 'fail'
|
|
|
|
elif current_time - newcutoff < inbox['timestamp']:
|
|
tag = 'new'
|
|
|
|
instances.append({
|
|
'domain': inbox['domain'],
|
|
'date': datetime.fromtimestamp(inbox['timestamp']).strftime('%Y-%m-%d'),
|
|
'tag': tag,
|
|
'retries': retries
|
|
})
|
|
|
|
return instances
|
|
|
|
|
|
def get_whitelist_data():
|
|
instances = []
|
|
|
|
for instance in get.whitelist('all'):
|
|
instances.append({
|
|
'domain': instance['domain'],
|
|
'date': datetime.fromtimestamp(instance['timestamp']).strftime('%Y-%m-%d')
|
|
})
|
|
|
|
return instances
|
|
|
|
|
|
def get_domainbans():
|
|
instances = []
|
|
|
|
for instance in get.domainban('all'):
|
|
instances.append({
|
|
'domain': instance['domain'],
|
|
'reason': instance['reason']
|
|
})
|
|
|
|
return instances
|
|
|
|
|
|
def get_userbans():
|
|
users = []
|
|
|
|
for user in get.userban(None, 'all'):
|
|
users.append({
|
|
'user': user['username'],
|
|
'domain': user['domain'],
|
|
'reason': user['reason']
|
|
})
|
|
|
|
return users
|
|
|
|
|
|
def sanitize(data, extras=None):
|
|
if extras == 'spaces':
|
|
extra = '\s'
|
|
|
|
elif extras == 'markdown':
|
|
extra = '>|`()[\]*#~\-:/\s'
|
|
|
|
else:
|
|
extra = ''
|
|
|
|
return re.sub(r'[^a-zA-Z0-9@_.\-\!\'d,%{}]+'.format(extra), '', data).strip()
|
|
|
|
|
|
def ip_check(ip):
|
|
try:
|
|
return address(ip)
|
|
|
|
except:
|
|
return '127.0.0.1'
|
|
|
|
|
|
def port_check(port):
|
|
if int(port) < 1 or int(port) > 25565:
|
|
return 3621
|
|
|
|
else:
|
|
return int(port)
|
|
|
|
|
|
def settings(data):
|
|
#if not config('whitelist') and bool_check(sanitize(data['whitelist'])):
|
|
# for domain in [inbox['domain'] for inbox in table.inbox.all()]:
|
|
# if domain not in LIST['whitelist']:
|
|
# update_actor(remove, f'https://{domain}/inbox')
|
|
|
|
new_data = {}
|
|
|
|
for setting in ['info', 'rules']:
|
|
if not data.get(setting):
|
|
put.config({setting: None})
|
|
|
|
for k, v in data.items():
|
|
if k == 'port':
|
|
try:
|
|
new_data.update({k: int(v)})
|
|
|
|
except ValueError:
|
|
logging.warning(f'{v} is not a valid value for \'port\'')
|
|
|
|
else:
|
|
new_data.update({k: v})
|
|
|
|
put.config(new_data)
|
|
|
|
logging.setLevel(data["log_level"])
|
|
|
|
def ban(data):
|
|
domain = data['name']
|
|
reason = data.get('reason')
|
|
|
|
if put.ban('add', domain, reason=reason):
|
|
logging.info(f'Added {domain} to the banlist')
|
|
|
|
else:
|
|
logging.info(f'Failed to add {domain} to the banlist')
|
|
|
|
|
|
def unban(data):
|
|
domain = data['name']
|
|
|
|
if put.ban('remove', domain):
|
|
logging.info(f'Removed {domain} from the banlist')
|
|
|
|
else:
|
|
logging.info(f'Failed to remove {domain} from the banlist')
|
|
|
|
|
|
def add(data):
|
|
domain = data['name']
|
|
|
|
if put.whitelist('add', domain):
|
|
logging.info(f'Added {domain} to the whitelist')
|
|
|
|
else:
|
|
logging.info(f'Failed to add {domain} to the whitelist')
|
|
|
|
def remove(data):
|
|
domain = data['name']
|
|
|
|
if put.whitelist('remove', domain):
|
|
logging.info(f'Removed {domain} from the whitelist')
|
|
|
|
else:
|
|
logging.info(f'Failed to remove {domain} from the whitelist')
|
|
|
|
|
|
def accept(data):
|
|
row = get.request(data.get('name'))
|
|
|
|
if not row:
|
|
return
|
|
|
|
if not messages.accept(row['followid'], row):
|
|
return
|
|
|
|
if put.inbox('add', row):
|
|
put.request('remove', row)
|
|
|
|
return True
|
|
|
|
|
|
def deny(data):
|
|
row = get.request(data.get('name'))
|
|
|
|
if not row:
|
|
return
|
|
|
|
if put.request('remove', row):
|
|
return True
|
|
|
|
|
|
def eject(data):
|
|
row = get.inbox(data.get('name'))
|
|
|
|
if not row:
|
|
return
|
|
|
|
if put.inbox('remove', row):
|
|
return True
|
|
|
|
|
|
def retry(data):
|
|
rowid = data.get('name')
|
|
|
|
if not rowid:
|
|
return
|
|
|
|
messages.run_retries(msgid=rowid)
|
|
|
|
|
|
def remret(data):
|
|
rowid = data.get('name')
|
|
|
|
if not rowid:
|
|
return
|
|
|
|
try:
|
|
rowid = int(rowid)
|
|
except:
|
|
return
|
|
|
|
put.del_retries(rowid)
|
|
|
|
|
|
def auth_code(data):
|
|
action = data.get('action', '').lower()
|
|
|
|
if action in ['delete', 'regen']:
|
|
get.code(action)
|
|
act_msg = 'Updated' if action == 'regen' else 'Removed'
|
|
|
|
return f'{act_msg} authentication code'
|
|
|
|
return f'Invalid auth code action: {action}'
|
|
|
|
|
|
def run(action, data):
|
|
cmd = {
|
|
'settings': settings,
|
|
'ban': ban,
|
|
'unban': unban,
|
|
'add': add,
|
|
'remove': remove,
|
|
'accept': accept,
|
|
'deny': deny,
|
|
'eject': eject,
|
|
'retry': retry,
|
|
'remret': remret,
|
|
'authcode': auth_code
|
|
}
|
|
|
|
if action in cmd:
|
|
return cmd[action](data)
|
|
|
|
else:
|
|
logging.error(f'Invalid admin post action: {action}')
|