-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
172 lines (150 loc) · 5.25 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
from data.var import *
import sqlite3
import hashlib
import getpass
import socket
import threading
class main:
def __init__(self):
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
self.cursor.executescript(dbInit)
self.conn.commit()
self.conn.close()
self.main()
def main(self):
while True:
print("-" * 70)
print("1. Connect")
print("2. Register")
print("3. Add server")
print("4. Delete server")
print("5. Exit")
choice = input("Choice < ")
if choice == "1":
self.connect(input("Server name < "))
elif choice == "2":
self.register()
elif choice == "3":
self.addServer()
elif choice == "4":
self.deleteServer()
elif choice == "5":
break
else:
print("Invalid choice")
pass
def login(self):
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
username = input("Username < ")
password = hashlib.sha256(getpass.getpass("Password < ").encode()).hexdigest()
self.cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password))
user = self.cursor.fetchone()
if user:
print("Login successful")
return user
else:
print("Login failed")
return False
self.conn.close()
pass
def register(self):
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
username = input("Username < ")
password = hashlib.sha256(getpass.getpass("Password < ").encode()).hexdigest()
self.cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
self.conn.commit()
self.conn.close()
print("User created")
pass
def addServer(self):
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
while True:
name = input("Server name < ")
ip = input("Server IP < ")
port = input("Server port < ")
if name == "" or ip == "":
print("Cannot add server")
print("Server name and IP are required")
return
if port == "":
port = 4007
print("Port set to default 4007")
break
else:
port = int(port)
break
if self.cursor.execute("SELECT * FROM servers WHERE name = ? AND ip = ? AND port = ?", (name, ip, port)).fetchone():
print("Server already exists")
return
self.cursor.execute("INSERT INTO servers (name, ip, port) VALUES (?, ?, ?)", (name, ip, port))
self.conn.commit()
self.conn.close()
pass
def deleteServer(self):
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
name = input("Server name < ")
if not self.cursor.execute("SELECT * FROM servers WHERE name = ?", (name,)).fetchone():
print("Server not found")
return
self.cursor.execute("DELETE FROM servers WHERE name = ?", (name,))
print("Server deleted")
self.conn.commit()
self.conn.close()
pass
def connect(self, serverName):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn = sqlite3.connect(dataBaseFile)
self.cursor = self.conn.cursor()
server = self.cursor.execute("SELECT * FROM servers WHERE name = ?", (serverName,)).fetchone()
if not server:
print("Server not found")
return
ip = server[2]
port = int(server[3])
user = self.login()
if not user:
return
username = user[1]
client_socket.connect((ip, port))
client_socket.send(username.encode())
def receive():
while True:
try:
message = client_socket.recv(1024).decode()
if message:
print()
print(message)
print("<")
except Exception as e:
print("An error occurred.")
print(e)
client_socket.close()
break
def send():
while True:
try:
message = input("New message < ")
if message == "exit":
client_socket.close()
break
full_message = f"{message}"
client_socket.send(full_message.encode())
except Exception as e:
print(e)
client_socket.close()
break
receive_thread = threading.Thread(target=receive)
receive_thread.start()
send_thread = threading.Thread(target=send)
send_thread.start()
receive_thread.join()
send_thread.join()
pass
if __name__ == "__main__":
print(motd)
main()