/
log serv.py
99 lines (86 loc) · 3.6 KB
/
log serv.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
"""Login sys"""
from cryptography.fernet import Fernet
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtNetwork as qtn
from json import loads
import sys
import json
import mysql.connector
class log_serv(qtw.QPushButton):
"""log_serv"""
s = 670
ip = 'xxx.xxx.xxx.xxx'
def __init__(self):
"""Initialize"""
super().__init__()
self.server = qtn.QTcpServer()
self.sock = qtn.QTcpSocket(self.server)
# if self.server.listen(qtn.QHostAddress(self.ip), self.s):
if self.server.listen(qtn.QHostAddress.LocalHost, self.s):
print(
f'DATA SERVER:\n\tAddress: {self.server.serverAddress().toString()}\n\tPort: {str(self.server.serverPort())}')
else:
print('ERROR!!!!')
exit()
self.server.newConnection.connect(self.__session)
f = open('key.bin', 'rb')
self.key = f.read()
f.close()
self.f = Fernet(self.key)
del self.key
# set your properties
self.db = mysql.connector.connect(
host = 'localhost',
user = 'user',
passwd = 'password',
database = 'login_data',
auth_plugin = 'mysql_native_password'
)
self.mycursor = self.db.cursor()
def __session(self):
"""Session"""
clientconn = self.server.nextPendingConnection()
clientconn.waitForReadyRead()
data = str(clientconn.read(4096), 'utf8')
data = loads(data)
new = data['type']
name = data['nick'].lower()
passwd = data['password']
# print(new + " " + name + " " + passwd)
if name.isalnum():
self.mycursor.execute("SELECT * FROM users WHERE nickname = %s LIMIT 1 ", (name, ))
temp_db_data = self.mycursor.fetchone()
if new == 'new':
if temp_db_data == None:
self.mycursor.execute("INSERT INTO users (nickname, password) VALUES (%s, %s)", (name, self.f.encrypt(bytes(passwd, 'utf8'))))
self.db.commit()
print(name + " - Succesfully signed up")
clientconn.write(bytes("Succesfully signed up", 'utf8'))
clientconn.waitForBytesWritten()
else:
print(name + " - This user already exists.")
clientconn.write(bytes("This user already exists.", 'utf8'))
clientconn.waitForBytesWritten()
elif new == 'log':
if temp_db_data:
localpass = str(self.f.decrypt(bytes(temp_db_data[2], 'utf8')), 'utf8')
if localpass == passwd:
print(name + " - Succesfully logged in!")
clientconn.write(bytes("Succesfully logged in!", 'utf8'))
clientconn.waitForBytesWritten()
else:
clientconn.write(bytes("Login doesn't match with the password", 'utf8'))
clientconn.waitForBytesWritten()
print(f"Login '{name}' doesn't match with the password")
else:
clientconn.write(bytes("Check your login!", 'utf8'))
clientconn.waitForBytesWritten()
print(name + " - user not found!")
else:
print(name + " - name contains not allowed marks!")
clientconn.write(bytes('name contains not allowed marks!', 'utf8'))
clientconn.waitForBytesWritten()
if __name__ == "__main__":
app = qtw.QApplication([])
s = log_serv()
sys.exit(app.exec_())