-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
163 lines (127 loc) · 3.85 KB
/
Copy pathapi.py
File metadata and controls
163 lines (127 loc) · 3.85 KB
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 mysql.connector
import configparser
import os
from flask import request, session
import bcrypt
socketio = None
def register_socketio(sio):
global socketio
socketio = sio
def set_config():
config = configparser.ConfigParser()
conf = "config.ini"
if os.path.isfile(conf):
config.read(conf)
else:
config["DEFAULT"] = {
"host": "localhost",
"user": "",
"password": "",
"database": "chat",
"emailpw": "",
"-------": "------",
"domain": "eddi.cowdie.com/chat/"
}
with open(conf, "w") as f:
config.write(f)
from mysql.connector import pooling
config = configparser.ConfigParser()
conf = "config.ini"
if os.path.isfile(conf):
config.read(conf)
def get_conf(option, fallback=None):
return config["DEFAULT"].get(option, fallback)
db_pool = None
def get_connection():
global db_pool
host = get_conf("host", "127.0.0.1")
if host == "localhost":
host = "127.0.0.1"
if db_pool is None:
try:
db_pool = pooling.MySQLConnectionPool(
pool_name="chat_pool",
pool_size=10,
pool_reset_session=True,
host=host,
user=get_conf("user"),
password=get_conf("password"),
database=get_conf("database")
)
except Exception:
return mysql.connector.connect(
host=host,
user=get_conf("user"),
password=get_conf("password"),
database=get_conf("database")
)
try:
return db_pool.get_connection()
except Exception:
return mysql.connector.connect(
host=host,
user=get_conf("user"),
password=get_conf("password"),
database=get_conf("database")
)
def log(username, name, wert, action):
conn = get_connection()
cursor = conn.cursor()
try:
user_ip = request.remote_addr
except Exception:
user_ip = "GUI"
cursor.execute(
"INSERT INTO log (ip,username, name, wert, action) VALUES (%s,%s, %s, %s, %s)",
(user_ip,username, name, wert, action)
)
conn.commit()
cursor.close()
conn.close()
def sqlq(query,values,type="all"):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(query,values)
if type == "all":
ans = cursor.fetchall()
elif type == "one":
ans = cursor.fetchone()
elif type == "count":
ans = cursor.rowcount
elif type == "lastid":
conn.commit()
ans = cursor.lastrowid
elif type == "none":
conn.commit()
ans = ""
cursor.close()
conn.close()
return ans
def auth(username=None,password=None):
daten = {}
hash = sqlq("SELECT password FROM users WHERE username = %s",(username,),"one")
stored_hash = hash[0] if isinstance(hash, tuple) else hash
if bcrypt.checkpw(password.encode('utf-8'),stored_hash.encode('utf-8')):
daten["data"] = 1
daten["code"] = 200
else:
daten["data"] = 0
daten["code"] = 401
return daten
def background():
while True:
socketio.emit("msg", 1)
def send_mail(email,msg):
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
USERNAME = "verifikation.eddi@gmail.com"
DESTINATION = email
APP_PASSWORD = get_conf("emailpw")
msg = MIMEText(msg, "plain")
msg["Subject"] = "verification"
msg["From"] = USERNAME
msg["To"] = DESTINATION
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(USERNAME, APP_PASSWORD)
server.send_message(msg)