-
Notifications
You must be signed in to change notification settings - Fork 5
Captcha V2 #14
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
Captcha V2 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import pystache | ||
| import px_template | ||
| import px_constants | ||
| import json | ||
|
|
||
|
|
||
| class PXBlocker(object): | ||
| def __init__(self): | ||
| self.mustache_renderer = pystache.Renderer() | ||
| self.ratelimit_rendered_page = self.mustache_renderer.render( | ||
| px_template.get_template(px_constants.RATELIMIT_TEMPLATE), {}) | ||
|
|
||
| def handle_blocking(self, ctx, config, start_response): | ||
| action = ctx.get('block_action') | ||
| status = '403 Forbidden' | ||
|
|
||
| is_json_response = self.is_json_response(ctx) | ||
| if is_json_response: | ||
| content_type = 'application/json' | ||
| else: | ||
| content_type = 'text/html' | ||
| headers = [('Content-Type', content_type)] | ||
|
|
||
| if action is 'j': | ||
| blocking_props = ctx['block_action_data'] | ||
| blocking_response = blocking_props | ||
| elif action is 'r': | ||
| blocking_response = self.ratelimit_rendered_page | ||
| status = '429 Too Many Requests' | ||
| else: | ||
| blocking_props = self.prepare_properties(ctx, config) | ||
| blocking_response = self.mustache_renderer.render(px_template.get_template(px_constants.BLOCK_TEMPLATE), | ||
| blocking_props) | ||
| start_response(status, headers) | ||
| if is_json_response: | ||
| blocking_response = json.dumps(blocking_props) | ||
| return str(blocking_response) | ||
|
|
||
| def prepare_properties(self, ctx, config): | ||
| app_id = config.get('app_id').lower() | ||
| vid = ctx.get('vid') if ctx.get('vid') is not None else '' | ||
| uuid = ctx.get('uuid') | ||
| custom_logo = config.get('CUSTOM_LOGO') if config.get('CUSTOM_LOGO') is not None else '' | ||
| is_mobile_num = 1 if ctx.get('is_mobile') else 0 | ||
| captcha_uri = 'captcha.js?a={}&u={}&v={}&m={}'.format(ctx.get('block_action'), uuid, vid, is_mobile_num) | ||
|
|
||
| if config.get('first_party') and not ctx.get('is_mobile'): | ||
| prefix = app_id[2:] | ||
| js_client_src = '/{}/{}'.format(prefix, px_constants.CLIENT_FP_PATH) | ||
| captcha_src = '/{}/{}/{}'.format(prefix, px_constants.CAPTCHA_FP_PATH, captcha_uri) | ||
| host_url = '/{}/{}'.format(prefix, px_constants.XHR_FP_PATH) | ||
| else: | ||
| js_client_src = '//{}/{}/main.min.js'.format(px_constants.CLIENT_HOST, app_id) | ||
| captcha_src = '//{}/{}/{}'.format(px_constants.CAPTCHA_HOST, app_id, captcha_uri) | ||
| host_url = px_constants.COLLECTOR_URL.format(app_id.lower()) | ||
|
|
||
| return { | ||
| 'refId': uuid, | ||
| 'appId': app_id, | ||
| 'vid': vid, | ||
| 'uuid': uuid, | ||
| 'customLogo': custom_logo, | ||
| 'cssRef': config.get('css_ref'), | ||
| 'jsRef': config.get('js_ref'), | ||
| 'logoVisibility': 'visible' if custom_logo is not None else 'hidden', | ||
| 'hostUrl': host_url, | ||
| 'jsClientSrc': js_client_src, | ||
| 'firstPartyEnabled': config.get('first_party'), | ||
| 'blockScript': captcha_src | ||
| } | ||
|
|
||
| def is_json_response(self, ctx): | ||
| headers = ctx.get('headers') | ||
| if ctx.get('block_action') is not 'r': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not redundant, It sets the content-type, even if the block_Action is 'r' it will get there |
||
| for item in headers.keys(): | ||
| if (item.lower() == 'accept' or item.lower() == 'content-type'): | ||
| item_arr = headers[item].split(',') | ||
| for header_item in item_arr: | ||
| if header_item.strip() == 'application/json': | ||
| return True | ||
| return False | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,16 @@ | ||
| import pystache | ||
| import os | ||
|
|
||
| def get_template(template, config, uuid, vid): | ||
| template_content = get_content(template) | ||
| props = get_props(config, uuid, vid) | ||
| generatedHtml = pystache.render(template_content, props) | ||
| return generatedHtml | ||
|
|
||
| def get_path(): | ||
| return os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| def get_content(template): | ||
| templatePath = "%s/templates/%s.mustache" % (get_path(),template) | ||
| templatePath = "%s/templates/%s" % (get_path(), template) | ||
| file = open(templatePath, "r") | ||
| content = file.read() | ||
| return content | ||
|
|
||
| def get_props(config, uuid, vid): | ||
| return { | ||
| 'refId': uuid, | ||
| 'appId': config.get('app_id'), | ||
| 'vid': vid, | ||
| 'uuid': uuid, | ||
| 'customLogo': config.get('custom_logo'), | ||
| 'cssRef': config.get('css_ref'), | ||
| 'jsRef': config.get('js_ref'), | ||
| 'logoVisibility': 'visible' if config['custom_logo'] else 'hidden' | ||
| } | ||
|
|
||
|
|
||
| def get_template(template_name): | ||
| return get_content(template_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's json response, then the response shouldn't be a compiled html template
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done