From e5fc00e506806f06bde32e9334fdec57dd8cb86d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 27 Jul 2021 20:24:36 +0200 Subject: Implement new 4chan captcha --- src/plugins/Fourchan.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/plugins/Fourchan.cpp') diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index d4fb726..84a0675 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -3,6 +3,8 @@ #include "../../include/Storage.hpp" #include "../../include/StringUtils.hpp" #include "../../include/NetUtils.hpp" +#include "../../include/Notification.hpp" +#include "../../external/cppcodec/base64_rfc4648.hpp" #include #include #include @@ -500,7 +502,7 @@ namespace QuickMedia { return filepath.c_str() + index + 1; } - PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &comment, const std::string &filepath) { + PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &captcha_solution, const std::string &comment, const std::string &filepath) { std::string url = "https://sys.4chan.org/" + board_id + "/post"; std::vector additional_args = { @@ -517,7 +519,9 @@ namespace QuickMedia { } if(pass_id.empty()) { - additional_args.push_back(CommandArg{"--form-string", "g-recaptcha-response=" + captcha_id}); + additional_args.push_back(CommandArg{"--form-string", "t-challenge=" + captcha_id}); + if(!captcha_solution.empty()) + additional_args.push_back(CommandArg{"--form-string", "t-response=" + captcha_solution}); } else { Path cookies_filepath; if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) { @@ -558,4 +562,58 @@ namespace QuickMedia { } return pass_id; } + + static bool base64_decode(const Json::Value &json_to_decode, std::string &decoded) { + if(!json_to_decode.isString()) + return false; + + const char *start; + const char *end; + if(!json_to_decode.getString(&start, &end)) + return false; + + try { + decoded = cppcodec::base64_rfc4648::decode(start, end - start); + return true; + } catch(std::exception&) { + return false; + } + } + + PluginResult FourchanThreadPage::request_captcha_challenge(ImageBoardCaptchaChallenge &challenge_response) { + Json::Value json_root; + DownloadResult result = download_json(json_root, "https://sys.4chan.org/captcha?board=" + url_param_encode(board_id) + "&thread_id=" + thread_id, {}, true); + if(result != DownloadResult::OK) return download_result_to_plugin_result(result); + + if(!json_root.isObject()) + return PluginResult::ERR; + + const Json::Value &error_json = json_root["error"]; + if(error_json.isString()) { + show_notification("QuickMedia", "Failed to get captcha, error: " + error_json.asString(), Urgency::CRITICAL); + return PluginResult::ERR; + } + + const Json::Value &challenge_json = json_root["challenge"]; + const Json::Value &img_json = json_root["img"]; + const Json::Value &bg_json = json_root["bg"]; + const Json::Value &ttl_json = json_root["ttl"]; + if(!challenge_json.isString() || !img_json.isString()) + return PluginResult::ERR; + + challenge_response.challenge_id = challenge_json.asString(); + + if(!base64_decode(img_json, challenge_response.img_data)) + return PluginResult::ERR; + + if(bg_json.isString() && !base64_decode(bg_json, challenge_response.bg_data)) + return PluginResult::ERR; + + if(ttl_json.isInt()) + challenge_response.ttl = ttl_json.asInt(); + else + challenge_response.ttl = 120; + + return PluginResult::OK; + } } \ No newline at end of file -- cgit v1.2.3