2024-11-16 13:17:39 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models.mirrors import Country, Origin, Proxy, country_origin
|
|
|
|
from app.terraform.block_mirror import BlockMirrorAutomation
|
|
|
|
|
|
|
|
|
|
|
|
def clean_json_response(raw_response: str) -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Seems to be a bug in the API where a <script> tag is appended after the JSON.
|
|
|
|
"""
|
|
|
|
end_index = raw_response.rfind("}")
|
|
|
|
if end_index != -1:
|
2024-12-06 18:15:47 +00:00
|
|
|
raw_response = raw_response[: end_index + 1]
|
2024-11-16 13:17:39 +00:00
|
|
|
response: Dict[str, Any] = json.loads(raw_response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def request_test_now(test_url: str) -> str:
|
|
|
|
api_url = "https://blocky.greatfire.org/api/test_now"
|
|
|
|
headers = {
|
|
|
|
"User-Agent": "bypasscensorship.org",
|
|
|
|
"Content-Type": "application/json;charset=utf-8",
|
|
|
|
"Pragma": "no-cache",
|
2024-12-06 18:15:47 +00:00
|
|
|
"Cache-Control": "no-cache",
|
2024-11-16 13:17:39 +00:00
|
|
|
}
|
|
|
|
request_count = 0
|
2024-11-29 18:43:56 +00:00
|
|
|
while request_count < 180:
|
2024-12-06 18:15:47 +00:00
|
|
|
params = {"url": test_url, "timestamp": str(int(time.time()))} # unix timestamp
|
|
|
|
response = requests.post(
|
|
|
|
api_url, params=params, headers=headers, json={}, timeout=30
|
|
|
|
)
|
2024-11-16 13:17:39 +00:00
|
|
|
response_data = clean_json_response(response.text)
|
|
|
|
print(f"Response: {response_data}")
|
|
|
|
if "url_test_id" in response_data.get("d", {}):
|
|
|
|
url_test_id: str = response_data["d"]["url_test_id"]
|
2024-12-06 18:15:47 +00:00
|
|
|
logging.debug(
|
|
|
|
"Test result for %s has test result ID %s", test_url, url_test_id
|
|
|
|
)
|
2024-11-16 13:17:39 +00:00
|
|
|
return url_test_id
|
|
|
|
request_count += 1
|
2024-11-29 18:43:56 +00:00
|
|
|
time.sleep(2)
|
2024-11-16 13:17:39 +00:00
|
|
|
raise RuntimeWarning("Exceeded timeout requesting result")
|
|
|
|
|
|
|
|
|
|
|
|
def request_test_result(url_test_id: str) -> int:
|
|
|
|
url = f"https://blocky.greatfire.org/api/url_test_result/{url_test_id}"
|
|
|
|
headers = {
|
|
|
|
"User-Agent": "bypasscensorship.org",
|
|
|
|
"Pragma": "no-cache",
|
2024-12-06 18:15:47 +00:00
|
|
|
"Cache-Control": "no-cache",
|
2024-11-16 13:17:39 +00:00
|
|
|
}
|
|
|
|
response = requests.get(url, headers=headers, timeout=30)
|
|
|
|
response_data = response.json()
|
|
|
|
tests = response_data.get("d", [])
|
2024-12-06 18:15:47 +00:00
|
|
|
non_zero_curl_exit_count: int = sum(
|
|
|
|
1 for test in tests if test.get("curl_exit_value") != "0"
|
|
|
|
)
|
|
|
|
logging.debug(
|
|
|
|
"Test result for %s has %s non-zero exit values",
|
|
|
|
url_test_id,
|
|
|
|
non_zero_curl_exit_count,
|
|
|
|
)
|
2024-11-16 13:17:39 +00:00
|
|
|
return non_zero_curl_exit_count
|
|
|
|
|
|
|
|
|
|
|
|
class BlockBlockyAutomation(BlockMirrorAutomation):
|
|
|
|
short_name = "block_blocky"
|
|
|
|
description = "Use the Blocky API to test for blocked mirrors in China"
|
2024-11-29 18:43:56 +00:00
|
|
|
frequency = 300
|
2024-11-16 13:17:39 +00:00
|
|
|
|
|
|
|
def fetch(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parse(self) -> None:
|
|
|
|
cn_proxies = (
|
|
|
|
db.session.query(Proxy.url)
|
|
|
|
.join(Origin, Proxy.origin_id == Origin.id)
|
|
|
|
.join(country_origin, Origin.id == country_origin.c.origin_id)
|
|
|
|
.join(Country, Country.id == country_origin.c.country_id)
|
|
|
|
.filter(
|
|
|
|
Country.country_code == "CN",
|
2024-11-29 18:43:56 +00:00
|
|
|
Proxy.url.is_not(None),
|
2024-11-16 13:17:39 +00:00
|
|
|
Proxy.deprecated.is_(None),
|
|
|
|
Proxy.destroyed.is_(None),
|
2024-12-06 18:15:47 +00:00
|
|
|
Proxy.pool_id != -1,
|
2024-11-16 13:17:39 +00:00
|
|
|
)
|
|
|
|
.all()
|
|
|
|
)
|
2024-11-16 13:27:29 +00:00
|
|
|
for row in cn_proxies:
|
|
|
|
proxy_url = row[0]
|
2024-11-16 13:17:39 +00:00
|
|
|
test_id = request_test_now(proxy_url)
|
|
|
|
if request_test_result(test_id) > 1:
|
|
|
|
self.patterns["blocky"].append(proxy_url)
|