-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (147 loc) · 4.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import secrets
import uuid
import logging
import string
import io
from flask import Flask, jsonify, send_file, Response
from flask_cors import CORS
from captcha.image import ImageCaptcha
from markdown import markdown
from deta import Deta
def random_text(length: int = 4) -> str:
alphabets = string.ascii_letters + string.digits
text = "".join(secrets.choice(alphabets) for i in range(int(length)))
return text
def response(content: str) -> Response:
resp = content if isinstance(content, Response) else Response(content)
resp.headers.add("Access-Control-Allow-Origin", "*")
resp.headers.add("Referrer-Policy", "no-referrer")
return resp
image_captcha = ImageCaptcha(400, 150, font_sizes=(105, 125, 140))
deta = Deta()
data = deta.Base("data")
drive = deta.Drive("CAPTCHA")
def emergency_call():
drive.delete_many(drive.list()["names"])
logging.getLogger("werkzeug").setLevel(logging.ERROR)
app = Flask("HDBVM")
CORS(app)
@app.route("/")
def home():
with open("index.md", "r") as readme_file:
css = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css"
js = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"
head = f"""
<head>
<link rel="icon" href="/icon">
<!--link rel="stylesheet" href="{css}"-->
<!--script src="{js}"></script-->
</head>
"""
return response(head + markdown(
readme_file.read(), extensions=["fenced_code"]
))
@app.route("/api")
def api():
with open("index.html", "r") as home_file:
return response(home_file.read())
@app.route("/api/js")
def api_js():
with open("hdbvm.js", "r") as js_file:
return response(js_file.read())
@app.route("/api/new")
@app.route("/api/new/length/<length>")
@app.route("/api/new/value/<captcha_value>")
def api_new(length: int = 4, captcha_value: str = None):
key = uuid.uuid4().hex
value = captcha_value or random_text(length if int(length) > 0 else 4)
image_data = image_captcha.generate(value)
data.put({"value": value}, key, expire_in=900)
drive.put(f"{key}.png", image_data)
return response(jsonify({
"id": key
}))
@app.route("/api/img")
@app.route("/api/img/id/<captcha_id>")
@app.route("/api/img/length/<length>")
@app.route("/api/img/value/<captcha_value>")
def api_img(captcha_id: str = None, length: int = 4, captcha_value: str = None):
key = captcha_id or uuid.uuid4().hex
value = captcha_value or random_text(length if int(length) > 0 else 4)
image_data = None
if captcha_id:
captcha_file = drive.get(f"{captcha_id}.png")
if captcha_file:
image_data = io.BytesIO(captcha_file.read())
else:
image_data = image_captcha.generate(value)
else:
image_data = image_captcha.generate(value)
return send_file(
image_data,
mimetype="image/png",
as_attachment=False,
download_name=f"{key}.png"
)
@app.route("/api/check/<captcha_id>/<input_data>")
def api_check(captcha_id: str = None, input_data: str = None):
captcha_data = data.get(captcha_id if captcha_id else "invalid data")
if not captcha_data:
return jsonify({
"id": captcha_id,
"exists": False,
"result": False
})
if captcha_data["value"] == input_data:
drive.delete(f"{captcha_id}.png")
data.delete(captcha_id)
return response(jsonify({
"id": captcha_id,
"exists": True,
"result": captcha_data["value"] == input_data
}))
@app.route("/api/renew/img/<captcha_id>")
def api_renew_img(captcha_id: str = None):
captcha_data = data.get(captcha_id if captcha_id else "invalid data")
if not captcha_data:
return jsonify({
"id": captcha_id,
"result": False
})
image_data = image_captcha.generate(captcha_data["value"])
drive.put(f"{captcha_id}.png", image_data)
return response(jsonify({
"id": captcha_id,
"result": True
}))
@app.route("/api/renew/<captcha_id>")
def api_renew(captcha_id: str = None):
captcha_data = data.get(captcha_id if captcha_id else "invalid data")
if not captcha_data:
return jsonify({
"id": captcha_id,
"result": False
})
value = random_text(len(captcha_data["value"]))
data.put({"value": value}, captcha_id, expire_in=900)
image_data = image_captcha.generate(value)
drive.put(f"{captcha_id}.png", image_data)
return response(jsonify({
"id": captcha_id,
"result": True
}))
@app.route("/icon")
def icon():
return response(send_file(
"icon.png",
mimetype="image/png",
as_attachment=False,
download_name="icon.png"
))
@app.errorhandler(404)
@app.errorhandler(403)
@app.errorhandler(500)
def error(*_, **__):
return ":/\nYou messed up\nSee <a href='/'>docs</a>."
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)