From 2a221f180ebf6f86e3709401804108fcda65184f Mon Sep 17 00:00:00 2001 From: marvin-borner@live.com Date: Wed, 11 Apr 2018 18:36:44 +0200 Subject: Started implementing chat encryption --- assets/php/Chatserver/bin/WebChatServer.php | 18 +++++ assets/php/Chatserver/bin/server.csr | 18 +++++ assets/php/Chatserver/src/ChatProcessor.php | 118 ++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 assets/php/Chatserver/bin/WebChatServer.php create mode 100644 assets/php/Chatserver/bin/server.csr create mode 100644 assets/php/Chatserver/src/ChatProcessor.php (limited to 'assets/php/Chatserver') diff --git a/assets/php/Chatserver/bin/WebChatServer.php b/assets/php/Chatserver/bin/WebChatServer.php new file mode 100644 index 0000000..15f573b --- /dev/null +++ b/assets/php/Chatserver/bin/WebChatServer.php @@ -0,0 +1,18 @@ +run(); \ No newline at end of file diff --git a/assets/php/Chatserver/bin/server.csr b/assets/php/Chatserver/bin/server.csr new file mode 100644 index 0000000..8523b82 --- /dev/null +++ b/assets/php/Chatserver/bin/server.csr @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICzTCCAbUCAQAwgYcxCzAJBgNVBAYTAkRFMRMwEQYDVQQIDApTb21lLVN0YXRl +MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxHjAcBgNVBAMMFW1h +cnZpbmJvcm5lci5kZG5zcy5kZTEgMB4GCSqGSIb3DQEJARYRbWFydmluQGJvcm5l +cnMuZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDjNPmJb5tz9xp+ +h9Jt7Go+VQ31oEu/FINxxW9jamPvvAzcBs82YKoLuOb1NHJX0wWLDAoCwXRn/vpP +pp2OPicl4ACFKOUoKLUj4XXpsFMNsfV5qT4z0rte8P3RYcwbVWlscNekVMbgA6DF +PXvOqZWHgJEZe8UoVP0Pj9J7/xcjY+VpfBhHAEimOipBVxijFAtnOzJ2o1M58xjZ +3XH2BmeSVbjmhSgIbB50zcATKcVudSvaj3hnGmnfwhPRpO2UOvBrrQubwRoTGyvV +TsW6dGGVY/qOJIyBcddayaZJraozBjDwp43c9S44DSotEgeLtvAyceQ27WDl37bl +eEEyMJpvAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEATQ8QnGSJmoudHnYuG8t2 +SHQOxp4R3RcsgXRtXSm3tFlIAvvvixqiqVBfppxK6f6cf4B8tagmXNdblCK8dbWn +9OIBxEh/hrooCtzydXcfvaJaFFaqPQ5YMyNNTtwLHd14w2hSSixNcON+YjfVY2ZA +CdlR/pEDv/xSCABaTNecvSUYBHWsbmo7pMSKBZ/wI0UcgJHbB4PhCjuLTdB79RTp +RJsMzA9ISZd6dLFxkmnOgmq2+sX3xTRK5XSrNfkE9Veou26BHeCwSBA25JVkatqC +DaSuNw3a9SY28Tk1GPjhrKaX8lI40aViWtnuwW36jD3G2GMyXbo4K8hST0wI6gHi +PQ== +-----END CERTIFICATE REQUEST----- diff --git a/assets/php/Chatserver/src/ChatProcessor.php b/assets/php/Chatserver/src/ChatProcessor.php new file mode 100644 index 0000000..da78c9b --- /dev/null +++ b/assets/php/Chatserver/src/ChatProcessor.php @@ -0,0 +1,118 @@ +clients = new \SplObjectStorage; + $this->subscriptions = []; + $this->users = []; + $this->connectedUsersNames = []; + } + + public function onOpen(ConnectionInterface $conn) { + $generator = new Alliteration(); + $this->clients->attach($conn); + $this->users[$conn->resourceId] = $conn; + $this->connectedUsersNames[$conn->resourceId] = $generator->getName(); + } + + /*public function onMessage(ConnectionInterface $from, $msg) { + $numRecv = count($this->clients) - 1; + echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n" + , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'); + + foreach ($this->clients as $client) { + if ($from === $client) { + $client->send("You - " . $msg); + } else { + $client->send("" . $from->resourceId . " - " . $msg); + } + } + } + */ + + public function onMessage(ConnectionInterface $conn, MessageInterface $msg) { + $data = json_decode($msg); + switch ($data->ClientMessageType) { + case "Subscribe": + $this->subscriptions[$conn->resourceId] = $data->Channel; + foreach ($this->subscriptions as $id => $channel) { + if ($this->subscriptions[$conn->resourceId] == $channel) { + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = true; + $MessageObject->ServerMessageType = "GroupJoin"; + $MessageObject->GroupName = $channel; + $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId]; + if ($id === $conn->resourceId) { + $MessageObject->WasHimself = true; + } else { + $MessageObject->WasHimself = false; + } + $MessageJson = json_encode($MessageObject, true); + $this->users[$id]->send($MessageJson); + } + } + break; + case "Message": + if (isset($this->subscriptions[$conn->resourceId])) { + $target = $this->subscriptions[$conn->resourceId]; + foreach ($this->subscriptions as $id => $channel) { + if ($channel == $target) { + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = false; + $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId]; + $MessageObject->Message = htmlspecialchars($data->Message); + if ($id === $conn->resourceId) { + $MessageObject->WasHimself = true; + } else { + $MessageObject->WasHimself = false; + } + $MessageJson = json_encode($MessageObject, true); + $this->users[$id]->send($MessageJson); + } + } + } + } + } + + public function onClose(ConnectionInterface $conn) { + $this->clients->detach($conn); + foreach ($this->clients as $client) { + if (isset($this->subscriptions[$conn->resourceId])) { + $target = $this->subscriptions[$conn->resourceId]; + foreach ($this->subscriptions as $id => $channel) { + if ($channel == $target) { + $MessageObject = new \stdClass(); + $MessageObject->ServerMessage = true; + $MessageObject->ServerMessageType = "UserDisconnect"; + $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId]; + $MessageJson = json_encode($MessageObject, true); + $this->users[$id]->send($MessageJson); + } + } + } + } + unset($this->users[$conn->resourceId]); + unset($this->subscriptions[$conn->resourceId]); + unset($this->connectedUsersNames[$conn->resourceId]); + } + + public function onError(ConnectionInterface $conn, \Exception $e) { + echo "An error has occurred: {$e->getMessage()}\n"; + + $conn->close(); + } +} \ No newline at end of file -- cgit v1.2.3