Advertisement
ItzFimes

server code

Feb 22nd, 2024
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import socket
  2. import threading
  3. import hashlib
  4. import string
  5. import random
  6. from cryptography.hazmat.primitives import hashes
  7. from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
  8. from cryptography.hazmat.backends import default_backend
  9.  
  10.  
  11. def receive_messages(client_socket, username):
  12.     while True:
  13.         try:
  14.             data = client_socket.recv(1024)
  15.             print(data)
  16.             print(f"{username}: {data}")
  17.             broadcast(f"{username}:", client_socket)
  18.             broadcast(f" {data}", client_socket)
  19.         except ConnectionResetError:
  20.             print(f"[*] {username} disconnected")
  21.             break
  22.  
  23.  
  24. def broadcast(message, sender_socket):
  25.     for client_socket in clients:
  26.         if client_socket != sender_socket:
  27.             client_socket.send(message.encode())
  28.  
  29.  
  30. clients = []  # Define the clients list outside the main() function
  31.  
  32.  
  33. def generate_random_password(length):
  34.     # Define the set of characters to choose from
  35.     characters = string.ascii_letters + string.digits + string.punctuation
  36.  
  37.     # Generate a random password by selecting 'length' number of random characters
  38.     password = ''.join(random.choice(characters) for _ in range(length))
  39.  
  40.     return password
  41.  
  42.  
  43. def generate_key(password):
  44.     # salt = b'salt_'
  45.     # kdf = PBKDF2HMAC(
  46.     #     algorithm=hashes.SHA256(),
  47.     #     length=32,
  48.     #     salt=salt,
  49.     #     iterations=100000,
  50.     #     backend=default_backend()
  51.     # )
  52.     # return kdf.derive(password.encode())
  53.     key = generate_random_password(20) + password + generate_random_password(20)
  54.     print(key)
  55.     key = key.encode()
  56.     key = bytearray(key)
  57.     return key
  58.  
  59.  
  60. def main():
  61.     hostname = socket.gethostname()
  62.     IPAddr = socket.gethostbyname(hostname)
  63.     host = IPAddr
  64.     port = 9988
  65.  
  66.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  67.     server_socket.bind((host, port))
  68.     server_socket.listen(5)
  69.     print(f"[*] Listening on {host}:{port}")
  70.  
  71.     while True:
  72.         client_socket, client_address = server_socket.accept()
  73.         print(f"[*] Accepted connection from {client_address}")
  74.         clients.append(client_socket)
  75.  
  76.         username = client_socket.recv(1024).decode()
  77.         if len(clients) == 1:
  78.             client_socket.send("first_device".encode())
  79.             password = client_socket.recv(1024).decode()  # Receive the encryption password from the client
  80.             password = str(password)
  81.             key = generate_key(password)
  82.             print(type(key))
  83.             print(key)
  84.             print(key)
  85.             client_socket.send(key)
  86.         else:
  87.             client_socket.send("not_first_device".encode())
  88.             try:
  89.                 key = key.encode()
  90.             except:
  91.                 pass
  92.             client_socket.send(key)  # Send the encryption key to the client
  93.  
  94.         client_handler = threading.Thread(target=receive_messages, args=(client_socket, username))
  95.         client_handler.start()
  96.  
  97.  
  98. if __name__ == "__main__":
  99.     main()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement