-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
78 lines (69 loc) · 2.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, request, render_template
from redis import Redis
import json
import datetime
import jiami
app = Flask(__name__)
redis = Redis()
@app.route('/', methods=['GET'])
def pagePost():
return render_template('post.html')
@app.route('/get', methods=['GET'])
def pageGet():
scKey = request.args.get("sc")
if scKey == None:
scKey = ''
return render_template('get.html', scKey=scKey)
@app.route('/result/get', methods=['POST'])
def pageResultGet():
scKey = request.form['sc']
data = getData(scKey)
return render_template('resultGet.html', data=data)
@app.route('/result/post', methods=['POST'])
def pageResultPost():
text = request.form['text']
expiration = request.form['expiration']
if expiration == '1h':
exTime = 3600
elif expiration == '1d':
exTime = 86400
elif expiration == '1w':
exTime = 604800
else:
exTime = 1592000
if expiration == '1t':
isOneTime = True
else:
isOneTime = False
scKey = postData(text, exTime, isOneTime)
return render_template('resultPost.html', scKey=scKey)
@app.template_filter('formatdatetime')
def format_datetime(ts):
return "{:0>8}".format(str(datetime.timedelta(seconds=ts)))
def postData(text, exTime, isOneTime):
index = jiami.getRandomString(8)
aes = jiami.AES()
eText = aes.encrypt(text)
key = aes.getKey()
data = json.dumps({'text': eText, 'isOneTime': isOneTime, 'readTimes': 0})
redis.set(index, data, ex = exTime)
return index + ':' + key
def getData(token):
try:
index = token.split(':')[0]
key = token.split(':')[1]
data = json.loads(redis.get(index))
ttl = redis.ttl(index)
data['readTimes'] += 1
redis.set(index, json.dumps(data), ex = ttl)
aes = jiami.AES(key)
data['text'] = aes.decrypt(data['text'])
data['status'] = 1
data['ttl'] = ttl
if data['isOneTime']:
redis.delete(index)
except:
data = {'status': 0}
return data
if __name__ == "__main__":
app.run(host='127.0.0.1', port=12700)