blck

ephemeral pastebin/url shortener
git clone https://git.parazyd.org/blck
Log | Files | Refs | README | LICENSE

commit f2b0200b30168f05341eec71975e0bfbb5a07424
parent 7735f76b4850a6ab87ab0ac16ef4a3d25b8c01ef
Author: parazyd <parazyd@dyne.org>
Date:   Tue,  3 Oct 2017 10:51:21 +0200

support switching pastebin/url shortener

Diffstat:
MREADME.md | 10+++++++---
Mblck.py | 46++++++++++++++++++++++++++++------------------
Mstatic/style.css | 1+
Mtemplates/index.html | 14++++++++++++--
4 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md @@ -1,7 +1,8 @@ -blck.cf -======= +blck +==== -a one-click url shortener. +an ephemeral pastebin/url-shortener. you can only retrieve the paste +once, and afterwards it's deleted from the server. installation @@ -10,6 +11,9 @@ installation get `python-flask` and execute `blck.py`. by default it starts on `localhost:5000`, but you can configure it at the bottom of the script. +to choose whether the app runs as a pastebin or url shortener, just +change the `pastebin` variable inside `blck.py` + nginx ----- diff --git a/blck.py b/blck.py @@ -1,15 +1,17 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # copyleft (c) 2017 - parazyd # see LICENSE file for details import flask import random -import re import os import string + app = flask.Flask(__name__) +pastebin = False + @app.route("/", methods=['GET', 'POST']) def main(): @@ -17,7 +19,7 @@ def main(): url = flask.request.form['url'] return s(url) except: - return flask.render_template("index.html") + return flask.render_template("index.html", pastebin=pastebin) @app.route("/<urlshort>") @@ -27,33 +29,40 @@ def u(urlshort): realurl = f.readline() os.remove('uris/' + urlshort) except: - return "could not find url\n" + return "could not find paste\n" - if "curl" not in flask.request.headers.get('User-Agent'): + cliagents = ['curl', 'Wget'] + if flask.request.headers.get('User-Agent').split('/')[0] not in cliagents \ + and not pastebin: return flask.redirect(realurl.rstrip('\n'), code=301) else: return realurl def s(url): - ## taken from django - regex = re.compile( - r'^(?:http|ftp)s?://' # http:// or https:// - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' - r'localhost|' #localhost... - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip - r'(?::\d+)?' # optional port - r'(?:/?|[/?]\S+)$', re.IGNORECASE) - - if not url or len(url) > 1024 or not regex.match(url): - return "invalid url\n" + if not pastebin: + # taken from django + import re + regex = re.compile( + r'^(?:http|ftp)s?://' # http:// or https:// + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' + r'localhost|' # localhost... + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + r'(?::\d+)?' # optional port + r'(?:/?|[/?]\S+)$', re.IGNORECASE) + + if len(url) > 1024 or not regex.match(url): + return "invalid url\n" + + if not url: + return "invalid paste\n" urlshort = genid() try: with open('uris/' + urlshort, 'w') as f: f.write(url + '\n') except: - return "could not save url\n" + return "could not save paste\n" if flask.request.headers.get('X-Forwarded-Proto') == 'https': return flask.request.url_root.replace('http://', 'https://') \ @@ -66,4 +75,5 @@ def genid(size=4, chars=string.ascii_uppercase + string.ascii_lowercase): return ''.join(random.choice(chars) for i in range(size)) -app.run(host="127.0.0.1", port=5000) +if __name__ == '__main__': + app.run(host="127.0.0.1", port=5000) diff --git a/static/style.css b/static/style.css @@ -9,6 +9,7 @@ body { .container { font-size: 300%; } .inputbox , .button { font-size: 70%; } +.textbox { height: 360px; width: 580px; } a, a:visited, a:active { color: #179c3f; text-decoration: none; } a:hover { text-decoration: underline; } diff --git a/templates/index.html b/templates/index.html @@ -7,11 +7,21 @@ </head> <body> <div class="container"> - <h1>blck.cf</h1> - <p>Create a one-click expiring link</p> + <h1>blck</h1> + <p>Create an ephemeral + {% if not pastebin %} + short url + {% else %} + paste + {% endif %} + </p> <div class="form"> <form method="post" action="/"> + {% if not pastebin %} <input type="url" name="url" id="url" class="inputbox" placeholder="http://blck.cf" required autofocus></input> + {% else %} + <textarea name="url" class="textbox" required autofocus>Enter text here</textarea><br> + {% endif %} <input type="submit" class="button"></input> </form> </div>