From 616ff1501e544d96e5863b13633d5ec591666612 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Wed, 1 Nov 2023 02:17:39 +0100 Subject: [PATCH] Make client/utils executable + fix line breaks --- client/fstream-util-pipe_to_tcp.py | 0 client/fstream-util-tcp_to_pipe.py | 3 +- client/fstream.py | 286 ++++++++++++++--------------- 3 files changed, 145 insertions(+), 144 deletions(-) mode change 100644 => 100755 client/fstream-util-pipe_to_tcp.py mode change 100644 => 100755 client/fstream-util-tcp_to_pipe.py mode change 100644 => 100755 client/fstream.py diff --git a/client/fstream-util-pipe_to_tcp.py b/client/fstream-util-pipe_to_tcp.py old mode 100644 new mode 100755 diff --git a/client/fstream-util-tcp_to_pipe.py b/client/fstream-util-tcp_to_pipe.py old mode 100644 new mode 100755 index b24fb77..ffe208e --- a/client/fstream-util-tcp_to_pipe.py +++ b/client/fstream-util-tcp_to_pipe.py @@ -20,6 +20,7 @@ sp = pUp(s) # script start import socket bufferSize = 1000 +unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) @@ -47,4 +48,4 @@ while True: while True: data = connection.recv(bufferSize) if data == b"": break - sys.stdout.buffer.write(data) \ No newline at end of file + unbufferedStdout.write(data) diff --git a/client/fstream.py b/client/fstream.py old mode 100644 new mode 100755 index 8863782..fa9ae9f --- a/client/fstream.py +++ b/client/fstream.py @@ -1,143 +1,143 @@ -#!/usr/bin/env python3 -import sys - -oldexcepthook = sys.excepthook -def newexcepthook(type,value,traceback): - oldexcepthook(type,value,traceback) - #input("Press ENTER to quit.") -sys.excepthook = newexcepthook - -import os -p = os.path.join -pUp = os.path.dirname -s = False -if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): - s = os.path.realpath(sys.executable) -else: - s = os.path.realpath(__file__) -sp = pUp(s) - -# script start -import subprocess -import socket -import time - -def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) - -bufferSize = 4096 # max buffer size in bytes for receiving data, lower values shouldn't reduce the delay -bufferSizeStdin = 128 # min buffer size for buffer, lower values DO reduce delay but raise CPU usage -timeout = 15 # timeout in seconds -connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - -unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout - -def listToCommand(lst): - cmd = "" - for arg in lst: - arg = arg.replace("\\","\\\\") - arg = arg.replace(",","\\,") - cmd += arg + "," - - return cmd[:-1] - -def commandToList(cmd): - args = [] - cArg = "" - escape = False - for letter in cmd: - if escape == True: - cArg += letter - escape = False - continue - - if letter == "\\": - escape = True - continue - - if letter == ",": - if cArg == "": continue - args.append(cArg) - cArg = "" - continue - - cArg += letter - - args.append(cArg) - - return args - -def recv(conn,l): - start = time.process_time() - timeo = conn.gettimeout() - bytes = b"" - while l > 0: - b = conn.recv(l) - if b == b"": raise ConnectionResetError - if time.process_time() - start > timeo: raise TimeoutError - bytes += b - l -= len(b) - return bytes - -def getResponse(connection,maxLength = 0): - data = b'' - data = recv(connection,4) - if not data: return False - nul = recv(connection,1) - if not nul: return False - if nul != b"\x00": return False - requestLength = int.from_bytes(data,"big") - if maxLength != 0 and requestLength > maxLength: return False - return recv(connection,requestLength) - -def sendResponse(connection,data): - connection.sendall(len(data).to_bytes(4,"big") + b"\x00" + data) - -def stringToAddressTuple(addr): - rtn = addr.rsplit(":",1) - rtn[1] = int(rtn[1]) - rtn = tuple(rtn) - return rtn - -def main(): - global serverAddr - serverAddr = stringToAddressTuple(sys.argv[1]) - global bufferSize - - eprint("Connecting to server...") - connection.settimeout(timeout) - connection.connect(serverAddr) - eprint("Sending payload...") - sendResponse(connection,sys.argv[2].encode("utf-8")) - - cmd = commandToList(sys.argv[2]) - args = {} - for arg in cmd[1:]: - argSplit = arg.split("=",1) - args[argSplit[0]] = argSplit[1] - cmd = cmd[0] - - if cmd == "watch": - try: - eprint("Receiving data...") - while True: - data = connection.recv(bufferSize) - if data == b"": - eprint("Connection closed.") - return - unbufferedStdout.write(data) - except: - connection.close() - raise - - if cmd == "broadcast": - try: - eprint("Sending data...") - while True: - data = sys.stdin.buffer.read(bufferSizeStdin) - connection.sendall(data) - except: - connection.close() - raise - -if __name__ == '__main__': - main() +#!/usr/bin/env python3 +import sys + +oldexcepthook = sys.excepthook +def newexcepthook(type,value,traceback): + oldexcepthook(type,value,traceback) + #input("Press ENTER to quit.") +sys.excepthook = newexcepthook + +import os +p = os.path.join +pUp = os.path.dirname +s = False +if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): + s = os.path.realpath(sys.executable) +else: + s = os.path.realpath(__file__) +sp = pUp(s) + +# script start +import subprocess +import socket +import time + +def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) + +bufferSize = 4096 # max buffer size in bytes for receiving data, lower values shouldn't reduce the delay +bufferSizeStdin = 128 # min buffer size for buffer, lower values DO reduce delay but raise CPU usage +timeout = 15 # timeout in seconds +connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout + +def listToCommand(lst): + cmd = "" + for arg in lst: + arg = arg.replace("\\","\\\\") + arg = arg.replace(",","\\,") + cmd += arg + "," + + return cmd[:-1] + +def commandToList(cmd): + args = [] + cArg = "" + escape = False + for letter in cmd: + if escape == True: + cArg += letter + escape = False + continue + + if letter == "\\": + escape = True + continue + + if letter == ",": + if cArg == "": continue + args.append(cArg) + cArg = "" + continue + + cArg += letter + + args.append(cArg) + + return args + +def recv(conn,l): + start = time.process_time() + timeo = conn.gettimeout() + bytes = b"" + while l > 0: + b = conn.recv(l) + if b == b"": raise ConnectionResetError + if time.process_time() - start > timeo: raise TimeoutError + bytes += b + l -= len(b) + return bytes + +def getResponse(connection,maxLength = 0): + data = b'' + data = recv(connection,4) + if not data: return False + nul = recv(connection,1) + if not nul: return False + if nul != b"\x00": return False + requestLength = int.from_bytes(data,"big") + if maxLength != 0 and requestLength > maxLength: return False + return recv(connection,requestLength) + +def sendResponse(connection,data): + connection.sendall(len(data).to_bytes(4,"big") + b"\x00" + data) + +def stringToAddressTuple(addr): + rtn = addr.rsplit(":",1) + rtn[1] = int(rtn[1]) + rtn = tuple(rtn) + return rtn + +def main(): + global serverAddr + serverAddr = stringToAddressTuple(sys.argv[1]) + global bufferSize + + eprint("Connecting to server...") + connection.settimeout(timeout) + connection.connect(serverAddr) + eprint("Sending payload...") + sendResponse(connection,sys.argv[2].encode("utf-8")) + + cmd = commandToList(sys.argv[2]) + args = {} + for arg in cmd[1:]: + argSplit = arg.split("=",1) + args[argSplit[0]] = argSplit[1] + cmd = cmd[0] + + if cmd == "watch": + try: + eprint("Receiving data...") + while True: + data = connection.recv(bufferSize) + if data == b"": + eprint("Connection closed.") + return + unbufferedStdout.write(data) + except: + connection.close() + raise + + if cmd == "broadcast": + try: + eprint("Sending data...") + while True: + data = sys.stdin.buffer.read(bufferSizeStdin) + connection.sendall(data) + except: + connection.close() + raise + +if __name__ == '__main__': + main()