Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,11 @@ import websockets
|
|
3 |
import sqlite3
|
4 |
import secrets
|
5 |
|
6 |
-
# SQLite database
|
7 |
-
conn = sqlite3.connect('
|
8 |
cursor = conn.cursor()
|
9 |
-
|
10 |
-
# Create Pairing table if not exists
|
11 |
cursor.execute('''
|
12 |
-
CREATE TABLE IF NOT EXISTS
|
13 |
key TEXT PRIMARY KEY,
|
14 |
client_address TEXT
|
15 |
)
|
@@ -18,54 +16,58 @@ conn.commit()
|
|
18 |
|
19 |
async def handle_client(websocket, path):
|
20 |
try:
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
if command == "create_pair":
|
25 |
-
# Create a unique key
|
26 |
-
key = secrets.token_hex(16)
|
27 |
-
|
28 |
-
# Save the key and client address in the database
|
29 |
-
cursor.execute('INSERT INTO Pairing (key, client_address) VALUES (?, ?)', (key, str(websocket.remote_address)))
|
30 |
-
conn.commit()
|
31 |
-
|
32 |
-
# Inform the client about the generated key
|
33 |
-
await websocket.send(f"Your pair key is: {key}")
|
34 |
-
|
35 |
-
elif command.startswith("connect_to_key"):
|
36 |
-
# Extract the key from the command
|
37 |
-
key_to_connect = command.split(" ")[1]
|
38 |
-
|
39 |
-
# Check if the key exists in the database
|
40 |
-
cursor.execute('SELECT client_address FROM Pairing WHERE key = ?', (key_to_connect,))
|
41 |
-
result = cursor.fetchone()
|
42 |
-
|
43 |
-
if result:
|
44 |
-
other_client_address = result[0]
|
45 |
-
|
46 |
-
# Inform both clients about the successful pairing
|
47 |
-
await websocket.send(f"Connected to {other_client_address}")
|
48 |
-
await websocket.send(f"You are paired with {other_client_address}")
|
49 |
|
50 |
-
# Create a bidirectional bridge
|
51 |
-
asyncio.ensure_future(bridge_clients(websocket, other_client_address))
|
52 |
-
|
53 |
-
except websockets.exceptions.ConnectionClosed:
|
54 |
-
pass # Connection closed, handle appropriately
|
55 |
-
|
56 |
-
async def bridge_clients(source, destination_address):
|
57 |
-
destination = next(c for c in connected_clients if str(c.remote_address) == destination_address)
|
58 |
-
|
59 |
-
try:
|
60 |
while True:
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
except websockets.exceptions.ConnectionClosed:
|
68 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
start_server = websockets.serve(handle_client, "localhost", 8765)
|
71 |
|
@@ -75,5 +77,4 @@ async def main():
|
|
75 |
await asyncio.Event().wait()
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
-
connected_clients = set()
|
79 |
asyncio.run(main())
|
|
|
3 |
import sqlite3
|
4 |
import secrets
|
5 |
|
6 |
+
# Connect to SQLite database (create it if not exists)
|
7 |
+
conn = sqlite3.connect('client_pairs.db')
|
8 |
cursor = conn.cursor()
|
|
|
|
|
9 |
cursor.execute('''
|
10 |
+
CREATE TABLE IF NOT EXISTS pairs (
|
11 |
key TEXT PRIMARY KEY,
|
12 |
client_address TEXT
|
13 |
)
|
|
|
16 |
|
17 |
async def handle_client(websocket, path):
|
18 |
try:
|
19 |
+
# Inform the client about successful connection
|
20 |
+
await websocket.send("Connected to the server")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
while True:
|
23 |
+
command = await websocket.recv()
|
24 |
+
|
25 |
+
if command == "create_pair_key":
|
26 |
+
# Generate a unique key
|
27 |
+
pair_key = secrets.token_urlsafe(8)
|
28 |
+
|
29 |
+
# Save the key and client address in the database
|
30 |
+
cursor.execute("INSERT INTO pairs (key, client_address) VALUES (?, ?)", (pair_key, str(websocket.remote_address)))
|
31 |
+
conn.commit()
|
32 |
+
|
33 |
+
# Send the generated key to the client
|
34 |
+
await websocket.send(f"Pair key created: {pair_key}")
|
35 |
+
|
36 |
+
elif command.startswith("connect_to_key "):
|
37 |
+
key_to_connect = command.split(" ")[1]
|
38 |
+
|
39 |
+
# Check if the key exists in the database
|
40 |
+
cursor.execute("SELECT client_address FROM pairs WHERE key=?", (key_to_connect,))
|
41 |
+
result = cursor.fetchone()
|
42 |
+
|
43 |
+
if result:
|
44 |
+
other_client_address = result[0]
|
45 |
+
|
46 |
+
# Inform both clients about the successful pairing
|
47 |
+
await websocket.send(f"Paired with {other_client_address}")
|
48 |
+
await websocket.send(f"Connected to {websocket.remote_address}")
|
49 |
+
other_client = next((client for client in connected_clients if str(client.remote_address) == other_client_address), None)
|
50 |
+
|
51 |
+
if other_client:
|
52 |
+
await other_client.send(f"Paired with {websocket.remote_address}")
|
53 |
+
await other_client.send(f"Connected to {other_client.remote_address}")
|
54 |
+
else:
|
55 |
+
await websocket.send("Invalid pair key")
|
56 |
+
|
57 |
+
else:
|
58 |
+
# Broadcast messages to all connected clients
|
59 |
+
for client in connected_clients:
|
60 |
+
if client != websocket:
|
61 |
+
await client.send(f"Client says: {command}")
|
62 |
|
63 |
except websockets.exceptions.ConnectionClosed:
|
64 |
+
pass
|
65 |
+
finally:
|
66 |
+
# Remove the client from the set upon disconnection
|
67 |
+
connected_clients.remove(websocket)
|
68 |
+
|
69 |
+
# Set to store connected clients
|
70 |
+
connected_clients = set()
|
71 |
|
72 |
start_server = websockets.serve(handle_client, "localhost", 8765)
|
73 |
|
|
|
77 |
await asyncio.Event().wait()
|
78 |
|
79 |
if __name__ == "__main__":
|
|
|
80 |
asyncio.run(main())
|