diff --git a/README.md b/README.md index 5c51b5d..494f8b6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ # lantalk-public Public repo for my lan chatting app +Features: +- Custom PORT +- Custom username +- Custom username color +- Random username color +- Chat logging +- Linux and windows clients + +# Running the server on linux +Download the repo. +## Run lantalk server +Run ```python server_lantalk.py``` + +# Running the server on windows +Go to releases tab on repo and download server. + +# Running client on linux +Download the repo +## Run lantalk client +Run ```python client_lantalk.py``` + +# Running the server on windows +Go to releases tab on repo and download server. \ No newline at end of file diff --git a/client_lantalk.py b/client_lantalk.py new file mode 100644 index 0000000..a0c3519 --- /dev/null +++ b/client_lantalk.py @@ -0,0 +1,101 @@ +import socket +import random +from threading import Thread +from datetime import datetime +from colorama import Fore, init, Back +#var +username="no" +class var: + join_msg=" joined" + debug_usr_name="Client_Test" + # null is disabled + # enabled is enabled + debug_enabled="null" + seperator="" + serverid="Server" + leave="left" +init() + + +colors = [Fore.BLUE, Fore.CYAN, Fore.GREEN, Fore.LIGHTBLACK_EX, + Fore.LIGHTBLUE_EX, Fore.LIGHTCYAN_EX, Fore.LIGHTGREEN_EX, + Fore.LIGHTMAGENTA_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX, + Fore.LIGHTYELLOW_EX, Fore.MAGENTA, Fore.RED, Fore.WHITE, Fore.YELLOW +] + + +client_color = random.choice(colors) + +host_ip=input("Enter host IP: ") +host_port=input("Enter host PORT: ") +serverHost = host_ip +serverPort = int(host_port) +separator_token = var.seperator # we will use this to separate the client name & message + +# initialize TCP socket +s = socket.socket() +print(f"[*] Connecting to {serverHost}:{serverPort}...") +# connect to the server +s.connect((serverHost, serverPort)) +print("[+] Connected.") + +if var.debug_enabled != "enabled": + username = input("Enter name: ") +elif var.debug_enabled == "enabled": + username=var.debug_usr_name + +print("[*] Logged in as", username) +print("Colors: Blue, Cyan, Green, Lime, Magenta, Red, Yellow, White, Anything else = Random") +colorInputFirst = input("Enter color: ") +color_input = colorInputFirst.lower() +if (color_input == "blue"): + client_color = Fore.BLUE +elif (color_input == "cyan"): + client_color = Fore.CYAN +elif (color_input == "green"): + client_color = Fore.GREEN +elif (color_input == "lime"): + client_color = Fore.LIGHTGREEN_EX +elif (color_input == "magenta"): + client_color = Fore.MAGENTA +elif (color_input == "red"): + client_color = Fore.RED +elif (color_input == "yellow"): + client_color = Fore.YELLOW +elif (color_input == "white"): + client_color = Fore.WHITE +elif (color_input != "white"): + client_color = random.choice(colors) +usr_join_msg = f"{username}{var.join_msg}" +date_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') +usr_join_msg = f"{client_color}[{date_now}] {var.serverid}{separator_token}{usr_join_msg}\n" +s.send(usr_join_msg.encode()) + + +def listen_for_messages(): + while True: + message = s.recv(1024).decode() + print("\n" + message) + +# make a thread that listens for messages to this client & print them +t = Thread(target=listen_for_messages) +# make the thread daemon so it ends whenever the main thread ends +t.daemon = True +# start the thread +t.start() + +while True: + # input message we want to send to the server + to_send = input() + # a way to exit the program + if to_send.lower() == 'q': + break + # add the datetime, name & the color of the sender + date_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + to_send = f"{client_color}[{date_now}] {username}{separator_token}{to_send}" + # finally, send the message + s.send(to_send.encode()) + +# close the socket +s.close() + diff --git a/server_lantalk.py b/server_lantalk.py new file mode 100644 index 0000000..8735a2b --- /dev/null +++ b/server_lantalk.py @@ -0,0 +1,73 @@ +import socket +from threading import Thread +import time + +host_ip=input("Enter IP: ") +host_port=input("Enter PORT: ") +serverHost = host_ip +serverPort = int(host_port) +enableChatLogging = "y" # Insert "y" for YES and "n" for NO +separator_token = "" # Do not modify + +f = open("logfile.txt", "a") +f.write("Logfile initialized" + "\n") +f.close() + +# initialize list/set of all connected client's sockets +client_sockets = set() +# create a TCP socket +s = socket.socket() +# make the port as reusable port +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +# bind the socket to the address we specified +s.bind((serverHost, serverPort)) +# listen for upcoming connections +s.listen(5) +print(f"[*] Listening as {serverHost}:{serverPort}") + +def listen_for_client(cs): + while True: + try: + msg = cs.recv(1024).decode() + except Exception as e: + client_sockets.remove(cs) + else: + # if we received a message, replace the + # token with ": " for nice printing + msg = msg.replace(separator_token, ": ") + if (enableChatLogging == "y"): + removalValue = 5 + fixedMsg = msg[removalValue:] + logInput = fixedMsg + "\n" + f = open("logfile.txt", "a") + f.write(logInput) + f.close() + else: + print("Server chat logging is disabled") + # iterate over all connected sockets + for client_socket in client_sockets: + # and send the message + client_socket.send(msg.encode()) + +while True: + # we keep listening for new connections all the time + client_socket, client_address = s.accept() + print(f"[+] {client_address} connected.") + # add the new connected client to connected sockets + client_sockets.add(client_socket) + # start a new thread that listens for each client's messages + t = Thread(target=listen_for_client, args=(client_socket,)) + # make the thread daemon so it ends whenever the main thread ends + t.daemon = True + # start the thread + t.start() + +while True: + time.sleep(2) + print(f"[*] Users lol") + +# close client sockets +for cs in client_sockets: + cs.close() +# close server socket +s.close() \ No newline at end of file