Merge branch 'master' into disk-cache

This commit is contained in:
ouwou 2020-11-28 02:16:59 -05:00
commit 1989b22993
9 changed files with 51 additions and 15 deletions

View File

@ -20,6 +20,11 @@ Abaddon::Abaddon()
, m_discord(m_settings.GetSettingBool("discord", "memory_db", false)) { // stupid but easy , m_discord(m_settings.GetSettingBool("discord", "memory_db", false)) { // stupid but easy
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

@ -424,6 +424,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;

View File

@ -85,15 +85,15 @@ void Cache::OnResponse(const cpr::Response &r) {
if (r.error || r.status_code > 300) return; if (r.error || r.status_code > 300) return;
std::vector<uint8_t> data(r.text.begin(), r.text.end()); std::vector<uint8_t> data(r.text.begin(), r.text.end());
auto path = m_tmp_path / SanitizeString(r.url); auto path = m_tmp_path / SanitizeString(static_cast<std::string>(r.url));
FILE *fp = std::fopen(path.string().c_str(), "wb"); FILE *fp = std::fopen(path.string().c_str(), "wb");
if (fp == nullptr) if (fp == nullptr)
return; return;
std::fwrite(data.data(), 1, data.size(), fp); std::fwrite(data.data(), 1, data.size(), fp);
std::fclose(fp); std::fclose(fp);
for (const auto &cb : m_callbacks[r.url]) { for (const auto &cb : m_callbacks[static_cast<std::string>(r.url)]) {
cb(path.string()); cb(path.string());
} }
m_callbacks.erase(r.url); m_callbacks.erase(static_cast<std::string>(r.url));
} }

View File

@ -86,8 +86,8 @@ MainWindow::MainWindow()
m_chan_chat_paned.pack1(*channel_list); m_chan_chat_paned.pack1(*channel_list);
m_chan_chat_paned.pack2(m_chat_members_paned); m_chan_chat_paned.pack2(m_chat_members_paned);
m_chan_chat_paned.child_property_shrink(*channel_list) = true; m_chan_chat_paned.child_property_shrink(*channel_list) = false;
m_chan_chat_paned.child_property_resize(*channel_list) = true; m_chan_chat_paned.child_property_resize(*channel_list) = false;
m_chan_chat_paned.set_position(200); m_chan_chat_paned.set_position(200);
m_content_box.add(m_chan_chat_paned); m_content_box.add(m_chan_chat_paned);