Potential auth fix

This commit is contained in:
Anonymous275 2021-01-07 15:29:06 +02:00
parent 61776d6a1b
commit fc4bc14ce5
3 changed files with 29 additions and 14 deletions

View File

@ -69,15 +69,14 @@ std::string RunPromise(const std::string& IP, const std::string& R) {
} }
} }
//Server Authenticated //Server Authenticated
if (T.length() == 4) info(T);
info(("Server authenticated"));
R.clear();
T.clear();
if (!isAuth) { if (!isAuth) {
WebsocketInit(); WebsocketInit();
if (T.length() == 4)info(("Authenticated!"));
else info(("Resumed authenticated session!"));
isAuth = true; isAuth = true;
} }
std::this_thread::sleep_for(std::chrono::seconds(5)); //std::this_thread::sleep_for(std::chrono::seconds(5));
} }
} }
void HBInit() { void HBInit() {

View File

@ -9,15 +9,31 @@
#include "CustomAssert.h" #include "CustomAssert.h"
#include <curl/curl.h> #include <curl/curl.h>
#include <iostream> #include <iostream>
class CurlManager{
public:
CurlManager(){
curl = curl_easy_init();
}
~CurlManager(){
curl_easy_cleanup(curl);
}
inline CURL* Get(){
return curl;
}
private:
CURL *curl;
};
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb); ((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb; return size * nmemb;
} }
std::string HttpRequest(const std::string& IP, int port) { std::string HttpRequest(const std::string& IP, int port) {
CURL* curl; static thread_local CurlManager M;
CURLcode res;
std::string readBuffer; std::string readBuffer;
curl = curl_easy_init(); CURL* curl = M.Get();
CURLcode res;
Assert(curl); Assert(curl);
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
@ -25,7 +41,6 @@ std::string HttpRequest(const std::string& IP, int port) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) if (res != CURLE_OK)
return "-1"; return "-1";
} }
@ -33,13 +48,14 @@ std::string HttpRequest(const std::string& IP, int port) {
} }
std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json) { std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json) {
static auto* header = new curl_slist { (char*)"Content-Type: application/json" }; auto header = curl_slist { (char*)"Content-Type: application/json" };
static thread_local CurlManager M;
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
CURL* curl; CURL* curl = M.Get();
CURLcode res; CURLcode res;
std::string readBuffer; std::string readBuffer;
curl = curl_easy_init();
Assert(curl); Assert(curl);
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
@ -52,7 +68,6 @@ std::string PostHTTP(const std::string& IP, const std::string& Fields, bool json
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) if (res != CURLE_OK)
return "-1"; return "-1";
} }

View File

@ -18,6 +18,7 @@
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <array>
SOCKET UDPSock; SOCKET UDPSock;
void UDPSend(Client* c, std::string Data) { void UDPSend(Client* c, std::string Data) {
@ -73,7 +74,7 @@ void SendLarge(Client* c, std::string Data) {
std::string UDPRcvFromClient(sockaddr_in& client) { std::string UDPRcvFromClient(sockaddr_in& client) {
size_t clientLength = sizeof(client); size_t clientLength = sizeof(client);
std::array<char, 1024> Ret; std::array<char, 1024> Ret{};
int64_t Rcv = recvfrom(UDPSock, Ret.data(), Ret.size(), 0, (sockaddr*)&client, (socklen_t*)&clientLength); int64_t Rcv = recvfrom(UDPSock, Ret.data(), Ret.size(), 0, (sockaddr*)&client, (socklen_t*)&clientLength);
if (Rcv == -1) { if (Rcv == -1) {
#ifdef WIN32 #ifdef WIN32