-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (142 loc) · 4.78 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
import time, random, json, threading
import pymysql
from flask import Flask, request, redirect
with open("config.json", "r") as fb:
config = json.loads(fb.read())
DB = config["DB"]
KEYS = config["KEYS"]
LISTEN = config["LISTEN"]
DEBUG = config["DEBUG"]
app = Flask("Ghink Short Link Service")
db = pymysql.connect(
host=DB["host"],
user=DB["user"],
password=DB["password"],
database=DB["database"]
)
field_map = {
'A': 0, 'a': 1, 'B': 2, 'b': 3,
'C': 4, 'c': 5, 'D': 6, 'd': 7,
'1': 8, 'E': 9, 'e': 10, 'F': 11,
'f': 12, 'G': 13, 'g': 14, 'H': 15,
'h': 16, '2': 17, 'I': 18, 'i': 19,
'J': 20, 'j': 21, 'K': 22, 'k': 23,
'L': 24, 'l': 25, '3': 26, 'M': 27,
'm': 28, 'N': 29, 'n': 30, 'O': 31,
'o': 32, 'P': 33, 'p': 34, '4': 35,
'Q': 36, 'q': 37, 'R': 38, 'r': 39,
'S': 40, 's': 41, 'T': 42, 't': 43,
'5': 44, 'U': 45, 'u': 46, 'V': 47,
'v': 48, 'W': 49, 'w': 50, 'X': 51,
'x': 52, '6': 53, 'Y': 54, 'y': 55,
'Z': 56, 'z': 57, '7': 58, '8': 59,
'9': 60, '0': 61}
@app.route("/", methods=["GET"])
def index():
with open("index.html", "r", encoding="utf-8") as fb:
return fb.read()
@app.route("/<string:link_id>", methods=["GET"])
def route(link_id: str):
global db
for char in link_id:
if char not in tuple(field_map.keys()):
return redirect("https://www.ghink.net")
link_id_converted = 0
for i in range(len(link_id)):
link_id_converted += field_map[link_id[::-1][i]] * 62 ** i
try:
db.ping()
except pymysql.err.InterfaceError:
db = pymysql.connect(
host=DB["host"],
user=DB["user"],
password=DB["password"],
database=DB["database"]
)
cursor = db.cursor()
cursor.execute('SELECT link, validity FROM links WHERE id=%s', link_id_converted)
db.commit()
link = cursor.fetchone()
if link is None or link[0] is None:
with open("404.html", "r", encoding="utf-8") as fb:
return fb.read(), 404
if link[1] is not None and link[1] < time.time():
remove_thread = threading.Thread(target=remove_link, args=(link_id_converted,))
remove_thread.start()
with open("404.html", "r", encoding="utf-8") as fb:
return fb.read(), 404
return redirect(link[0])
@app.route("/", methods=["POST"])
def add():
global db
key = request.form.get("key")
link = request.form.get("link")
validity = request.form.get("validity")
# Judge whether fields are empty
if key == "" or link == "":
return json.dumps({"ok": False, "message": "bad field(s)", "id": ""})
# No access
if key not in KEYS:
return json.dumps({"ok": False, "message": "forbidden", "id": ""})
# Check validity
if validity:
if validity.isdecimal() and int(validity) > time.time():
validity = int(validity)
else:
return json.dumps({"ok": False, "message": "bad field(s)", "id": ""})
else:
validity = None
# Random
while True:
link_id_random = ''.join(random.sample(tuple(field_map.keys()), 6))
link_id_converted = 0
for i in range(len(link_id_random)):
link_id_converted += field_map[link_id_random[::-1][i]] * 62 ** i
# Get Cursor
try:
db.ping()
except pymysql.err.InterfaceError:
db = pymysql.connect(
host=DB["host"],
user=DB["user"],
password=DB["password"],
database=DB["database"]
)
cursor = db.cursor()
cursor.execute('SELECT link FROM links WHERE id=%s', link_id_converted)
db.commit()
link_selected = cursor.fetchone()
if link_selected is None:
break
# Insert
cursor.execute("INSERT INTO `links` VALUES (%s, %s, %s)", [link_id_converted, link, validity])
db.commit()
return json.dumps({"ok": True, "message": "successful", "id": link_id_random})
@app.route("/", methods=["PATCH"])
def reload():
global config, DB, KEYS, LISTEN, DEBUG
with open("config.json", "r") as fb:
config = json.loads(fb.read())
DB = config["DB"]
KEYS = config["KEYS"]
LISTEN = config["LISTEN"]
DEBUG = config["DEBUG"]
return json.dumps({"ok": True, "message": "successful"})
def remove_link(id):
global db
# Get Cursor
try:
db.ping()
except pymysql.err.InterfaceError:
db = pymysql.connect(
host=DB["host"],
user=DB["user"],
password=DB["password"],
database=DB["database"]
)
cursor = db.cursor()
cursor.execute('DELETE FROM links WHERE id=%s', id)
db.commit()
if __name__ == "__main__":
app.run(LISTEN[0], LISTEN[1], DEBUG)
db.close()