From c305781e8eaf4e8b18e938af20b61b4ebd721a56 Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 31 Jan 2024 05:37:06 +0100 Subject: [PATCH] init --- README.md | 22 ++++++++++++++++++++++ netchat.full.py | 35 +++++++++++++++++++++++++++++++++++ netchat.min.py | 20 ++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 README.md create mode 100755 netchat.full.py create mode 100755 netchat.min.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..24385f3 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Netc(h)at +> minimalist webchat where the client is any generic netcat implementation + +I saw someone posting about a program like this one, +I wondered how hard could it be. +Apparently, +too easy, +so I started pushing for the minimun number of characters +with which all initial features keep working. +Hence `net_chat.full.py` & `net\_chat.min.py. + +To run the server: +```Bash +$ python3 net_chat.min.py 12345 +``` + +To join: +```Bash +$ nc 127.0.0.1 12345 +``` + +That, or have more fun using Tor tunneling. diff --git a/netchat.full.py b/netchat.full.py new file mode 100755 index 0000000..81a8e86 --- /dev/null +++ b/netchat.full.py @@ -0,0 +1,35 @@ +#!/bin/python3 +import socket +from sys import argv +from signal import signal, SIGINT + +socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +socket.bind(('localhost', int(argv[1]))) +socket.listen(100) +socket.setblocking(False) + +signal(SIGINT, lambda a, b: (socket.close(), exit(0))) + +print(f"Listening on {argv[1]}...") + +class User: + def __init__(self, i): + self.username = "anon" + str((id(self) % 999 + 1)) + self.socket, _ = i + self.socket.settimeout(1) + +users = [] + +while True: + try: + users.append(User(socket.accept())) + except BlockingIOError: + pass + for i in users: + try: + msg = i.socket.recv(1024) + except TimeoutError: + continue + if msg: + for h in users: + h.socket.send((i.username + ": " + msg.decode()).encode()) diff --git a/netchat.min.py b/netchat.min.py new file mode 100755 index 0000000..b6b29f9 --- /dev/null +++ b/netchat.min.py @@ -0,0 +1,20 @@ +import sys,signal as S +s=__import__("socket").socket() +s.bind(("localhost",int(sys.argv[1]))) +s.listen() +s.setblocking(0) +S.signal(2,lambda a,b:(s.close(),exit(0))) +print(f"Listening on {sys.argv[1]}...") +def U(i): + i[0].settimeout(1) + return ["anon"+str((id("")%999+1)),i[0]] +u=[] +while 1: + try:u+=[(U(s.accept()))] + except OSError:pass + for i in u: + try: + m=i[1].recv(1024) + if m: + for h in u:h[1].send((i[0]+": "+m.decode()).encode()) + except OSError:pass