Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added possibility to wait for something in interactive recaptcha solving #4245

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/pyload/core/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,18 @@ def set_captcha_result(self, tid, result):
task.set_result(result)
self.pyload.captcha_manager.remove_task(task)

def set_captcha_failed(self, tid, message):
"""
Set result for a captcha task.

:param tid: task id
"""
self.pyload.last_client_connected = time.time()
task = self.pyload.captcha_manager.get_task_by_id(tid)
if task:
task.set_failed(message)
self.pyload.captcha_manager.remove_task(task)

@legacy("getEvents")
@permission(Perms.STATUS)
def get_events(self, uuid):
Expand Down
5 changes: 5 additions & 0 deletions src/pyload/core/managers/captcha_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def set_result(self, result):
except Exception:
self.result = None

def set_failed(self, message):
self.result = None
self.status = 'failed'
self.error = message

def get_result(self):
return self.result

Expand Down
194 changes: 136 additions & 58 deletions src/pyload/plugins/anticaptchas/ReCaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,58 +42,131 @@ class ReCaptcha(CaptchaService):
"0c85257db01aeaf58474b00190fb702f"

RECAPTCHA_INTERACTIVE_JS = """
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Update the signature - i don't have the private key :-)

while(document.children[0].childElementCount > 0) {
document.children[0].removeChild(document.children[0].children[0]);
}
document.children[0].innerHTML = '<html><head></head><body style="display:inline-block;"><div id="captchadiv" style="display: inline-block;"></div></body></html>';

gpyload.data.sitekey = request.params.sitekey;

gpyload.getFrameSize = function() {
var rectAnchor = {top: 0, right: 0, bottom: 0, left: 0},
rectPopup = {top: 0, right: 0, bottom: 0, left: 0},
rect;
var anchor = document.body.querySelector("iframe[src*='/anchor']");
if (anchor !== null && gpyload.isVisible(anchor)) {
rect = anchor.getBoundingClientRect();
rectAnchor = {top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left};
}
var popup = document.body.querySelector("iframe[src*='/bframe']");
if (popup !== null && gpyload.isVisible(popup)) {
rect = popup.getBoundingClientRect();
rectPopup = {top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left};
}
var left = Math.round(Math.min(rectAnchor.left, rectAnchor.right, rectPopup.left, rectPopup.right));
var right = Math.round(Math.max(rectAnchor.left, rectAnchor.right, rectPopup.left, rectPopup.right));
var top = Math.round(Math.min(rectAnchor.top, rectAnchor.bottom, rectPopup.top, rectPopup.bottom));
var bottom = Math.round(Math.max(rectAnchor.top, rectAnchor.bottom, rectPopup.top, rectPopup.bottom));
return {top: top, left: left, bottom: bottom, right: right};
};

// function that is called when the captcha finished loading and is ready to interact
window.pyloadCaptchaOnLoadCallback = function() {
grecaptcha.render (
"captchadiv",
{size: "compact",
'sitekey': gpyload.data.sitekey,
'callback': function() {
var recaptchaResponse = grecaptcha.getResponse(); // get captcha response
gpyload.submitResponse(recaptchaResponse);
}}
);
gpyload.activated();
};

delete window.grecaptcha;
if(typeof grecaptcha !== 'undefined' && grecaptcha) {
window.pyloadCaptchaOnLoadCallback();
} else {
var js_script = document.createElement('script');
js_script.type = "text/javascript";
js_script.src = "//www.google.com/recaptcha/api.js?onload=pyloadCaptchaOnLoadCallback&render=explicit";
js_script.async = true;
document.getElementsByTagName('head')[0].appendChild(js_script);
}"""
(() => {
let is_replaced=false;
gpyload.getFrameSize = function() {
var rectAnchor = {top: 0, right: 0, bottom: 0, left: 0},
rectPopup = {top: 0, right: 0, bottom: 0, left: 0},
rect;
if (is_replaced) {
var anchor = document.body.querySelector("iframe[src*='/anchor']");
if (anchor !== null && gpyload.isVisible(anchor)) {
rect = anchor.getBoundingClientRect();
rectAnchor = {top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left};
}
var popup = document.body.querySelector("iframe[src*='/bframe']");
if (popup !== null && gpyload.isVisible(popup)) {
rect = popup.getBoundingClientRect();
rectPopup = {top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left};
}
}
var left = Math.round(Math.min(rectAnchor.left, rectAnchor.right, rectPopup.left, rectPopup.right));
var right = Math.round(Math.max(rectAnchor.left, rectAnchor.right, rectPopup.left, rectPopup.right));
var top = Math.round(Math.min(rectAnchor.top, rectAnchor.bottom, rectPopup.top, rectPopup.bottom));
var bottom = Math.round(Math.max(rectAnchor.top, rectAnchor.bottom, rectPopup.top, rectPopup.bottom));
return {top: top, left: left, bottom: bottom, right: right};
};
let do_replacement = function()
{
while(document.children[0].childElementCount > 0) {
document.children[0].removeChild(document.children[0].children[0]);
}
document.children[0].innerHTML = '<html><head></head><body style="display:inline-block;"><div id="captchadiv" style="display: inline-block;"></div></body></html>';
is_replaced = true;
gpyload.data.sitekey = request.params.sitekey;

// function that is called when the captcha finished loading and is ready to interact
window.pyloadCaptchaOnLoadCallback = function() {
grecaptcha.render (
"captchadiv", {
size: "compact",
'sitekey': gpyload.data.sitekey,
'callback': function() {
var recaptchaResponse = grecaptcha.getResponse(); // get captcha response
gpyload.submitResponse(recaptchaResponse);
}
}
);
gpyload.activated();
};

delete window.grecaptcha;
if(typeof grecaptcha !== 'undefined' && grecaptcha) {
window.pyloadCaptchaOnLoadCallback();
} else {
var js_script = document.createElement('script');
js_script.type = "text/javascript";
js_script.src = "//www.google.com/recaptcha/api.js?onload=pyloadCaptchaOnLoadCallback&render=explicit";
js_script.async = true;
document.getElementsByTagName('head')[0].appendChild(js_script);
}
};

if (
typeof request.params.script.params == 'undefined'
|| request.params.script.params === null
|| request.params.script.params.wait_for === null
) {
do_replacement();
} else {
let wait_for_target = typeof request.params.script.params.wait_for_target === 'undefined'
? null
: request.params.script.params.wait_for_target;
let wait_for_element = document.querySelector(request.params.script.params.wait_for);
if (
wait_for_element !== null
&& (
wait_for_target === null
|| wait_for_element.parentNode.matches(wait_for_target)
)
) {
console.log('already found');
do_replacement();
} else {
let did_replacement = false;
let observer = new MutationObserver((mutations) => {
const loaded = function() {
gpyload.loaded();
window.clearTimeout(loadTimeOut);
did_replacement = true;
observer.disconnect()
do_replacement();
};

mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
if (wait_for_target !== null && ! mutation.target.matches(wait_for_target)) {
return;
}
for (let i = 0; i < mutation.addedNodes.length; i++) {
let node = mutation.addedNodes[i];
if (
(
node.nodeType === 1
&& [node, ...node.querySelectorAll(request.params.script.params.wait_for)]
.filter(el => el.matches(request.params.script.params.wait_for)).length > 0
)
&& !did_replacement
) {
loaded();
}
}
});
});

console.log('loading');
gpyload.loading();
let loadTimeOut = window.setTimeout(() => { gpyload.aborted(); }, 10000);
observer.observe(document, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}
}
})();
"""

RECAPTCHA_INVISIBLE_SIG = "6c6fb9970fdeac68b95809ce45a344f1225d2284e2c08507261f3506dbeebe9f0e1d9040df64191e" + \
"938f578b52009c31d0da920e1a9616d73ff7b7eb1e964477fac169e412fd1e325992c1783c4664e8" + \
Expand Down Expand Up @@ -185,7 +258,7 @@ def detect_version(self, data=None):
)
return 2

def challenge(self, key=None, data=None, version=None, secure_token=None):
def challenge(self, key=None, data=None, version=None, secure_token=None, script_params=None):
key = key or self.retrieve_key(data)
secure_token = (
secure_token or self.detect_secure_token(data)
Expand All @@ -195,14 +268,17 @@ def challenge(self, key=None, data=None, version=None, secure_token=None):

if version in (2, "2js", "2invisible"):
return getattr(self, "_challenge_v{}".format(version))(
key, secure_token=secure_token
key,
secure_token=secure_token,
script_params=script_params
)
else:
return self.challenge(
key,
data,
version=self.detect_version(data=data),
secure_token=secure_token,
script_params=script_params
)

def _prepare_image(self, image, challenge_msg):
Expand Down Expand Up @@ -332,7 +408,7 @@ def _prepare_image(self, image, challenge_msg):

return img

def _challenge_v2(self, key, secure_token=None):
def _challenge_v2(self, key, secure_token=None, script_params=None):
fallback_url = (
"http://www.google.com/recaptcha/api/fallback?k="
+ key
Expand All @@ -348,7 +424,7 @@ def _challenge_v2(self, key, secure_token=None):
self.log_warning(
self._("reCAPTCHA noscript is blocked, trying reCAPTCHA interactive")
)
return self._challenge_v2js(key, secure_token=secure_token)
return self._challenge_v2js(key, secure_token=secure_token, script_params=script_params)

for i in range(10):
try:
Expand Down Expand Up @@ -417,7 +493,7 @@ def _challenge_v2(self, key, secure_token=None):

# solve interactive captcha (javascript required), use when non-JS captcha
# fallback for v2 is not allowed
def _challenge_v2js(self, key, secure_token=None):
def _challenge_v2js(self, key, secure_token=None, script_params=None):
self.log_debug("Challenge reCAPTCHA v2 interactive")

params = {
Expand All @@ -427,6 +503,7 @@ def _challenge_v2js(self, key, secure_token=None):
"script": {
"signature": self.RECAPTCHA_INTERACTIVE_SIG,
"code": self.RECAPTCHA_INTERACTIVE_JS,
"params": script_params
},
}

Expand All @@ -435,7 +512,7 @@ def _challenge_v2js(self, key, secure_token=None):
return result

# solve invisible captcha (browser only, no user interaction)
def _challenge_v2invisible(self, key, secure_token=None):
def _challenge_v2invisible(self, key, secure_token=None, script_params=None):
self.log_debug("Challenge reCAPTCHA v2 invisible")

params = {
Expand All @@ -445,6 +522,7 @@ def _challenge_v2invisible(self, key, secure_token=None):
"script": {
"signature": self.RECAPTCHA_INVISIBLE_SIG,
"code": self.RECAPTCHA_INVISIBLE_JS,
"params": script_params
},
}

Expand Down
12 changes: 12 additions & 0 deletions src/pyload/webui/app/blueprints/json_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def set_captcha():

return jsonify(data)

@bp.route("/json/set_captcha_failed", methods=["POST"], endpoint="set_captcha_failed")
# @apiver_check
@login_required("ADD")
def set_captcha_failed():
api = flask.current_app.config["PYLOAD_API"]

if flask.request.method == "POST":
tid = int(flask.request.form["cap_id"])
result = flask.request.form["cap_result"]
api.set_captcha_failed(tid, result)
return jsonify(True)
return jsonify(False)

@bp.route("/json/load_config", endpoint="load_config")
# @apiver_check
Expand Down
13 changes: 13 additions & 0 deletions src/pyload/webui/app/static/js/captcha-interactive.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@
var responseMessage = {actionCode: "pyloadSubmitResponse", params: {response: response}};
parent.postMessage(JSON.stringify(responseMessage),"*");
},
loading: function() {
var responseMessage = {actionCode: "pyloadLoadingInteractive"};
parent.postMessage(JSON.stringify(responseMessage),"*");
},
loaded: function() {
console.log('loading ...');
var responseMessage = {actionCode: "pyloadLoadedInteractive"};
parent.postMessage(JSON.stringify(responseMessage),"*");
},
aborted: function() {
var responseMessage = {actionCode: "pyloadAbortedInteractive"};
parent.postMessage(JSON.stringify(responseMessage),"*");
},
activated: function() {
var responseMessage = {actionCode: "pyloadActivatedInteractive"};
parent.postMessage(JSON.stringify(responseMessage),"*");
Expand Down
1 change: 1 addition & 0 deletions src/pyload/webui/app/templates/captcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h1>{{_("Captcha reading")}}</h1>
<p>{{_('Your browser does not support iframes.')}}</p>
</iframe>
</div>
<div id="cap_interactive_content_loading" style="display: none; background-image: url({{theme_static('img/ajax-loader.gif')}});" ></div>
<div id="cap_interactive_loading">
<p style="white-space: nowrap;">{{_("The captcha may take a few seconds to load.")}}</p>
<p>{{_("Note: to solve this interactive captchas")}}<br>
Expand Down
9 changes: 9 additions & 0 deletions src/pyload/webui/app/themes/modern/static/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ fieldset[disabled] .btn {
.modal-dialog {
margin-top: 10px;
}

#cap_interactive_content_loading {
position: relative;
height: 100%;
width: 100%;
background-repeat:no-repeat;
background-position: top center;
min-height: 24px;
}
1 change: 1 addition & 0 deletions src/pyload/webui/app/themes/modern/templates/captcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<p>{{_('Your browser does not support iframes.')}}</p>
</iframe>
</div>
<div id="cap_interactive_content_loading" style="display: none; background-image: url({{theme_static('img/ajax-loader.gif')}});" ></div>
<div id="cap_interactive_loading">
<p style="white-space: nowrap;">{{_("The captcha may take a few seconds to load.")}}</p>
<p>{{_("Note: to solve this interactive captcha")}}<br>
Expand Down