forked from Feluk6174/doxa_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
176 lines (145 loc) · 7.14 KB
/
database.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
import mysql.connector
import random
import threading
import time
import log as log_lib
import conf
def log(*message, logger:log_lib.Logger = None):
if not logger == None:
logger.log(*message)
else:
print(message)
def is_safe(*args, logger = None):
invalid_chars = ["\\", "\'", "\"", "\n", "\t", "\r", "\0", "%", "\b", ";", "=", "\u259e"]
arguments = ""
for argument in args:
arguments += argument if not argument == None else ""
for char in invalid_chars:
if char in arguments:
log(f"[ERROR] Invalid char {char}", logger)
return False
return True
class Database():
def __init__(self, logger:log_lib.Logger=None):
self.connect()
self.queue = []
self.return_response = []
self.logger = logger
self.file = open("sql.txt", "a")
thread = threading.Thread(target=self.proces_queue)
thread.start()
def connect(self):
self.connection = mysql.connector.connect(
host = conf.database("host"),
user = conf.database("user"),
password = conf.database("password"),
database = conf.database("database")
)
def stop(self):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("s", "stop", queue_id))
def querry(self, querry:str):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("q", querry, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def execute(self, sql:str):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("e", sql, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def import_db(self, commands:list):
queue_id = random.randint(1000000000, 9999999999)
self.queue.append(("n", commands, queue_id))
while True:
for response in self.return_response:
if response[0] == queue_id:
self.return_response.remove((queue_id, response[1]))
return response[1]
def proces_queue(self):
log("[STARTED QUEUE PROCESOR]", logger=self.logger)
while True:
if len(self.queue) > 0:
if self.queue[0][0] == "q":
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.return_response.append((self.queue[0][2], cursor.fetchall()))
except mysql.connector.Error as e:
self.connect()
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.return_response.append((self.queue[0][2], cursor.fetchall()))
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "e":
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.connection.commit()
self.return_response.append((self.queue[0][2], None))
self.file.write(self.queue[0][1]+"\n")
except mysql.connector.Error as e:
self.connect()
try:
cursor = self.connection.cursor()
cursor.execute(self.queue[0][1])
self.connection.commit()
self.return_response.append((self.queue[0][2], None))
self.file.write(self.queue[0][1]+"\n")
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "n":
try:
cursor = self.connection.cursor()
for command in self.queue[0][1]:
try:
cursor.execute(command)
except mysql.connector.Error as e:
pass
self.connection.commit()
self.return_response.append((self.queue[0][2], "SUCCESS"))
except mysql.connector.Error as e:
try:
self.connect()
cursor = self.connection.cursor()
for command in self.queue[0][1]:
try:
cursor.execute(command)
except mysql.connector.Error as e:
pass
self.connection.commit()
self.return_response.append((self.queue[0][2], "SUCCESS"))
except mysql.connector.Error as e:
log("[ERROR]", e, logger = self.logger)
self.connect()
self.return_response.append((self.queue[0][2], "ERROR"))
elif self.queue[0][0] == "s":
self.queue.pop(0)
break
self.queue.pop(0)
time.sleep(0.1)
def create(self):
cursor = self.connection.cursor()
cursor.execute("DROP TABLE IF EXISTS posts;")
cursor.execute("DROP TABLE IF EXISTS users;")
cursor.execute("CREATE TABLE users(user_name VARCHAR(16) COLLATE ascii_general_ci NOT NULL UNIQUE PRIMARY KEY, public_key VARCHAR(392) COLLATE ascii_general_ci NOT NULL UNIQUE, key_file VARCHAR(1764) COLLATE ascii_general_ci NOT NULL UNIQUE, time_created INT NOT NULL, profile_picture VARCHAR(64) COLLATE ascii_general_ci NOT NULL, info VARCHAR(200));")
cursor.execute("CREATE TABLE posts(id VARCHAR(23) NOT NULL PRIMARY KEY, user_id VARCHAR(16) COLLATE ascii_general_ci NOT NULL, post VARCHAR(255) NOT NULL, background_color VARCHAR(1) NOT NULL, time_posted INT NOT NULL, signature VARCHAR(344), FOREIGN KEY (user_id) REFERENCES users (user_name));")
cursor.execute("DROP TABLE IF EXISTS ips;")
cursor.execute("CREATE TABLE ips(ip VARCHAR(21) NOT NULL PRIMARY KEY, time_connected INT NOT NULL);")
self.connection.commit()
if __name__ == "__main__":
db = Database()
db.create()
db.stop()