From 61e09c760223d123448234dfb0144bd6aee191c1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 30 Apr 2020 15:55:40 +0200 Subject: [PATCH] Working handler to handler --- server.conf | 4 +-- source/connection/connection.d | 2 ++ source/handlers/handler.d | 15 ++++++++--- source/handlers/response.d | 6 ++--- source/server/server.d | 9 +------ testing/testSuite.py | 4 +-- testing/type1Handler.py | 47 ++++++++++++++++++++++++++++++++++ testing/type2Handler.py | 37 ++++++++++++++++++++++++++ testing/unixSock.py | 12 ++++----- 9 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 testing/type1Handler.py create mode 100644 testing/type2Handler.py diff --git a/server.conf b/server.conf index ed2c166..62b84cb 100644 --- a/server.conf +++ b/server.conf @@ -9,12 +9,12 @@ "address" : "0.0.0.0" }, "tcp6" : { - "port" : "2225", + "port" : "2224", "address" : "::" } }, "handlers" : { - "availableTypes" : ["type1"], + "availableTypes" : ["type1", "type2"], "typeMap" : { "type1" : {"handlerBinary" : "aBin", "unixDomainSocketPath" : "aSock"}, "type2" : {"handlerBinary" : "bBin", "unixDomainSocketPath" : "bSock"} diff --git a/source/connection/connection.d b/source/connection/connection.d index cfd8d6b..6972b4d 100644 --- a/source/connection/connection.d +++ b/source/connection/connection.d @@ -179,6 +179,8 @@ public final class BesterConnection : Thread try { + + /* TODO: Collect return value */ HandlerResponse handlerResponse = chosenHandler.handleMessage(payloadData); diff --git a/source/handlers/handler.d b/source/handlers/handler.d index 7829412..0e5ab99 100644 --- a/source/handlers/handler.d +++ b/source/handlers/handler.d @@ -7,6 +7,7 @@ import utils.debugging : debugPrint; import handlers.response; import base.net; import connection.message; +import server.server : BesterServer; public final class MessageHandler { @@ -22,12 +23,15 @@ public final class MessageHandler /* The UNIX domain socket path */ public string socketPath; + /* The BesterServer being used */ + public BesterServer server; + public Socket getSocket() { return domainSocket; } - this(string executablePath, string socketPath, string pluginName) + this(BesterServer server, string executablePath, string socketPath, string pluginName) { /* Set the plugin name */ this.pluginName = pluginName; @@ -37,6 +41,9 @@ public final class MessageHandler /* Set the socket path */ this.socketPath = socketPath; + + /* Set the server this handler is associated with */ + this.server = server; } public string getPluginName() @@ -134,7 +141,7 @@ public final class MessageHandler } /* TODO: Implement me */ - public static MessageHandler[] constructHandlers(JSONValue handlerBlock) + public static MessageHandler[] constructHandlers(BesterServer server, JSONValue handlerBlock) { /* List of loaded message handlers */ MessageHandler[] handlers; @@ -158,7 +165,7 @@ public final class MessageHandler string[2] configuration = getConfigurationArray(pluginName, typeMap); debugPrint("Module executable at: \"" ~ configuration[0] ~ "\""); debugPrint("Module socket path at: \"" ~ configuration[1] ~ "\""); - MessageHandler constructedMessageHandler = new MessageHandler(configuration[0], configuration[1], pluginName); + MessageHandler constructedMessageHandler = new MessageHandler(server, configuration[0], configuration[1], pluginName); handlers ~= constructedMessageHandler; debugPrint("Module \"" ~ pluginName ~ "\" loaded"); } @@ -206,6 +213,6 @@ public final class MessageHandler } - return new HandlerResponse(this, response); + return new HandlerResponse(server, this, response); } } \ No newline at end of file diff --git a/source/handlers/response.d b/source/handlers/response.d index 40d4211..e6d2661 100644 --- a/source/handlers/response.d +++ b/source/handlers/response.d @@ -30,10 +30,9 @@ public final class HandlerResponse /* The handler that caused such a response to be illicited */ private MessageHandler handler; - /* The BesterServer being used */ - public static BesterServer server; + private BesterServer server; - this(MessageHandler handler, JSONValue messageResponse) + this(BesterServer server, MessageHandler handler, JSONValue messageResponse) { /* Set the message-handler's response message */ this.messageResponse = messageResponse; @@ -42,6 +41,7 @@ public final class HandlerResponse this.handler = handler; writeln("Heyo ", server); + this.server= server; /* Attempt parsing the message and error checking it */ parse(messageResponse); diff --git a/source/server/server.d b/source/server/server.d index c14a19b..ef398af 100644 --- a/source/server/server.d +++ b/source/server/server.d @@ -72,20 +72,13 @@ public final class BesterServer /* TODO: Bounds check and JSON type check */ debugPrint("Setting up message handlers..."); setupHandlers(config["handlers"]); - - /** - * Set the `server` field in the HandlerResponse class - * to be this server. - */ - HandlerResponse.server = this; - writeln("jdfjfdhhjfh", HandlerResponse.server); } private void setupHandlers(JSONValue handlerBlock) { /* TODO: Implement me */ debugPrint("Constructing message handlers..."); - handlers = MessageHandler.constructHandlers(handlerBlock); + handlers = MessageHandler.constructHandlers(this, handlerBlock); writeln(handlers[0].getPluginName()); } diff --git a/testing/testSuite.py b/testing/testSuite.py index 3281d95..e854bbe 100644 --- a/testing/testSuite.py +++ b/testing/testSuite.py @@ -8,14 +8,14 @@ def sendAs(username): d.connect(("127.0.0.1",2223)) # First do it and authenticate - bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}}) + bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type2"}}) print(len(bys), bys) d.send(len(bys).to_bytes(4, "little")) d.send(bys.encode()) # Loop prompt and sending of message to tbk while True: - bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}}) + bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type2"}}) print(len(bys), bys) d.send(len(bys).to_bytes(4, "little")) d.send(bys.encode()) diff --git a/testing/type1Handler.py b/testing/type1Handler.py new file mode 100644 index 0000000..4b5a577 --- /dev/null +++ b/testing/type1Handler.py @@ -0,0 +1,47 @@ +import socket +import json + +def runTest(): + d=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + d.bind("../aSock") + d.listen() + while True: + print("Waiting for connection to (this) handler...") + s = d.accept()[0] + + size=int.from_bytes(s.recv(4), "little") + receivedBys = json.loads(s.recv(size).decode()) + print(receivedBys) + + #bys = json.dumps({ + # "header" : { + # "status" : "0", + # "command" : {"type" : "sendClients", "data": ["tbk", "skippy"]} + # }, "data" : receivedBys }) + #print(s.send(len(bys).to_bytes(4, "little"))) + #print(s.send(bys.encode())) + + bys = json.dumps({ + "header" : { + "status" : "0", + "command" : {"type" : "sendServers", "data": ["10.1.0.7:2223"]} + }, "data" : receivedBys }) + print(s.send(len(bys).to_bytes(4, "little"))) + print(s.send(bys.encode())) + + + + #bys = json.dumps({ + # "header" : { + # "status" : "0", + # "command" : {"type" : "sendServers", "data": ["10.0.0.4:2223"]} + # }, "data" : receivedBys["bruhMsg"] }) + #print(s.send(len(bys).to_bytes(4, "little"))) + #print(s.send(bys.encode())) + + + print("Connection to (this) handler finished") + + while True: pass + +runTest() \ No newline at end of file diff --git a/testing/type2Handler.py b/testing/type2Handler.py new file mode 100644 index 0000000..eefc958 --- /dev/null +++ b/testing/type2Handler.py @@ -0,0 +1,37 @@ +import socket +import json + +def runTest(): + d=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + d.bind("../bSock") + d.listen() + while True: + print("Waiting for connection to (this) handler...") + s = d.accept()[0] + + size=int.from_bytes(s.recv(4), "little") + receivedBys = json.loads(s.recv(size).decode()) + print(receivedBys) + + bys = json.dumps({ + "header" : { + "status" : "0", + "command" : {"type" : "sendHandler", "data": "type1"} + }, "data" : receivedBys["bruhMsg"] }) + print(s.send(len(bys).to_bytes(4, "little"))) + print(s.send(bys.encode())) + + bys = json.dumps({ + "header" : { + "status" : "0", + "command" : {"type" : "sendServers", "data": ["10.0.0.4:2223"]} + }, "data" : receivedBys["bruhMsg"] }) + #print(s.send(len(bys).to_bytes(4, "little"))) + #print(s.send(bys.encode())) + + + print("Connection to (this) handler finished") + + while True: pass + +runTest() \ No newline at end of file diff --git a/testing/unixSock.py b/testing/unixSock.py index dc97b44..fc0dc84 100644 --- a/testing/unixSock.py +++ b/testing/unixSock.py @@ -17,15 +17,15 @@ def runTest(): "header" : { "status" : "0", "command" : {"type" : "sendClients", "data": ["tbk", "skippy"]} - }, "data" : receivedBys["bruhMsg"] }) + }, "data" : receivedBys }) print(s.send(len(bys).to_bytes(4, "little"))) print(s.send(bys.encode())) - bys = json.dumps({ - "header" : { - "status" : "0", - "command" : {"type" : "sendServers", "data": ["10.0.0.4:2223"]} - }, "data" : receivedBys["bruhMsg"] }) + # bys = json.dumps({ + # "header" : { + # "status" : "0", + # "command" : {"type" : "sendServers", "data": ["10.0.0.4:2223"]} + # }, "data" : receivedBys["bruhMsg"] }) #print(s.send(len(bys).to_bytes(4, "little"))) #print(s.send(bys.encode()))