-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
193 lines (166 loc) · 6.62 KB
/
server.py
File metadata and controls
193 lines (166 loc) · 6.62 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import socket
import threading
import sqlite3
HOST = "0.0.0.0"
PORT = 12346
BUFF_SIZE = 1024
USER_DB = "users.db"
clients = {}
user_sessions = {}
def initialize_database():
# Initialize the SQLite database
conn = sqlite3.connect(USER_DB)
cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL)""")
conn.commit()
conn.close()
def authenticate_user(username, password):
# Check if username and password match in the database
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM users WHERE username = ? AND password = ?", (username, password)
)
result = cursor.fetchone()
conn.close()
return result is not None
def register_user(username, password):
# Register a new user in the database
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
try:
cursor.execute(
"INSERT INTO users (username, password) VALUES (?, ?)", (username, password)
)
conn.commit()
success = True
except sqlite3.IntegrityError:
success = False
conn.close()
return success
def notify_disconnection(username):
# Notify other users that a user has disconnected
for user, session in user_sessions.items():
if user != username:
try:
session["connection"].send(
f"{username} has disconnected.".encode("utf-8")
)
except Exception:
pass
def handle_client(conn, addr):
conn.send(b'Welcome! Type "login" to log in or "register" to create an account: ')
while True:
try:
choice = conn.recv(BUFF_SIZE).decode("utf-8").strip()
if choice == "login":
conn.send(b"Username: ")
username = conn.recv(BUFF_SIZE).decode("utf-8").strip()
conn.send(b"Password: ")
password = conn.recv(BUFF_SIZE).decode("utf-8").strip()
if authenticate_user(username, password):
conn.send(
b'Login successful. You are idle. Type a username to communicate or "exit" to logout: '
)
user_sessions[username] = {
"ip": addr[0],
"port": addr[1],
"status": "online",
"connection": conn,
}
clients[conn] = username
break
else:
conn.send(b"Invalid credentials. Try again: ")
elif choice == "register":
conn.send(b"Choose a username: ")
username = conn.recv(BUFF_SIZE).decode("utf-8").strip()
conn.send(b"Choose a password: ")
password = conn.recv(BUFF_SIZE).decode("utf-8").strip()
if register_user(username, password):
conn.send(b'Registration successful! Type "login" to log in: ')
else:
conn.send(b"Username already exists. Try again: ")
else:
conn.send(b'Invalid option. Type "login" or "register": ')
except ConnectionResetError:
break
username = clients.get(conn)
while True:
try:
message = conn.recv(BUFF_SIZE).decode("utf-8").strip()
if message == "exit":
conn.send(b"Goodbye!")
notify_disconnection(username)
break
elif (
message in user_sessions
and user_sessions[message]["status"] == "online"
):
conn.send(
f"Request sent to {message}. Waiting for response...".encode(
"utf-8"
)
)
target_conn = user_sessions[message]["connection"]
target_conn.send(
f'{username} wants to communicate. Type "yes" to accept or "no" to decline: '.encode(
"utf-8"
)
)
response = target_conn.recv(BUFF_SIZE).decode("utf-8").strip()
if response == "yes":
conn.send(b'Call started. Type "exit" to end the call.')
target_conn.send(b'Call started. Type "exit" to end the call.')
def relay_data(source, target):
while True:
try:
data = source.recv(BUFF_SIZE)
if (
data.decode("utf-8", errors="ignore").strip()
== "exit"
):
source.send(b"Call ended.")
target.send(b"Call ended.")
break
target.sendall(data)
except Exception:
break
# Create threads for bidirectional communication
thread1 = threading.Thread(
target=relay_data, args=(conn, target_conn)
)
thread2 = threading.Thread(
target=relay_data, args=(target_conn, conn)
)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
else:
conn.send(f"{message} declined the request.".encode("utf-8"))
else:
conn.send(b"No such user online. Try again: ")
except ConnectionResetError:
break
# Cleanup after disconnection
if username in user_sessions:
user_sessions.pop(username)
if conn in clients:
clients.pop(conn)
notify_disconnection(username)
conn.close()
def main():
initialize_database()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print(f"Server listening on {HOST}:{PORT}")
while True:
conn, addr = server_socket.accept()
threading.Thread(target=handle_client, args=(conn, addr)).start()
server_socket.close()
if __name__ == "__main__":
main()