This script is basically a proxy service; except it doesn't require an active client connection. In fact it expects the client connection to connect before, and disconnect after every transaction. Once running the proxy will buffer all incoming bytes, and will dump those bytes to the client upon request. Before I go any further and begin working on the IRC client intended to be used with this, I am posting here to persuade any feedback regarding the project in its current state from readers here:
soxxy.py
#! /usr/bin/python3
from socket import socket
from threading import Thread as thread
from sys import getdefaultencoding as enc
from sys import byteorder
from sys import argv
enc = enc()
CNCT, SEND, RECV, DISC, STAT = 0, 1, 2, 3, 4
class sox:
def recv_t(self, s):
while self.run:
try: self.buff += s.recv(8192)
except: continue
def process(self, lo):
try: bs = lo[0].recv(8192)
except: return
op, data = bs[0], bs[1:]
if op is CNCT:
data = str(data, enc)
host, port = data.split(":")
self.sock = socket()
self.sock.connect((host, int(port)))
self.run = True
thread(target=self.recv_t, args=(self.sock, )).start()
elif op is SEND: self.sock.send(data)
elif op is RECV: lo[0].send(self.buff); self.buff = bytes()
elif op is DISC: self.run = False; self.sock.close()
elif op is STAT: bytes("error: %s" % (str(self.serr), ), enc)
def server(self, port, host):
ss = socket()
ss.bind((host, int(port)))
while True:
ss.listen(1)
self.process(ss.accept())
def __init__(self, argv):
self.sock = None
self.buff = bytes()
self.serr = False
self.server(argv[1], argv[2])
if __name__ == "__main__":
sox([argv + ["127.0.0.1", ], argv][len(argv) == 3])
IRC
#! /usr/bin/python3
from soxxy import *
lo_host = None
lo_port = None
remote_host = None
remote_port = None
def send(bs):
s = socket()
s.connect((lo_host, int(lo_port)))
s.send(bs)
x = s.recv(8192)
s.close()
return x
if __name__ == "__main__":
lo_host, lo_port, remote_host, remote_port = argv[1:]
send(bytes([CNCT, ] + [ord(c) for c in remote_host + ":" + remote_port]))
from time import sleep
sleep(8)
print(str(send(bytes([RECV, ])), enc))
send(bytes([DISC, ]))
Test case
$ cat test.sh
./soxxy.py 8000 & sleep 2 && \
./irc 127.0.0.1 8000 irc.gnu.org 6667
$ sh test.sh
:morgan.freenode.net NOTICE * :*** Looking up your hostname...
:morgan.freenode.net NOTICE * :*** Checking Ident
:morgan.freenode.net NOTICE * :*** Found your hostname
:morgan.freenode.net NOTICE * :*** No Ident response