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
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
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
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 = int(defGet(os.environ,"fstream_stdin_buffer","128")) # min buffer size for buffer, lower values DO reduce delay but raise CPU usage
timeout = float(defGet(os.environ,"fstream_net_timeout","15")) # timeout in seconds
useSSL = False
sslIgnoreCert = False
if "fstream_ssl" in os.environ and os.environ["fstream_ssl"] == "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","0") == "1": useSSL = 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):
cmd = ""
@ -37,32 +40,6 @@ def listToCommand(lst):
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()
@ -100,6 +77,12 @@ def main():
serverAddr = stringToAddressTuple(sys.argv[1])
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...")
connection.settimeout(timeout)
connection.connect(serverAddr)
@ -122,16 +105,30 @@ def main():
eprint("Sending payload...")
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])
args = {}
for arg in cmd[1:]:
argSplit = arg.split("=",1)
args[argSplit[0]] = argSplit[1]
cmd = cmd[0]
if sys.argv[2] == "token":
try:
token = b""
eprint("Receiving token...")
while True:
data = connection.recv(bufferSize)
if data != b"":
token += data
continue
if cmd == "watch":
if token == b"":
eprint("Connection closed: failure")
os.exit(1)
else:
eprint("Connection closed: success")
unbufferedStdout.write(token)
return
except:
connection.close()
raise
if sys.argv[2] == "watch":
try:
eprint("Receiving data...")
while True:
@ -144,11 +141,13 @@ def main():
connection.close()
raise
if cmd == "broadcast":
if sys.argv[2] == "broadcast":
try:
eprint("Sending data...")
while True:
data = sys.stdin.buffer.read(bufferSizeStdin)
if data == b"":
return
connection.sendall(data)
except:
connection.close()