feat: download mcaptcha cli and setup mcaptcha server for testing

This commit is contained in:
Aravinth Manivannan 2023-12-31 21:09:47 +05:30
parent 32a43b5445
commit b6732958c9
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 148 additions and 0 deletions

114
scripts/create_captcha.py Normal file
View file

@ -0,0 +1,114 @@
# SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
#
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT
import sys
from urllib.parse import urlparse, urlunparse, urlencode, parse_qsl
from requests import Session
def clean_url(url):
parsed = urlparse(url)
return urlunparse((parsed.scheme, parsed.netloc, "", "", "", ""))
def set_path(url: str, path: str) -> str:
parsed = urlparse(url)
return urlunparse((parsed.scheme, parsed.netloc, path, "", "", ""))
def help():
print("Usage: create_captcha.py <option>")
print("OPTIONS:\ncreate_captcha <mcaptcha_instance_url> <username> <password>")
print("get_secret <mcaptcha_instance_url> <username> <password>")
print("widget_url <mcaptcha_instance_url> <sitekey>")
c = Session()
if len(sys.argv) < 1:
help()
exit(1)
option = sys.argv[1]
def login():
if len(sys.argv) < 4:
help()
exit(1)
instance_url = clean_url(sys.argv[2])
username = sys.argv[3]
password = sys.argv[4]
url = set_path(instance_url, "/api/v1/signin")
payload = {"login": username, password: password}
resp = c.post(url, json=payload)
assert resp.status_code == 200
def create_captcha():
instance_url = clean_url(sys.argv[2])
username = sys.argv[3]
password = sys.argv[4]
levels = [
{"difficulty_factor": 50, "visitor_threshold": 50},
{"difficulty_factor": 500, "visitor_threshold": 500},
]
payload = {
"levels": levels,
"duration": 30,
"description": "create_captcha_test_script",
"publish_benchmarks": False,
}
url = set_path(instance_url, "/api/v1/mcaptcha/create")
resp = c.post(url, json=payload, cookies=c.cookies.get_dict())
assert resp.status_code == 200
return resp.json()["key"]
def get_secret():
if len(sys.argv) < 3:
help()
exit(1)
instance_url = clean_url(sys.argv[2])
username = sys.argv[3]
password = sys.argv[4]
instance_url = clean_url(sys.argv[2])
sitekey = sys.argv[3]
url = set_path(instance_url, "/api/v1/account/secret/get")
resp = c.get(url, cookies=c.cookies.get_dict())
assert resp.status_code == 200
return resp.json()["secret"]
def widget_url():
if len(sys.argv) < 4:
help()
exit(1)
instance_url = set_path(clean_url(sys.argv[2]), "/widget")
sitekey = sys.argv[3]
query = urlencode({"sitekey": sitekey})
url = f"{instance_url}?{query}"
return url
if __name__ == "__main__":
if option == "create_captcha":
login()
print(create_captcha())
elif option == "get_secret":
login()
print(get_secret())
elif option == "widget_url":
print(widget_url())
else:
help()

34
scripts/download-cli.sh Executable file
View file

@ -0,0 +1,34 @@
#!/bin/bash
# SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
#
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT
readonly PROJECT_ROOT=$(pwd)
readonly SOURCE=https://dl.mcaptcha.org/mcaptcha/cli/0.3.0/x86_64-unknown-linux-gnu.tar.gz
readonly BIN_PATH=tmp/cli/mcaptcha-cli
readonly TARBALL=mcaptcha-cli.tar.gz
download() {
if [ ! -e $BIN_PATH ];
then
mkdir -p tmp/cli
cd $BIN_PATH
wget $SOURCE --quiet --output-document=$TARBALL $SOURCE
tar -xvzf $TARBALL > /dev/null
rm $TARBALL
echo "[*] Downloaded mCaptcha CLI"
mv build/x86_64-unknown-linux-gnu/mcaptcha-cli .
rm -rf build/
cd $PROJECT_ROOT
fi
}
# 1: widget URI
gen_pow() {
$BIN_PATH widget-url $2 | cut -d ':' -f 2 | tr -d ' '
}
$1 $@