2018-12-21 20:49:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
2019-06-26 10:08:43 +02:00
|
|
|
import os
|
2019-04-27 15:06:03 +02:00
|
|
|
import subprocess
|
2019-06-26 10:08:43 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import requests
|
2018-12-22 04:46:57 +01:00
|
|
|
from ruamel.yaml import YAML, scalarstring
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BlacklistImporter:
|
|
|
|
def __init__(self, args):
|
|
|
|
self.outfile = args.outfile
|
|
|
|
self.dryrun = args.dryrun
|
2019-04-25 15:34:47 +02:00
|
|
|
self.path = os.path.dirname(__file__)
|
2018-12-21 20:49:15 +01:00
|
|
|
self.url = "https://raw.githubusercontent.com/JabberSPAM/blacklist/master/blacklist.txt"
|
2019-03-10 14:55:20 +01:00
|
|
|
self.blacklist = ""
|
2018-12-22 04:46:57 +01:00
|
|
|
self.change = False
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
def request(self):
|
2019-03-10 14:55:20 +01:00
|
|
|
"""
|
|
|
|
determine if the download is required
|
|
|
|
"""
|
2019-06-26 10:08:43 +02:00
|
|
|
etag_path = "/".join([self.path, ".etag"])
|
|
|
|
blacklist_path = "/".join([self.path, "blacklist.txt"])
|
2019-04-25 15:34:47 +02:00
|
|
|
|
2018-12-21 20:49:15 +01:00
|
|
|
# check if etag header is present if not set local_etag to ""
|
2019-04-25 15:34:47 +02:00
|
|
|
if os.path.isfile(etag_path):
|
2019-03-10 14:55:20 +01:00
|
|
|
# catch special case were etag file is present and blacklist.txt is not
|
2019-04-25 15:34:47 +02:00
|
|
|
if not os.path.isfile(blacklist_path):
|
2019-03-10 14:55:20 +01:00
|
|
|
local_etag = ""
|
|
|
|
else:
|
|
|
|
# if both files are present continue normally
|
2019-04-27 15:06:03 +02:00
|
|
|
with open(etag_path, "r") as local_file:
|
|
|
|
local_etag = local_file.read()
|
2018-12-21 20:49:15 +01:00
|
|
|
else:
|
|
|
|
local_etag = ""
|
|
|
|
|
|
|
|
with requests.Session() as s:
|
|
|
|
# head request to check etag
|
|
|
|
head = s.head(self.url)
|
|
|
|
etag = head.headers['etag']
|
|
|
|
|
2019-03-10 14:55:20 +01:00
|
|
|
# if etags match up or if the connection is not possible fall back to local cache
|
2019-02-15 19:39:27 +01:00
|
|
|
if local_etag == etag or head.status_code != 200:
|
2019-03-10 14:55:20 +01:00
|
|
|
# if local cache is present overwrite blacklist var
|
2019-04-25 15:34:47 +02:00
|
|
|
if os.path.isfile(blacklist_path):
|
2019-04-27 15:06:03 +02:00
|
|
|
with open(blacklist_path, "r", encoding="utf-8") as local_file:
|
|
|
|
self.blacklist = local_file.read()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
# in any other case request a new file
|
|
|
|
else:
|
|
|
|
r = s.get(self.url)
|
|
|
|
r.encoding = 'utf-8'
|
|
|
|
local_etag = head.headers['etag']
|
2018-12-22 04:46:57 +01:00
|
|
|
self.blacklist = r.content.decode()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2019-04-27 15:06:03 +02:00
|
|
|
with open(blacklist_path, "w") as local_file:
|
|
|
|
local_file.write(self.blacklist)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2019-04-27 15:06:03 +02:00
|
|
|
with open(etag_path, 'w') as local_file:
|
|
|
|
local_file.write(local_etag)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
def main(self):
|
|
|
|
# first check if blacklist is updated
|
|
|
|
self.request()
|
|
|
|
|
2019-06-26 11:17:56 +02:00
|
|
|
# only output the selected outfile
|
2018-12-21 20:49:15 +01:00
|
|
|
if self.dryrun:
|
|
|
|
print("outfile selected: %s" % self.outfile)
|
|
|
|
|
2019-06-26 11:17:56 +02:00
|
|
|
# blacklist processing
|
2018-12-22 04:46:57 +01:00
|
|
|
self.process()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
# reload config if changes have been applied
|
|
|
|
if self.change:
|
2019-06-26 12:47:24 +02:00
|
|
|
# catch ejabberdctl missing
|
|
|
|
if os.path.isfile('/usr/sbin/ejabberdctl'):
|
|
|
|
subprocess.call(['/usr/sbin/ejabberdctl', 'reload_config'], shell=False)
|
|
|
|
|
|
|
|
# report missing ejabberdctl reload_config
|
|
|
|
else:
|
|
|
|
print('/usr/sbin/ejabberdctl was not found', file=sys.stderr)
|
|
|
|
print('blacklist changes have been applied\nejabberd config was not reloaded', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
def process(self):
|
2019-03-10 14:55:20 +01:00
|
|
|
"""
|
|
|
|
function to build and compare the local yaml file to the remote file
|
|
|
|
if the remote file is different, the local file gets overwritten
|
|
|
|
"""
|
2018-12-22 14:57:13 +01:00
|
|
|
# init new YAML variable
|
|
|
|
local_file = YAML(typ="safe")
|
2019-03-04 01:17:02 +01:00
|
|
|
|
2019-06-26 11:17:56 +02:00
|
|
|
# None catch
|
2019-03-04 01:17:02 +01:00
|
|
|
if self.outfile is not None:
|
2019-03-10 14:55:20 +01:00
|
|
|
# prevent FileNotFoundError on first run or file missing
|
|
|
|
if os.path.isfile(self.outfile):
|
2019-03-04 01:17:02 +01:00
|
|
|
local_file = local_file.load(open(self.outfile, "r", encoding="utf-8"))
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2019-03-10 14:55:20 +01:00
|
|
|
# blacklist frame
|
2018-12-21 20:49:15 +01:00
|
|
|
remote_file = {
|
|
|
|
"acl": {
|
|
|
|
"spamblacklist": {
|
|
|
|
"server": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 14:55:20 +01:00
|
|
|
# build the blacklist with the given frame to compare to local blacklist
|
2018-12-21 20:49:15 +01:00
|
|
|
for entry in self.blacklist.split():
|
2018-12-22 04:46:57 +01:00
|
|
|
entry = scalarstring.DoubleQuotedScalarString(entry)
|
2018-12-21 20:49:15 +01:00
|
|
|
remote_file["acl"]["spamblacklist"]["server"].append(entry)
|
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
yml = YAML()
|
|
|
|
yml.indent(offset=2)
|
|
|
|
yml.default_flow_style = False
|
|
|
|
|
2019-06-26 11:17:56 +02:00
|
|
|
# if dry-run true print expected content
|
2018-12-21 20:49:15 +01:00
|
|
|
if self.dryrun:
|
2018-12-22 04:46:57 +01:00
|
|
|
yml.dump(remote_file, sys.stdout)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2019-06-26 11:17:56 +02:00
|
|
|
# only if the local_file and remote_file are unequal write new file
|
2018-12-21 20:49:15 +01:00
|
|
|
elif local_file != remote_file:
|
2019-06-26 11:17:56 +02:00
|
|
|
|
|
|
|
# prevent FileNotFoundError if self.outfile is not assigned
|
|
|
|
if self.outfile is None:
|
|
|
|
print("no outfile assigned", file=sys.stderr)
|
|
|
|
print(parser.format_help(), file=sys.stderr)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
# proceed to update the defined outfile
|
|
|
|
elif self.outfile is not None:
|
|
|
|
self.change = True
|
|
|
|
yml.dump(remote_file, open(self.outfile, "w"))
|
|
|
|
|
|
|
|
# if that's impossible break and display help message
|
|
|
|
else:
|
|
|
|
print(parser.format_help(), file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-o', '--outfile', help='set path to output file', dest='outfile', default=None)
|
2018-12-22 04:46:57 +01:00
|
|
|
parser.add_argument('-dr', '--dry-run', help='perform a dry run', action='store_true', dest='dryrun', default=False)
|
2018-12-21 20:49:15 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
# run
|
2018-12-21 20:49:15 +01:00
|
|
|
BlacklistImporter(args).main()
|