1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00
0bin/start.py

139 lines
4.0 KiB
Python
Raw Normal View History

2012-04-24 14:22:59 +04:00
# -*- coding: utf-8 -*-
2012-04-26 23:19:12 +04:00
"""
Main script including controller, rooting, dependancy management, and
server run.
"""
import sys
2012-04-24 14:22:59 +04:00
import os
2012-04-24 22:15:38 +04:00
import hashlib
2012-04-29 02:07:25 +04:00
import thread
import time
import tempfile
import glob
2012-04-29 22:10:04 +04:00
import math
2012-04-24 14:22:59 +04:00
from datetime import datetime, timedelta
2012-04-26 23:19:12 +04:00
from src import settings, setup_path, Paste
2012-04-24 14:22:59 +04:00
2012-04-26 23:19:12 +04:00
setup_path()
2012-04-24 14:22:59 +04:00
try:
from privilege import drop_privileges_permanently, coerce_user, coerce_group
except AttributeError:
pass # privilege does't work on several plateform
2012-04-29 02:07:25 +04:00
2012-04-26 23:19:12 +04:00
from bottle import (Bottle, route, run, abort,
static_file, debug, view, request)
2012-04-24 14:22:59 +04:00
2012-04-24 22:15:38 +04:00
app = Bottle()
2012-04-29 22:10:04 +04:00
import settings
2012-04-24 22:15:38 +04:00
@app.route('/')
2012-04-24 14:22:59 +04:00
@view('home')
def index():
2012-04-29 22:10:04 +04:00
max_size_kb = int(math.ceil(settings.MAX_SIZE/1024.0))
return {'max_size': settings.MAX_SIZE, 'max_size_kb': max_size_kb}
2012-04-24 14:22:59 +04:00
2012-04-24 22:15:38 +04:00
@app.route('/paste/create', method='POST')
def create_paste():
try:
content = unicode(request.forms.get('content', ''), 'utf8')
except UnicodeDecodeError:
return {'status': 'error',
'message': u"Encoding error: the paste couldn't be saved."}
2012-04-24 22:15:38 +04:00
2012-04-30 16:33:27 +04:00
if '{"iv":' not in content: # reject silently non encrypted content
return ''
2012-04-24 22:15:38 +04:00
if content:
2012-05-01 17:20:10 +04:00
# check size of the paste. if more than settings return error without saving paste.
# prevent from unusual use of the system.
# need to be improved
if len(content) < settings.MAX_SIZE:
expiration = request.forms.get('expiration', u'burn_after_reading')
paste = Paste(expiration=expiration, content=content)
paste.save()
return {'status': 'ok',
'paste': paste.uuid}
2012-04-24 22:15:38 +04:00
return {'status': 'error',
'message': u"Serveur error: the paste couldn't be saved. Please try later."}
2012-04-24 22:15:38 +04:00
2012-04-26 13:56:13 +04:00
@app.route('/paste/:paste_id')
@view('paste')
2012-04-26 13:38:55 +04:00
def display_paste(paste_id):
2012-04-24 22:15:38 +04:00
now = datetime.now()
keep_alive = False
2012-04-26 13:38:55 +04:00
try:
paste = Paste.load(paste_id)
# Delete the paste if it expired:
if 'burn_after_reading' in str(paste.expiration):
# burn_after_reading contains the paste creation date
# if this read appends 10 seconds after the creation date
# we don't delete the paste because it means it's the redirection
# to the paste that happens during the paste creation
try:
keep_alive = paste.expiration.split('#')[1]
keep_alive = datetime.strptime(keep_alive,'%Y-%m-%d %H:%M:%S.%f')
keep_alive = now < keep_alive + timedelta(seconds=10)
except IndexError:
keep_alive = False
if not keep_alive:
paste.delete()
elif paste.expiration < now:
paste.delete()
raise ValueError()
2012-04-26 13:38:55 +04:00
except (TypeError, ValueError):
abort(404, u"This paste doesn't exist or has expired")
2012-04-26 13:38:55 +04:00
return {'paste': paste, 'keep_alive': keep_alive}
2012-04-24 22:15:38 +04:00
2012-04-26 13:56:13 +04:00
@app.route('/static/<filename:path>')
2012-04-24 14:22:59 +04:00
def server_static(filename):
2012-04-24 22:15:38 +04:00
return static_file(filename, root=settings.STATIC_FILES_ROOT)
2012-04-24 14:22:59 +04:00
if __name__ == "__main__":
2012-04-29 02:07:25 +04:00
def drop_privileges():
time.sleep(5)
if settings.USER:
settings.GROUP = settings.GROUP or settings.USER
try:
user = coerce_user(settings.USER)
group = coerce_group(settings.GROUP)
lock_files = glob.glob(os.path.join(tempfile.gettempdir(),
'bottle.*.lock'))
for lock_file in lock_files:
os.chown(lock_file, user, group)
drop_privileges_permanently(settings.USER, settings.GROUP, ())
except Exception:
print "Failed to drop privileges. Running with current user."
thread.start_new_thread(drop_privileges, ())
2012-04-24 22:15:38 +04:00
if settings.DEBUG:
2012-04-24 14:22:59 +04:00
debug(True)
2012-04-29 02:07:25 +04:00
run(app, host=settings.DEV_HOST, port=settings.DEV_PORT,
reloader=True, server="cherrypy")
2012-04-24 14:22:59 +04:00
else:
2012-04-29 02:07:25 +04:00
run(app, host=settings.PROD_HOST,
port=settings.PROD_PORT, server="cherrypy")