set user-agent through ini

This commit is contained in:
ouwou 2020-11-27 01:37:01 -05:00
parent 1463d8da9e
commit ae77bfd1d1
7 changed files with 46 additions and 10 deletions

View File

@ -19,6 +19,11 @@ Abaddon::Abaddon()
, m_emojis("res/emojis.bin") { , m_emojis("res/emojis.bin") {
LoadFromSettings(); LoadFromSettings();
// todo: set user agent for non-client(?)
std::string ua = m_settings.GetSettingString("http", "user_agent", "");
if (ua != "")
m_discord.SetUserAgent(ua);
m_discord.signal_gateway_ready().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady)); m_discord.signal_gateway_ready().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady));
m_discord.signal_message_create().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageCreate)); m_discord.signal_message_create().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageCreate));
m_discord.signal_message_delete().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageDelete)); m_discord.signal_message_delete().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnMessageDelete));

View File

@ -421,6 +421,11 @@ void DiscordClient::UpdateToken(std::string token) {
} }
} }
void DiscordClient::SetUserAgent(std::string agent) {
m_http.SetUserAgent(agent);
m_websocket.SetUserAgent(agent);
}
void DiscordClient::HandleGatewayMessageRaw(std::string str) { void DiscordClient::HandleGatewayMessageRaw(std::string str) {
// handles multiple zlib compressed messages, calling HandleGatewayMessage when a full message is received // handles multiple zlib compressed messages, calling HandleGatewayMessage when a full message is received
std::vector<uint8_t> buf(str.begin(), str.end()); std::vector<uint8_t> buf(str.begin(), str.end());

View File

@ -112,6 +112,7 @@ public:
std::optional<Snowflake> FindDM(Snowflake user_id); // wont find group dms std::optional<Snowflake> FindDM(Snowflake user_id); // wont find group dms
void UpdateToken(std::string token); void UpdateToken(std::string token);
void SetUserAgent(std::string agent);
private: private:
static const constexpr int InflateChunkSize = 0x10000; static const constexpr int InflateChunkSize = 0x10000;

View File

@ -6,6 +6,10 @@ HTTPClient::HTTPClient(std::string api_base)
m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks)); m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks));
} }
void HTTPClient::SetUserAgent(std::string agent) {
m_agent = agent;
}
void HTTPClient::SetAuth(std::string auth) { void HTTPClient::SetAuth(std::string auth) {
m_authorization = auth; m_authorization = auth;
} }
@ -16,16 +20,18 @@ void HTTPClient::MakeDELETE(std::string path, std::function<void(cpr::Response r
auto headers = cpr::Header { auto headers = cpr::Header {
{ "Authorization", m_authorization }, { "Authorization", m_authorization },
}; };
auto ua = cpr::UserAgent {m_agent != "" ? m_agent : "Abaddon" };
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::DeleteCallback( m_futures.push_back(cpr::DeleteCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, url, headers, ua,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::DeleteCallback( m_futures.push_back(cpr::DeleteCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers)); url, headers, ua));
#endif #endif
} }
@ -36,17 +42,19 @@ void HTTPClient::MakePATCH(std::string path, std::string payload, std::function<
{ "Authorization", m_authorization }, { "Authorization", m_authorization },
{ "Content-Type", "application/json" }, { "Content-Type", "application/json" },
}; };
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
auto body = cpr::Body { payload }; auto body = cpr::Body { payload };
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::PatchCallback( m_futures.push_back(cpr::PatchCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body, url, headers, body, ua,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::PatchCallback( m_futures.push_back(cpr::PatchCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body)); url, headers, body, ua));
#endif #endif
} }
@ -57,17 +65,19 @@ void HTTPClient::MakePOST(std::string path, std::string payload, std::function<v
{ "Authorization", m_authorization }, { "Authorization", m_authorization },
{ "Content-Type", "application/json" }, { "Content-Type", "application/json" },
}; };
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
auto body = cpr::Body { payload }; auto body = cpr::Body { payload };
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::PostCallback( m_futures.push_back(cpr::PostCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body, url, headers, body, ua,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::PostCallback( m_futures.push_back(cpr::PostCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body)); url, headers, body, ua));
#endif #endif
} }
@ -78,17 +88,19 @@ void HTTPClient::MakePUT(std::string path, std::string payload, std::function<vo
{ "Authorization", m_authorization }, { "Authorization", m_authorization },
{ "Content-Type", "application/json" }, { "Content-Type", "application/json" },
}; };
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
auto body = cpr::Body { payload }; auto body = cpr::Body { payload };
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::PutCallback( m_futures.push_back(cpr::PutCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body, url, headers, body, ua,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::PutCallback( m_futures.push_back(cpr::PutCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body)); url, headers, body, ua));
#endif #endif
} }
@ -99,16 +111,19 @@ void HTTPClient::MakeGET(std::string path, std::function<void(cpr::Response r)>
{ "Authorization", m_authorization }, { "Authorization", m_authorization },
{ "Content-Type", "application/json" }, { "Content-Type", "application/json" },
}; };
auto ua = cpr::UserAgent { m_agent != "" ? m_agent : "Abaddon" };
auto x = cpr::UserAgent {};
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::GetCallback( m_futures.push_back(cpr::GetCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, url, headers, ua,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::GetCallback( m_futures.push_back(cpr::GetCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers)); url, headers, ua));
#endif #endif
} }

View File

@ -13,6 +13,7 @@ class HTTPClient {
public: public:
HTTPClient(std::string api_base); HTTPClient(std::string api_base);
void SetUserAgent(std::string agent);
void SetAuth(std::string auth); void SetAuth(std::string auth);
void MakeDELETE(std::string path, std::function<void(cpr::Response r)> cb); void MakeDELETE(std::string path, std::function<void(cpr::Response r)> cb);
void MakeGET(std::string path, std::function<void(cpr::Response r)> cb); void MakeGET(std::string path, std::function<void(cpr::Response r)> cb);
@ -32,4 +33,5 @@ private:
std::vector<std::future<void>> m_futures; std::vector<std::future<void>> m_futures;
std::string m_api_base; std::string m_api_base;
std::string m_authorization; std::string m_authorization;
std::string m_agent;
}; };

View File

@ -7,9 +7,14 @@ void Websocket::StartConnection(std::string url) {
m_websocket.disableAutomaticReconnection(); m_websocket.disableAutomaticReconnection();
m_websocket.setUrl(url); m_websocket.setUrl(url);
m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1)); m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1));
m_websocket.setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent } }); // idk if this actually works
m_websocket.start(); m_websocket.start();
} }
void Websocket::SetUserAgent(std::string agent) {
m_agent = agent;
}
void Websocket::Stop() { void Websocket::Stop() {
m_websocket.stop(); m_websocket.stop();
} }

View File

@ -11,6 +11,8 @@ public:
Websocket(); Websocket();
void StartConnection(std::string url); void StartConnection(std::string url);
void SetUserAgent(std::string agent);
void Send(const std::string &str); void Send(const std::string &str);
void Send(const nlohmann::json &j); void Send(const nlohmann::json &j);
void Stop(); void Stop();
@ -21,6 +23,7 @@ private:
void OnMessage(const ix::WebSocketMessagePtr &msg); void OnMessage(const ix::WebSocketMessagePtr &msg);
ix::WebSocket m_websocket; ix::WebSocket m_websocket;
std::string m_agent;
public: public:
typedef sigc::signal<void> type_signal_open; typedef sigc::signal<void> type_signal_open;