Skip to content

Commit

Permalink
Merge branch 'msbuild' into pr/gh_actions
Browse files Browse the repository at this point in the history
  • Loading branch information
hdk5 committed Nov 5, 2022
2 parents c88208a + 767c065 commit e3f27b6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/link/twitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,11 @@ bool SetStatusText(const std::wstring& status_text) {
return false;
previous_status_text = status_text;

const auto in_reply_to_status_id = taiga::settings.GetShareTwitterReplyTo();

oauth::Parameters post_parameters;
post_parameters[L"status"] = Url::Encode(status_text);
post_parameters[L"in_reply_to_status_id"] = Url::Encode(in_reply_to_status_id);

constexpr auto kTarget = "https://api.twitter.com/1.1/statuses/update.json";

Expand All @@ -154,8 +157,6 @@ bool SetStatusText(const std::wstring& status_text) {
{}},
post_parameters));

const auto in_reply_to_status_id = taiga::settings.GetShareTwitterReplyTo();

taiga::http::Request request;
request.set_method("POST");
request.set_target(kTarget);
Expand Down
10 changes: 10 additions & 0 deletions src/sync/anilist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ bool HasError(const taiga::http::Response& response) {
if (response.status_code() == 200)
return false;

if (taiga::http::util::IsDdosProtectionEnabled(response)) {
const std::wstring error_description =
L"AniList: Cannot connect to server because "
L"of DDoS protection (Server: {})"_format(
StrToWstr(response.header("server")));
LOGE(error_description);
ui::ChangeStatusText(error_description);
return true;
}

if (Json root; JsonParseString(response.body(), root)) {
if (root.count("errors")) {
if (const auto& errors = root["errors"]; errors.is_array()) {
Expand Down
18 changes: 18 additions & 0 deletions src/taiga/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "taiga/http.h"

#include <nstd/string.hpp>

#include "base/file.h"
#include "base/format.h"
#include "base/gzip.h"
Expand Down Expand Up @@ -368,6 +370,22 @@ std::wstring GetUrlHost(const std::string_view url) {
return {};
}

// Check for DDoS protection that requires a JavaScript challenge to be solved
// (e.g. Cloudflare's "I'm Under Attack" mode)
bool IsDdosProtectionEnabled(const Response& response) {
const std::string server = nstd::tolower_string(response.header("server"));

switch (response.status_code()) {
case hypp::status::k403_Forbidden:
return nstd::starts_with(server, "ddos-guard");
case hypp::status::k429_Too_Many_Requests:
case hypp::status::k503_Service_Unavailable:
return nstd::starts_with(server, "cloudflare");
}

return false;
};

std::wstring to_string(const Error& error, const std::wstring& host) {
std::wstring message = StrToWstr(error.str());
TrimRight(message, L" \r\n");
Expand Down
1 change: 1 addition & 0 deletions src/taiga/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace util {

std::wstring GetUrlHost(const Uri& uri);
std::wstring GetUrlHost(const std::string_view url);
bool IsDdosProtectionEnabled(const Response& response);
std::wstring to_string(const Error& error, const std::wstring& host);
std::wstring to_string(const Transfer& transfer);

Expand Down
17 changes: 1 addition & 16 deletions src/track/feed_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <regex>
#include <set>

#include <nstd/string.hpp>

#include "track/feed_aggregator.h"

#include "base/file.h"
Expand Down Expand Up @@ -51,20 +49,7 @@ static bool HandleFeedError(const std::wstring& host,
return true;
}

// Check for DDoS protection that requires a JavaScript challenge to be solved
// (e.g. Cloudflare's "I'm Under Attack" mode)
const auto ddos_protection_enabled = [&response]() {
const std::string server = nstd::tolower_string(response.header("server"));
switch (response.status_code()) {
case hypp::status::k403_Forbidden:
return nstd::starts_with(server, "ddos-guard");
case hypp::status::k429_Too_Many_Requests:
case hypp::status::k503_Service_Unavailable:
return nstd::starts_with(server, "cloudflare");
}
return false;
};
if (ddos_protection_enabled()) {
if (taiga::http::util::IsDdosProtectionEnabled(response)) {
ui::ChangeStatusText(
L"Cannot connect to {} because of DDoS protection (Server: {})"_format(
host, StrToWstr(response.header("server"))));
Expand Down

0 comments on commit e3f27b6

Please sign in to comment.