From 0f669fe371c1e36949e581264fdc2905d9235a10 Mon Sep 17 00:00:00 2001 From: Burathar Date: Wed, 16 Dec 2020 14:58:18 +0100 Subject: [PATCH] Change default retention to 1 day, make reverse proxy config option --- .flaskenv | 1 - .flaskenv_example | 4 ++++ .gitignore | 3 ++- app/forms.py | 4 ++-- app/routes.py | 30 ++++++++++++++++++------------ config.py | 1 + 6 files changed, 27 insertions(+), 16 deletions(-) delete mode 100644 .flaskenv create mode 100644 .flaskenv_example diff --git a/.flaskenv b/.flaskenv deleted file mode 100644 index fcfed3b..0000000 --- a/.flaskenv +++ /dev/null @@ -1 +0,0 @@ -FLASK_APP=linkshortener.py diff --git a/.flaskenv_example b/.flaskenv_example new file mode 100644 index 0000000..2eeb97b --- /dev/null +++ b/.flaskenv_example @@ -0,0 +1,4 @@ +FLASK_APP=linkshortener.py +FLASK_ENV=production +#FLASK_ENV=development +REVERSE_PROXY=1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index fa1b5d1..db43a84 100644 --- a/.gitignore +++ b/.gitignore @@ -141,4 +141,5 @@ cython_debug/ app.db .vscode/ -logs/ \ No newline at end of file +logs/ +.flaskenv \ No newline at end of file diff --git a/app/forms.py b/app/forms.py index 96a0f4b..1b958a6 100644 --- a/app/forms.py +++ b/app/forms.py @@ -4,6 +4,6 @@ from wtforms.validators import DataRequired, NumberRange, Required class UrlForm(FlaskForm): url = StringField('Url', validators=[DataRequired()]) - retention = IntegerField('Retention', default=5, validators = [NumberRange(min=0, max=100)]) - retention_type = SelectField('Retention', choices = [ 'Minute', 'Hour', 'Day', 'Time'], default=2, validators = [Required()]) + retention = IntegerField('Retention', default=1, validators = [NumberRange(min=0, max=100)]) + retention_type = SelectField('Retention', choices = [(1, 'Minute'), (2, 'Hour'), (3, 'Day'), (4, 'Time')], default=3, validators = [Required()]) submit = SubmitField('Shorten Url') diff --git a/app/routes.py b/app/routes.py index d0f7bdc..df8c39a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -21,8 +21,8 @@ def index(): if hash is None: return redirect(url_for('index')) - death = calcDeath(form.retention.data, form.retention_type.data) - view_counter = None if form.retention_type.data != "Time" else form.retention.data + death = calcDeath(form.retention.data, int(form.retention_type.data)) + view_counter = None if form.retention_type.data != 4 else form.retention.data if death is None and view_counter is None: app.logger.warning("Neither death nor view_counter was recieved for url") flash('Please specify a retention time') @@ -32,7 +32,7 @@ def index(): db.session.add(url) db.session.commit() death = None if url.death is None else url.death.strftime('%Y-%m-%d %H:%M:%S') - app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}") + app.logger.info(f"{getClientIp()} created hash '{url.hash}' for '{url.url}'. Death: {death}, View Counter: {url.view_counter}") link = url_for("resolve_hash", hash=hash, _external = True) flash(Markup(f'Your url is shortend to {link}')) return redirect(url_for('index')) @@ -48,14 +48,20 @@ def getHash(): flash('Failed generating a unique hash. Please try again.') return None +def getClientIp(): + if app.config['REVERSE_PROXY'] == 0: + return request.remote_addr + if app.config['REVERSE_PROXY'] == 1: + return request.environ['HTTP_X_FORWARDED_FOR'] + def calcDeath(retention, retention_type): - if retention_type == 'Minute': + if retention_type == 1: return datetime.now() + timedelta(minutes=retention) - if retention_type == 'Hour': + if retention_type == 2: return datetime.now() + timedelta(hours=retention) - if retention_type == 'Day': + if retention_type == 3: return datetime.now() + timedelta(days=retention) - if retention_type == 'Time': + if retention_type == 4: return None app.logger.error(f"Retention_type out of range: '{retention_type}'") @@ -66,11 +72,11 @@ def resolve_hash(hash): return redirect(url_for('index')) url = Url.query.filter_by(hash=hash).first_or_404() - if check_url(url) is None: - if url.view_counter is not None: - url.view_counter -= 1 - db.session.commit() + if check_url(url) is not None: abort(404) + if url.view_counter is not None: + url.view_counter -= 1 + db.session.commit() countermessage = "" if url.view_counter is None else f". View counter was lowered to {url.view_counter}" - app.logger.info(f"{request.environ['HTTP_X_FORWARDED_FOR']} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}") + app.logger.info(f"{getClientIp()} requested hash '{url.hash}' which resolved to '{url.url}'{countermessage}") return redirect(url.url, 301) diff --git a/config.py b/config.py index 733f13e..0d8277c 100644 --- a/config.py +++ b/config.py @@ -14,3 +14,4 @@ class Config(object): MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') ADMINS = ['your-email@example.com'] + REVERSE_PROXY = os.environ.get('REVERSE_PROXY') or 0