Skip to content

Commit

Permalink
* Add support for X-Super-Properties.
Browse files Browse the repository at this point in the history
Closes #17.
  • Loading branch information
iProgramMC committed May 25, 2024
1 parent 78e5131 commit 48546f5
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deps/iprogsthreads
2 changes: 1 addition & 1 deletion deps/mwas
Submodule mwas updated 3 files
+0 −2 README.md
+1 −36 include/ri/reimpl.hpp
+1 −15 src/reimpl.cpp
116 changes: 116 additions & 0 deletions src/discord/DiscordClientConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "DiscordClientConfig.hpp"
#include <boost/base64/base64.hpp>

// TODO: Fetch some of these properties from the system (e.g. system locale)

// This configuration simulates a Microsoft Edge web client on Microsoft Windows 10.

DiscordClientConfig* GetClientConfig()
{
static DiscordClientConfig s_config;
return &s_config;
}

DiscordClientConfig::DiscordClientConfig()
{
// Setup here
m_os = "Windows";
m_browser = "Chrome";
m_systemLocale = "en-US";
m_timeZone = "Europe/Bucharest"; // TODO: UTC here
m_browserVersion = "124.0.0.0";
m_browserVersionSimple = "124";
m_webKitVersion = "537.36";
m_osVersion = "10";
m_releaseChannel = "stable";
m_clientBuildNumber = 291963;
m_device = "";
m_referrer = "";
m_referrerCurrent = "";
m_referringDomain = "";
m_referringDomainCurrent = "";
m_designId = 0;

m_browserUserAgent =
"Mozilla/5.0 (Windows NT "
+ m_osVersion
+ ".0; Win64; x64) AppleWebKit/"
+ m_webKitVersion
+ " (KHTML, like Gecko) Chrome/"
+ m_browserVersion
+ " Safari/"
+ m_webKitVersion
+ " Edg/"
+ m_browserVersion;

m_secChUa = "\"Chromium\";v=\"" + m_browserVersionSimple + "\", \"Microsoft Edge\";v=\"124\", \"Not-A.Brand\";v=\"99\"";

// Ok, now serialize it
nlohmann::json j = Serialize();

std::string str = j.dump();
m_serializedJsonBlob = str;

char* buffer = new char[base64::encoded_size(str.size()) + 1];
size_t sz = base64::encode(buffer, str.data(), str.size());
std::string dataToSend(buffer, sz);
delete[] buffer;

m_serializedBase64Blob = dataToSend;
}

nlohmann::json DiscordClientConfig::Serialize() const
{
nlohmann::json j;
j["os"] = m_os;
j["browser"] = m_browser;
j["device"] = m_device;
j["system_locale"] = m_systemLocale;
j["browser_user_agent"] = m_browserUserAgent;
j["browser_version"] = m_browserVersion;
j["os_version"] = m_osVersion;
j["referrer"] = m_referrer;
j["referring_domain"] = m_referringDomain;
j["referrer_current"] = m_referrerCurrent;
j["referring_domain_current"] = m_referringDomainCurrent;
j["release_channel"] = m_releaseChannel;
j["client_build_number"] = m_clientBuildNumber;
j["client_event_source"] = nullptr;
j["design_id"] = m_designId;
return j;
}

const std::string& DiscordClientConfig::GetSerializedJsonBlob() const
{
return m_serializedJsonBlob;
}

const std::string& DiscordClientConfig::GetSerializedBase64Blob() const
{
return m_serializedBase64Blob;
}

const std::string& DiscordClientConfig::GetUserAgent() const
{
return m_browserUserAgent;
}

const std::string& DiscordClientConfig::GetSecChUa() const
{
return m_secChUa;
}

const std::string& DiscordClientConfig::GetLocale() const
{
return m_systemLocale;
}

const std::string& DiscordClientConfig::GetTimezone() const
{
return m_timeZone;
}

const std::string& DiscordClientConfig::GetOS() const
{
return m_os;
}
48 changes: 48 additions & 0 deletions src/discord/DiscordClientConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <nlohmann/json.h>
#include <string>

class DiscordClientConfig
{
public:
DiscordClientConfig();

const std::string& GetUserAgent() const;
const std::string& GetSecChUa() const;
const std::string& GetLocale() const;
const std::string& GetTimezone() const;
const std::string& GetOS() const;

nlohmann::json Serialize() const;
const std::string& GetSerializedJsonBlob() const;
const std::string& GetSerializedBase64Blob() const;

private:
std::string m_os;
std::string m_browser;
std::string m_device;
std::string m_systemLocale;
std::string m_browserUserAgent;
std::string m_browserVersion;
std::string m_osVersion;
std::string m_referrer;
std::string m_referringDomain;
std::string m_referrerCurrent;
std::string m_referringDomainCurrent;
std::string m_releaseChannel;
int m_clientBuildNumber;
int m_designId;

// not part of the actual header itself, just the user agent
std::string m_webKitVersion;


std::string m_browserVersionSimple;
std::string m_secChUa;
std::string m_timeZone;
std::string m_serializedJsonBlob;
std::string m_serializedBase64Blob;
};

DiscordClientConfig* GetClientConfig();
3 changes: 2 additions & 1 deletion src/discord/DiscordInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Util.hpp"
#include "Frontend.hpp"
#include "HTTPClient.hpp"
#include "DiscordClientConfig.hpp"

#define DISCORD_WSS_DETAILS "?encoding=json&v=" DISCORD_API_VERSION

Expand Down Expand Up @@ -783,7 +784,7 @@ void DiscordInstance::HandleGatewayMessage(const std::string& payload)
presenceData["since"] = 0;
presenceData["status"] = "online";

GetFrontend()->GetIdentifyProperties(propertiesData);
propertiesData = GetClientConfig()->Serialize();

data["presence"] = presenceData;
data["properties"] = propertiesData;
Expand Down
4 changes: 0 additions & 4 deletions src/discord/Frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ class Frontend
// Quit
virtual void RequestQuit() = 0;

// User stuff
virtual void GetIdentifyProperties(nlohmann::json& j) = 0;
virtual std::string GetUserAgent() = 0;

// Strings
virtual std::string GetDirectMessagesText() = 0;
virtual std::string GetPleaseWaitText() = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/discord/WebsocketClient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "WebsocketClient.hpp"
#include "DiscordClientConfig.hpp"
#include "Frontend.hpp"
#include "Util.hpp"
#include "LocalSettings.hpp"
Expand Down Expand Up @@ -199,7 +200,7 @@ int WebsocketClient::Connect(const std::string& uri)
return -1;
}

con->append_header("User-Agent", GetFrontend()->GetUserAgent());
con->append_header("User-Agent", GetClientConfig()->GetUserAgent());
con->append_header("Origin", "https://discord.com");

int newID = m_nextId++;
Expand Down
24 changes: 0 additions & 24 deletions src/windows/Frontend_Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "../discord/UpdateChecker.hpp"
#include "../discord/LocalSettings.hpp"

const std::string g_UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) discord/1.0.284 Chrome/120.0.6099.283 Electron/28.2.3 Safari/537.36";

void Frontend_Win32::OnLoginAgain()
{
SendMessage(g_Hwnd, WM_LOGINAGAIN, 0, 0);
Expand Down Expand Up @@ -367,23 +365,6 @@ void Frontend_Win32::RequestQuit()
::WantQuit();
}

void Frontend_Win32::GetIdentifyProperties(nlohmann::json& j)
{
j["app_arch"] = "x64";
j["browser"] = "Discord Client";
j["browser_user_agent"] = g_UserAgent;
j["browser_version"] = "28.2.3";
j["client_build_number"] = 269579;
j["client_event_source"] = nullptr;
j["client_version"] = "1.0.284";
j["native_build_number"] = 44304;
j["os"] = "Windows";
j["os_arch"] = "x64";
j["os_version"] = "10.0.19044";
j["release_channel"] = "canary";
j["system_locale"] = "en-US";
}

#ifdef USE_DEBUG_PRINTS

void DbgPrintWV(const char* fmt, va_list vl)
Expand All @@ -410,11 +391,6 @@ void Frontend_Win32::DebugPrint(const char* fmt, va_list vl)

#endif

std::string Frontend_Win32::GetUserAgent()
{
return g_UserAgent;
}

void Frontend_Win32::SetHeartbeatInterval(int timeMs)
{
::SetHeartbeatInterval(timeMs);
Expand Down
2 changes: 0 additions & 2 deletions src/windows/Frontend_Win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class Frontend_Win32 : public Frontend
void RegisterAttachment(Snowflake sf, const std::string& avatarlnk) override;
void RegisterChannelIcon(Snowflake sf, const std::string& avatarlnk) override;
void RequestQuit() override;
void GetIdentifyProperties(nlohmann::json& j) override;
std::string GetUserAgent() override;
std::string GetDirectMessagesText() override;
std::string GetPleaseWaitText() override;
std::string GetMonthName(int index) override;
Expand Down
9 changes: 8 additions & 1 deletion src/windows/NetworkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../discord/DiscordRequest.hpp"
#include "../discord/LocalSettings.hpp"
#include "../discord/Frontend.hpp"
#include "../discord/DiscordClientConfig.hpp"

#define CPPHTTPLIB_OPENSSL_SUPPORT

Expand Down Expand Up @@ -167,7 +168,13 @@ void NetworkerThread::FulfillRequest(NetRequest& req)
client.set_follow_location(true);

Headers headers;
headers.insert(std::make_pair("User-Agent", GetFrontend()->GetUserAgent()));
headers.insert(std::make_pair("User-Agent", GetClientConfig()->GetUserAgent()));
headers.insert(std::make_pair("X-Super-Properties", GetClientConfig()->GetSerializedBase64Blob()));
headers.insert(std::make_pair("X-Discord-Timezone", GetClientConfig()->GetTimezone()));
headers.insert(std::make_pair("X-Discord-Locale", GetClientConfig()->GetLocale()));
headers.insert(std::make_pair("Sec-Ch-Ua", GetClientConfig()->GetSecChUa()));
headers.insert(std::make_pair("Sec-Ch-Ua-Mobile", "?0"));
headers.insert(std::make_pair("Sec-Ch-Ua-Platform", GetClientConfig()->GetOS()));

if (req.authorization.size())
{
Expand Down
2 changes: 2 additions & 0 deletions vs/DiscordMessenger.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
<ClInclude Include="..\src\discord\Channel.hpp" />
<ClInclude Include="..\src\discord\ContentType.hpp" />
<ClInclude Include="..\src\discord\DiscordAPI.hpp" />
<ClInclude Include="..\src\discord\DiscordClientConfig.hpp" />
<ClInclude Include="..\src\discord\DiscordInstance.hpp" />
<ClInclude Include="..\src\discord\DiscordRequest.hpp" />
<ClInclude Include="..\src\discord\FormattedText.hpp" />
Expand Down Expand Up @@ -386,6 +387,7 @@
<ClCompile Include="..\deps\mwas\src\reimpl.cpp" />
<ClCompile Include="..\src\discord\Channel.cpp" />
<ClCompile Include="..\src\discord\DiscordAPI.cpp" />
<ClCompile Include="..\src\discord\DiscordClientConfig.cpp" />
<ClCompile Include="..\src\discord\DiscordInstance.cpp" />
<ClCompile Include="..\src\discord\FormattedText.cpp" />
<ClCompile Include="..\src\discord\Guild.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions vs/DiscordMessenger.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@
<ClInclude Include="..\src\discord\UpdateChecker.hpp">
<Filter>Header Files\Discord</Filter>
</ClInclude>
<ClInclude Include="..\src\discord\DiscordClientConfig.hpp">
<Filter>Header Files\Discord</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\windows\ProfileView.cpp">
Expand Down Expand Up @@ -682,5 +685,8 @@
<ClCompile Include="..\src\discord\UpdateChecker.cpp">
<Filter>Source Files\Discord</Filter>
</ClCompile>
<ClCompile Include="..\src\discord\DiscordClientConfig.cpp">
<Filter>Source Files\Discord</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit 48546f5

Please sign in to comment.