2023-11-17 00:06:02 +11:00
|
|
|
from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory
|
|
|
|
import os
|
|
|
|
import dotenv
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import db
|
|
|
|
import varo_auth
|
|
|
|
import account
|
|
|
|
import render
|
2023-11-17 00:21:57 +11:00
|
|
|
import re
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
|
|
|
|
# Database connection
|
|
|
|
dbargs = {
|
|
|
|
'host':os.getenv('DB_HOST'),
|
|
|
|
'user':os.getenv('DB_USER'),
|
|
|
|
'password':os.getenv('DB_PASSWORD'),
|
|
|
|
'database':os.getenv('DB_NAME')
|
|
|
|
}
|
2023-11-17 11:05:06 +11:00
|
|
|
ADMIN_DOMAIN = os.getenv('ADMIN_DOMAIN')
|
|
|
|
if ADMIN_DOMAIN == None:
|
|
|
|
ADMIN_DOMAIN = "nathan.woodburn"
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
#Assets routes
|
|
|
|
@app.route('/assets/<path:path>')
|
|
|
|
def assets(path):
|
|
|
|
return send_from_directory('templates/assets', path)
|
|
|
|
|
|
|
|
|
|
|
|
def error(message):
|
|
|
|
if 'linkr' not in request.cookies:
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
domain = account.get_user(token)
|
|
|
|
if domain == False:
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
avatar=account.get_avatar(domain)
|
|
|
|
host = request.host
|
|
|
|
links = db.get_users_links(domain)
|
2023-11-17 00:27:38 +11:00
|
|
|
link_count = len(links)
|
2023-11-17 00:06:02 +11:00
|
|
|
if links == False:
|
2023-11-17 00:27:38 +11:00
|
|
|
links = "<h1>No links created yet</h1>"
|
|
|
|
else:
|
|
|
|
links = render.links(links,host)
|
|
|
|
return render_template('dash.html',domain=domain,avatar=avatar,host=host,links=links,link_count=link_count,message=message)
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2023-12-21 15:51:00 +11:00
|
|
|
# Check if domain is subdomain
|
2023-12-21 15:54:35 +11:00
|
|
|
if request.host.count('.') > 0:
|
2023-12-21 15:51:00 +11:00
|
|
|
# See if link exists
|
|
|
|
link = db.get_link(request.host.split('.')[0])
|
|
|
|
if link != False:
|
|
|
|
return redirect(link)
|
|
|
|
|
2023-11-17 00:06:02 +11:00
|
|
|
if 'linkr' in request.cookies:
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
domain = account.get_user(token)
|
|
|
|
if domain != False:
|
|
|
|
return redirect('/dash')
|
|
|
|
else:
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
links = db.get_link_count()
|
|
|
|
accounts = db.get_account_count()
|
|
|
|
|
|
|
|
return render_template('index.html',links=links,accounts=accounts)
|
|
|
|
|
|
|
|
@app.route('/dash')
|
|
|
|
def edit():
|
|
|
|
if 'linkr' not in request.cookies:
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
domain = account.get_user(token)
|
|
|
|
if domain == False:
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
avatar=account.get_avatar(domain)
|
|
|
|
host = request.host
|
2023-11-17 11:17:00 +11:00
|
|
|
admin=False
|
2023-11-17 11:05:06 +11:00
|
|
|
if domain.lower() == ADMIN_DOMAIN:
|
|
|
|
links = db.get_all_links()
|
2023-11-17 11:17:00 +11:00
|
|
|
admin=True
|
2023-11-17 11:05:06 +11:00
|
|
|
else:
|
|
|
|
links = db.get_users_links(domain)
|
2023-11-17 00:06:02 +11:00
|
|
|
link_count = len(links)
|
|
|
|
if links == False:
|
|
|
|
links = "<h1>No links created yet</h1>"
|
|
|
|
else:
|
2023-11-17 11:17:00 +11:00
|
|
|
links = render.links(links,host,admin)
|
2023-11-17 00:06:02 +11:00
|
|
|
return render_template('dash.html',domain=domain,avatar=avatar,host=host,links=links,link_count=link_count)
|
|
|
|
|
|
|
|
@app.route('/dash', methods=['POST'])
|
|
|
|
def add_link():
|
|
|
|
if 'linkr' not in request.cookies:
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
domain = account.get_user(token)
|
|
|
|
if domain == False:
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
link=request.form['link']
|
2023-11-17 00:21:57 +11:00
|
|
|
url=request.form['url'].lower()
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
# Verify link is valid
|
|
|
|
if not (url.startswith('http://') or url.startswith('https://')):
|
|
|
|
url = 'https://' + url
|
|
|
|
|
2023-11-17 11:45:13 +11:00
|
|
|
regexmatch = re.match(r"^https?://([a-z0-9]+(-[a-z0-9]+)*\.)*([a-z0-9]+(-[a-z0-9]+)*)(/([a-z0-9.#])+(-([a-z0-9.])+)?)*$", url)
|
2023-11-17 00:21:57 +11:00
|
|
|
if not regexmatch:
|
2023-11-18 21:53:13 +11:00
|
|
|
return error('Invalid destination link')
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
if len(link) > 32:
|
|
|
|
return error('Link too long')
|
2023-11-18 21:53:13 +11:00
|
|
|
if len(link) < 5 and domain.lower() != ADMIN_DOMAIN:
|
2023-11-17 00:06:02 +11:00
|
|
|
return error('Link too short')
|
|
|
|
|
2023-11-17 10:55:00 +11:00
|
|
|
regexmatch = re.match(r"^[a-zA-Z0-9]+$", link)
|
|
|
|
if not regexmatch:
|
2023-11-18 21:53:13 +11:00
|
|
|
return error('Invalid link name')
|
2023-11-17 10:55:00 +11:00
|
|
|
|
2023-11-17 00:06:02 +11:00
|
|
|
# Verify link is not taken
|
|
|
|
if db.get_link(link) != False:
|
|
|
|
return error('Link already taken')
|
2023-12-13 14:51:10 +11:00
|
|
|
if link in ['dash','login','logout','404','assets','admin']:
|
2023-11-18 21:54:04 +11:00
|
|
|
return error('Link already taken')
|
2023-11-17 00:06:02 +11:00
|
|
|
|
|
|
|
# Add link
|
|
|
|
db.add_link(link,url,domain)
|
|
|
|
return redirect('/dash')
|
|
|
|
|
|
|
|
@app.route('/delete/<path:path>')
|
|
|
|
def delete(path):
|
|
|
|
if 'linkr' not in request.cookies:
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
domain = account.get_user(token)
|
|
|
|
if domain == False:
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
2023-11-17 12:08:13 +11:00
|
|
|
if domain.lower() != ADMIN_DOMAIN:
|
|
|
|
db.delete_link(path,domain)
|
|
|
|
else:
|
2023-11-17 12:32:54 +11:00
|
|
|
db.delete_link_admin(path)
|
2023-11-17 00:06:02 +11:00
|
|
|
return redirect('/dash')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
if 'linkr' not in request.cookies:
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
token = request.cookies['linkr']
|
|
|
|
account.remove_user(token)
|
|
|
|
|
|
|
|
# Remove cookie
|
|
|
|
resp = make_response(redirect('/'))
|
|
|
|
resp.set_cookie('linkr', '', expires=0)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login():
|
|
|
|
auth = varo_auth.flask_login(request)
|
|
|
|
if auth == False:
|
|
|
|
return redirect('/')
|
|
|
|
resp = make_response(redirect('/dash'))
|
|
|
|
# Gen cookie
|
|
|
|
auth_cookie = account.generate_token()
|
|
|
|
account.add_user(auth, auth_cookie)
|
|
|
|
resp.set_cookie('linkr', auth_cookie)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def catch_all(path):
|
|
|
|
link = db.get_link(path)
|
|
|
|
if link != False:
|
|
|
|
return redirect(link)
|
|
|
|
return redirect('/404') # 404 catch all
|
|
|
|
|
|
|
|
# 404 catch all
|
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.route('/404')
|
|
|
|
def not_found(e=None):
|
|
|
|
return render_template('404.html'), 404
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
db.check_tables()
|
|
|
|
app.run(debug=False, port=5000, host='0.0.0.0')
|