code finishup and cleanup

+ yaml composing ejabberd conformal
+ added README.md
+ added .gitignore
+ added requirements.txt
This commit is contained in:
nico 2018-12-22 04:46:57 +01:00
parent 92efbd421e
commit af5f1e494d
4 changed files with 195 additions and 36 deletions

124
.gitignore vendored Normal file
View File

@ -0,0 +1,124 @@
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
### Python Patch ###
.venv/
.idea/
### project specific ###
blacklist.txt
.etag

31
README.md Normal file
View File

@ -0,0 +1,31 @@
## Blacklist import script
### ejabberd config
To use this script properly, a separate `yml` file is necessary, as the script will overwrite the file. To further
protect the config the `allow_only` sections defines only `acl` rules.
```yaml
"/etc/ejabberd/blacklist.yml":
allow_only:
- acl
```
### script configuration
The script is meant to be used in an automatic fashion.
Arguments:
- -dr , --dry-run : perform a dry run. `blacklist.txt` and `.etag` are written but no yaml file is overwritten.
- -o , --outfile filepath : set path to output file
The dry-run argument will output the file path, if set, in addition to the contents of the yaml file which would have be produced.
### script workflow
1. check if `.etag` file is present
2. HEAD request
2.1 requests etag and `.etag` are equal
2.1.1 use local `blacklist.txt` file
2.2 requests etag and `.etag` are _not_ equal
2.2.1 request new `blacklist.txt`
2.2.2 save new `.etag` and `blacklist.txt` file
3. process `blacklist.txt` and parse output file

73
main.py
View File

@ -2,27 +2,28 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# workflow # workflow
# start options main.py --ejabberd/prosody --dry-run --outfile file # start options main.py --dry-run --outfile file
import requests import requests
import sys
import os import os
import sys
import argparse import argparse
import yaml import yaml
from ruamel.yaml import YAML, scalarstring
class BlacklistImporter: class BlacklistImporter:
def __init__(self, args): def __init__(self, args):
self.server = args.software
self.outfile = args.outfile self.outfile = args.outfile
self.dryrun = args.dryrun self.dryrun = args.dryrun
self.url = "https://raw.githubusercontent.com/JabberSPAM/blacklist/master/blacklist.txt" self.url = "https://raw.githubusercontent.com/JabberSPAM/blacklist/master/blacklist.txt"
self.blacklist = None self.blacklist = None
self.change = False
def request(self): def request(self):
# check if etag header is present if not set local_etag to "" # check if etag header is present if not set local_etag to ""
if os.path.isfile(".etag"): if os.path.isfile(".etag"):
with open(".etag") as file: with open(".etag", "r") as file:
local_etag = file.read() local_etag = file.read()
else: else:
local_etag = "" local_etag = ""
@ -32,24 +33,22 @@ class BlacklistImporter:
head = s.head(self.url) head = s.head(self.url)
etag = head.headers['etag'] etag = head.headers['etag']
# compare etag with local_etag if they match up no request is made # if file is present
if local_etag == etag: if os.path.isfile("blacklist.txt"):
with open("blacklist.txt", "r") as file: # if etags match up or if a connection is not possible fall back to local cache
self.blacklist = file.readline() if local_etag == etag or head.status_code != 200:
with open("blacklist.txt", "r", encoding="utf-8") as file:
# if the connection is not possible use cached xml if present self.blacklist = file.readline()
elif os.path.isfile("blacklist.txt") and head.status_code != 200:
with open("blacklist.txt", "r") as file:
self.blacklist = file.readline()
# in any other case request a new file # in any other case request a new file
else: else:
r = s.get(self.url) r = s.get(self.url)
r.encoding = 'utf-8' r.encoding = 'utf-8'
local_etag = head.headers['etag'] local_etag = head.headers['etag']
self.blacklist = r.content.decode()
with open("blacklist.txt", "w") as file: with open("blacklist.txt", "w") as file:
file.write(r.content.decode()) file.write(self.blacklist)
with open('.etag', 'w') as string: with open('.etag', 'w') as string:
string.write(local_etag) string.write(local_etag)
@ -60,23 +59,23 @@ class BlacklistImporter:
if self.dryrun: if self.dryrun:
# only output the selected software and outfile # only output the selected software and outfile
print("server software selected: %s" % self.server)
print("outfile selected: %s" % self.outfile) print("outfile selected: %s" % self.outfile)
if self.server == "ejabberd": # select ejabberd processing
# select ejabberd processing self.process()
self.ejabberd()
elif self.server == "prosody": # reload config if changes have been applied
# select prosody processing if self.change:
self.prosody() os.system("ejabberdctl reload_config")
else:
# in any other case exit
sys.exit(3)
def ejabberd(self): def process(self):
# check if file was altered # check if file was altered
local_file = yaml.load(open(self.outfile, "r")) local_file = None
try:
if os.path.isfile(self.outfile):
local_file = yaml.load(open(self.outfile, "r", encoding="utf-8"))
except TypeError:
pass
remote_file = { remote_file = {
"acl": { "acl": {
@ -87,26 +86,28 @@ class BlacklistImporter:
} }
for entry in self.blacklist.split(): for entry in self.blacklist.split():
entry = scalarstring.DoubleQuotedScalarString(entry)
remote_file["acl"]["spamblacklist"]["server"].append(entry) remote_file["acl"]["spamblacklist"]["server"].append(entry)
yml = YAML()
yml.indent(offset=2)
yml.default_flow_style = False
if self.dryrun: if self.dryrun:
print(yaml.dump(remote_file)) # if dryrun true print expected content
yml.dump(remote_file, sys.stdout)
elif local_file != remote_file: elif local_file != remote_file:
yaml.dump(remote_file, open(self.outfile, "w")) self.change = True
# only if the local_file and remote_file are different write new file
def prosody(self): yml.dump(remote_file, open(self.outfile, "w"))
pass
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-e', '--ejabberd', help='set server software to ejabberd', action='store_const', dest='software',
const="ejabberd", default=None)
parser.add_argument('-p', '--prosody', help='set server software to prosody', action='store_const', dest='software',
const="prosody", default=None)
parser.add_argument('-o', '--outfile', help='set path to output file', dest='outfile', default=None) parser.add_argument('-o', '--outfile', help='set path to output file', dest='outfile', default=None)
parser.add_argument('--dry-run', help='perform only a dry run', action='store_true', dest='dryrun', default=False) parser.add_argument('-dr', '--dry-run', help='perform a dry run', action='store_true', dest='dryrun', default=False)
args = parser.parse_args() args = parser.parse_args()
# run
BlacklistImporter(args).main() BlacklistImporter(args).main()

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
PyYAML>=3.1.3
ruamel.yaml>=0.15.80
requests>=2.21.0