Change how the client works

This commit is contained in:
Fierelier 2023-11-08 11:43:24 +01:00
parent 45ed3cbd03
commit 1bc3b15b60

View File

@ -16,17 +16,20 @@ import socket
import time import time
def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs)
def defGet(tbl,key,defv):
if not key in tbl: return defv
return tbl[key]
bufferSize = 4096 # max buffer size in bytes for receiving data, lower values shouldn't reduce the delay bufferSize = int(defGet(os.environ,"fstream_net_buffer","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 bufferSizeStdin = int(defGet(os.environ,"fstream_stdin_buffer","128")) # min buffer size for buffer, lower values DO reduce delay but raise CPU usage
timeout = 15 # timeout in seconds timeout = float(defGet(os.environ,"fstream_net_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
useSSL = False useSSL = False
sslIgnoreCert = False sslIgnoreCert = False
if "fstream_ssl" in os.environ and os.environ["fstream_ssl"] == "1": useSSL = True if defGet(os.environ,"fstream_ssl","0") == "1": useSSL = True
if "fstream_ssl_ignoreCert" in os.environ and os.environ["fstream_ssl_ignoreCert"] == "1": sslIgnoreCert = True if defGet(os.environ,"fstream_ssl_ignoreCert","0") == "1": sslIgnoreCert = True
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) # Make unbuffered stdout
def listToCommand(lst): def listToCommand(lst):
cmd = "" cmd = ""
@ -37,32 +40,6 @@ def listToCommand(lst):
return cmd[:-1] 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): def recv(conn,l):
start = time.process_time() start = time.process_time()
timeo = conn.gettimeout() timeo = conn.gettimeout()
@ -100,6 +77,12 @@ def main():
serverAddr = stringToAddressTuple(sys.argv[1]) serverAddr = stringToAddressTuple(sys.argv[1])
if useSSL: import ssl if useSSL: import ssl
serverEnv = [sys.argv[2]]
for var in os.environ:
if not var.startswith("fstream_arg_"): continue
serverEnv.append(var.replace("fstream_arg_","",1)+ "=" +os.environ[var])
serverEnv = listToCommand(serverEnv)
eprint("Connecting to server...") eprint("Connecting to server...")
connection.settimeout(timeout) connection.settimeout(timeout)
connection.connect(serverAddr) connection.connect(serverAddr)
@ -122,16 +105,30 @@ def main():
eprint("Sending payload...") eprint("Sending payload...")
connection.sendall("\n\n".encode("ascii")) connection.sendall("\n\n".encode("ascii"))
sendResponse(connection,sys.argv[2].encode("utf-8")) sendResponse(connection,serverEnv.encode("utf-8"))
cmd = commandToList(sys.argv[2]) if sys.argv[2] == "token":
args = {} try:
for arg in cmd[1:]: token = b""
argSplit = arg.split("=",1) eprint("Receiving token...")
args[argSplit[0]] = argSplit[1] while True:
cmd = cmd[0] data = connection.recv(bufferSize)
if data != b"":
token += data
continue
if token == b"":
eprint("Connection closed: failure")
os.exit(1)
else:
eprint("Connection closed: success")
unbufferedStdout.write(token)
return
except:
connection.close()
raise
if cmd == "watch": if sys.argv[2] == "watch":
try: try:
eprint("Receiving data...") eprint("Receiving data...")
while True: while True:
@ -144,11 +141,13 @@ def main():
connection.close() connection.close()
raise raise
if cmd == "broadcast": if sys.argv[2] == "broadcast":
try: try:
eprint("Sending data...") eprint("Sending data...")
while True: while True:
data = sys.stdin.buffer.read(bufferSizeStdin) data = sys.stdin.buffer.read(bufferSizeStdin)
if data == b"":
return
connection.sendall(data) connection.sendall(data)
except: except:
connection.close() connection.close()