diff --git a/code/app.py b/code/app.py index ea1bd5a..505ba1e 100644 --- a/code/app.py +++ b/code/app.py @@ -14,23 +14,10 @@ from utils.utils import * from utils.crypt import * -__FLEES_VERSION__ = "20180821.0" +__FLEES_VERSION__ = "20180911.0" app = Flask(__name__) app.config.from_object(__name__) -# Read config from json ! -config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt')) -app.config['PUBLIC_URL'] = config_values['public_url'] -app.config['SITE_NAME'] = config_values['site_name'] -app.config['UPLOAD_FOLDER'] = config_values['data_folder'] -app.config['SHARES_FILE'] = config_values['shares_file'] -app.config['ZIP_FOLDER'] = config_values['zip_folder'] -app.config['MAX_ZIP_SIZE'] = config_values['max_zip_size'] # megabytes -app.config['DATE_FORMAT'] = config_values['date_format'] -app.config['UID'] = config_values['uid'] -app.config['GID'] = config_values['gid'] -app.config['DEBUG'] = config_values['debug'] -app.config['VERSION_FOLDER'] = config_values['version_folder'] -app.config['SESSION_COOKIE_NAME'] = secure_filename(config_values['site_name']) +config_values = read_config(app) if 'notifier' in config_values: if len(config_values['notifier']) > 0: notifier_config = config_values['notifier'].split(":") @@ -52,6 +39,7 @@ def before_request(): g.version = __FLEES_VERSION__ g.site_name = app.config['SITE_NAME'] g.max_zip_size = app.config['MAX_ZIP_SIZE'] + g.logger = app.config['LOGGER'] @app.route("/") @@ -80,13 +68,13 @@ def index(): @app.route('/authenticate/', methods=['GET','POST']) def authenticate(name): if request.method == 'GET': - return render_template('authenticate.html',name=name) + return render_template('authenticate.html', name = name) if request.method == 'POST': user_password = request.form['password'].encode('utf-8') session[name] = password_hash(user_password, app.secret_key) if name + 'Token' in session: del session[name + 'Token'] - return redirect(url_for('list_view',name=name)) + return redirect(url_for('list_view', name = name)) @app.route('/upload//', methods=['POST']) @@ -581,6 +569,10 @@ def script_client(name = None, token = None): @app.route('/script/flip//', methods=['GET']) @app.route('/script/flip', methods=['GET']) def script_flip(name = "", token = ""): + notify({ + 'share': name, + 'operation': 'script_flip' + }) return render_template( "flip", name = name, @@ -870,6 +862,15 @@ def makedirs_rights(path): def notify(msg): + """ Log notifier messages, and run the custom notifier """ + address = request.environ['REMOTE_ADDR'] + if 'HTTP_X_FORWARDED_FOR' in request.environ: + address += ':' + request.environ['HTTP_X_FORWARDED_FOR'] + log_msg = "%s: %s"%( + address, + ', '.join(["%s:%s"%( k, msg[k] ) for k in sorted(msg)]) + ) + g.logger.info(log_msg) if 'notifier' in app.config: msg['environment'] = request.environ app.config['notifier'].notify(msg) diff --git a/code/utils/utils.py b/code/utils/utils.py index 25a1a1e..49bb087 100644 --- a/code/utils/utils.py +++ b/code/utils/utils.py @@ -3,10 +3,16 @@ from datetime import datetime from flask import current_app as app import requests import re +import json try: import magic except ImportError: pass +try: + from werkzeug.utils import secure_filename +except ImportError: + pass + try: from urllib.request import pathname2url from urllib.request import urlparse @@ -14,6 +20,33 @@ except ImportError: from urllib import pathname2url from urlparse import urlparse +class Logger: + def __init__(self, filename): + self.filename = filename + self.init() + + + def info(self, message): + self.write("INFO", str(message)) + + + def init(self): + self.info("Service started") + + + def warning(self, message): + self.write("WARNING", str(message)) + + + def write(self, level, message): + with open(self.filename, 'at') as fp: + fp.write("%s\t%s\t%s\n"%( + datetime.now().isoformat(), + level, + message + )) + fp.flush() + def download_url(url, filename): try: @@ -186,6 +219,28 @@ def path2url(path): return pathname2url(path) +def read_config(app): + # Read config from json + config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt')) + app.config['PUBLIC_URL'] = config_values['public_url'] + app.config['SITE_NAME'] = config_values['site_name'] + app.config['UPLOAD_FOLDER'] = config_values['data_folder'] + app.config['SHARES_FILE'] = config_values['shares_file'] + if 'log_file' in config_values: + app.config['LOG_FILE'] = config_values['log_file'] + else: + app.config['LOG_FILE'] = os.path.join(app.config['UPLOAD_FOLDER'], 'flees.log') + app.config['LOGGER'] = Logger(app.config['LOG_FILE']) + app.config['ZIP_FOLDER'] = config_values['zip_folder'] + app.config['MAX_ZIP_SIZE'] = config_values['max_zip_size'] # megabytes + app.config['DATE_FORMAT'] = config_values['date_format'] + app.config['UID'] = config_values['uid'] + app.config['GID'] = config_values['gid'] + app.config['DEBUG'] = config_values['debug'] + app.config['VERSION_FOLDER'] = config_values['version_folder'] + app.config['SESSION_COOKIE_NAME'] = secure_filename(config_values['site_name']) + return config_values + def safe_name(s): return safe_string(s, "-_") diff --git a/data/config.json.example b/data/config.json.example index 2e39598..6e65c61 100644 --- a/data/config.json.example +++ b/data/config.json.example @@ -19,6 +19,7 @@ "__do_not_edit": "most likely you will not change anything after this line", "data_folder": "data", "shares_file": "data/shares.json", + "log_file": "data/flees.log", "version_folder": "_version", "zip_folder": "data/.zip", "date_format": "%Y-%m-%d %H:%M",