From 5af921f4cc60e4797e96461eca996aa594fdd7ad Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Sat, 22 Oct 2022 17:16:02 +0200 Subject: [PATCH] Use Header Login Failure Modes Switch to use proxied wss --- utilities/include/constants.h | 2 +- websocket/include/types.h | 7 ++++++ websocket/include/websocket.h | 1 + websocket/websocket.cpp | 46 +++++++++++++++++++++-------------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/utilities/include/constants.h b/utilities/include/constants.h index 4ac0e7d..77ec140 100644 --- a/utilities/include/constants.h +++ b/utilities/include/constants.h @@ -79,6 +79,6 @@ // Must be differentiated due to SxS Install #define FSXSE_SUBVERSION 62615 -#define WEBSOCKET_ADDRESS "wss://ws.hofmannnet.myhome-server.de:8000" +#define WEBSOCKET_ADDRESS "wss://wsecho.hofmannnet.myhome-server.de/ws" #endif \ No newline at end of file diff --git a/websocket/include/types.h b/websocket/include/types.h index 11dc85b..7b5d143 100644 --- a/websocket/include/types.h +++ b/websocket/include/types.h @@ -56,6 +56,13 @@ namespace gaconnector float trueHeading; }; + enum failures { + NONE, + AUTH, + DUPLICATESIM, + OTHER, + }; + #pragma pack(pop) /* restore original alignment from stack */ } // namespace websocket } // namespace gaconnector diff --git a/websocket/include/websocket.h b/websocket/include/websocket.h index 31fa201..62cd444 100644 --- a/websocket/include/websocket.h +++ b/websocket/include/websocket.h @@ -28,6 +28,7 @@ namespace gaconnector { private: bool isLoggedIn = false; + enum failures failureMode = failures::NONE; char lastPath[513] = ""; char lastHash[2 * MD5LEN + 1] = ""; ix::WebSocket *webSocket = nullptr; diff --git a/websocket/websocket.cpp b/websocket/websocket.cpp index cbc46c6..715d2b0 100644 --- a/websocket/websocket.cpp +++ b/websocket/websocket.cpp @@ -19,7 +19,9 @@ namespace gaconnector this->webSocket = new ix::WebSocket(); this->webSocket->setExtraHeaders({ - { "Authentication", "Bearer " + this->token } + {"origin", "GAClient"}, + {"authentication", "Bearer " + this->token}, + {"user", this->user}, }); this->webSocket->enableAutomaticReconnection(); this->webSocket->setUrl(host); @@ -51,29 +53,21 @@ namespace gaconnector for (const auto &it : msg->openInfo.headers) { debug_msg << "\t\t" << it.first << ": " << it.second << std::endl; } - this->toLog(debug_msg.str()); - this->webSocket->send("MASTER:" + user); - this->toLog("Connecting as " + user); + this->toLog("Connected as " + user); this->isLoggedIn = true; + this->failureMode = failures::NONE; } else if (msg->type == ix::WebSocketMessageType::Close) { - if (msg->closeInfo.reason.compare("DUPLICATE") == 0) { - this->webSocket->disableAutomaticReconnection(); + std::stringstream debug_msg; - this->toLog("Disconnected due to beeing a duplicate simualtor"); - this->isLoggedIn = false; - } else { - std::stringstream debug_msg; + debug_msg << "Connection closed" << std::endl; + debug_msg << "\tCode: " << msg->closeInfo.code << std::endl; + debug_msg << "\tReason: " << msg->closeInfo.reason << std::endl; + debug_msg << "\tRemote: " << msg->closeInfo.remote << std::endl; - debug_msg << "Connection closed" << std::endl; - debug_msg << "\tCode: " << msg->closeInfo.code << std::endl; - debug_msg << "\tReason: " << msg->closeInfo.reason << std::endl; - debug_msg << "\tRemote: " << msg->closeInfo.remote << std::endl; - - this->toLog(debug_msg.str()); - this->isLoggedIn = false; - } + this->toLog(debug_msg.str()); + this->isLoggedIn = false; } else if (msg->type == ix::WebSocketMessageType::Error) { std::stringstream debug_msg; @@ -86,6 +80,22 @@ namespace gaconnector debug_msg << "\tRetries: " << msg->errorInfo.retries << std::endl; debug_msg << "\tWait time: " << msg->errorInfo.wait_time << std::endl; + if (msg->errorInfo.http_status == 401) { + this->webSocket->disableAutomaticReconnection(); + + this->toLog("Disabling automatic reconnection due to auth failure"); + this->failureMode = failures::AUTH; + } else if (msg->errorInfo.http_status == 500 && + msg->errorInfo.reason.find("Duplicate") != + std::string::npos) { + this->webSocket->disableAutomaticReconnection(); + + this->toLog("Disabling automatic reconnection due to duplicate"); + this->failureMode = failures::DUPLICATESIM; + } else { + this->failureMode = failures::OTHER; + } + this->toLog(debug_msg.str()); this->isLoggedIn = false; } else if (msg->type == ix::WebSocketMessageType::Message) {