fix: 心跳摘要改为在线 GetEncryptRequest 不再写死密钥
This commit is contained in:
@@ -5,7 +5,6 @@ import pytest
|
||||
from datetime import datetime
|
||||
|
||||
from euniv.autostudy import HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
from euniv.client import study_encryption
|
||||
from euniv.proxy import ProxyError, build_proxy_url, parse_proxy
|
||||
from euniv.study_window import CN_TZ, StudyWindowError, validate_start, window_open
|
||||
|
||||
@@ -166,31 +165,6 @@ def test_video_skip_already_complete_does_not_refresh() -> None:
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
def test_study_encryption_matches_get_encrypt_request() -> None:
|
||||
import json
|
||||
|
||||
body = json.dumps(
|
||||
{
|
||||
"knowledgeId": "6d16a38ebdfe455fbb1e86ec0c0c35d7",
|
||||
"masterId": "",
|
||||
"masterType": "",
|
||||
"packageId": "",
|
||||
"pageSize": 1,
|
||||
"studySize": 1,
|
||||
"studyTime": 120,
|
||||
"type": 1,
|
||||
"offLine": False,
|
||||
"end": False,
|
||||
"care": True,
|
||||
"deviceId": "",
|
||||
"studyChapterIds": "",
|
||||
"viewSchedule": 1,
|
||||
},
|
||||
separators=(",", ":"),
|
||||
)
|
||||
assert study_encryption(body) == "ba119ee448648d9f8f046d11c426afa4"
|
||||
|
||||
|
||||
class _Start:
|
||||
def __init__(self, day: int, hour: int = 16, minute: int = 0, second: int = 0) -> None:
|
||||
self.study_start_day = day
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import inspect
|
||||
|
||||
import httpx
|
||||
|
||||
from euniv import autostudy as autostudy_mod
|
||||
from euniv import client as client_mod
|
||||
from euniv.encrypt import (
|
||||
encrypt_path_from_js,
|
||||
encrypt_payload,
|
||||
fetch_encryption,
|
||||
note_play_html,
|
||||
parse_encrypt_response,
|
||||
reset_encrypt_config,
|
||||
)
|
||||
|
||||
|
||||
def test_encrypt_payload_wraps_progress_json_as_string() -> None:
|
||||
body = '{"knowledgeId":"abc","end":false}'
|
||||
assert encrypt_payload(body) == b'{"body":"{\\"knowledgeId\\":\\"abc\\",\\"end\\":false}"}'
|
||||
|
||||
|
||||
def test_parse_encrypt_response_accepts_wcf_shapes() -> None:
|
||||
digest = "a" * 32
|
||||
as_object = httpx.Response(200, json={"Status": "OK", "Data": digest})
|
||||
assert parse_encrypt_response(as_object) == digest
|
||||
|
||||
as_string = httpx.Response(200, json='{"Status":"OK","Data":"%s"}' % digest)
|
||||
assert parse_encrypt_response(as_string) == digest
|
||||
|
||||
quoted = httpx.Response(200, text="{'Status':'OK','Data':'%s'}" % digest)
|
||||
assert parse_encrypt_response(quoted) == digest
|
||||
|
||||
wrapped = httpx.Response(200, json={"d": '{"Status":"OK","Data":"%s"}' % digest})
|
||||
assert parse_encrypt_response(wrapped) == digest
|
||||
|
||||
live = httpx.Response(
|
||||
200,
|
||||
text='"{\'Status\':\'OK\',\'Data\':\'%s\'}"' % digest,
|
||||
headers={"content-type": "application/json; charset=utf-8"},
|
||||
)
|
||||
assert parse_encrypt_response(live) == digest
|
||||
|
||||
|
||||
def test_note_play_html_and_js_path() -> None:
|
||||
reset_encrypt_config()
|
||||
html = (
|
||||
'<script src="https://staticbdc1.yunxuetang.cn/SystemFiles/Js/'
|
||||
'kng_studytrack_rw.js?v=10.11.45"></script>'
|
||||
)
|
||||
note_play_html(html, "https://euniv.zte.com.cn")
|
||||
js = (
|
||||
'jQuery.ajax({type:"POST",contentType:"text/json",'
|
||||
'url:"/kng/services/KngComService.svc/GetEncryptRequest",data:arr})'
|
||||
)
|
||||
assert encrypt_path_from_js(js) == "/kng/services/KngComService.svc/GetEncryptRequest"
|
||||
|
||||
|
||||
def test_fetch_encryption_posts_player_body() -> None:
|
||||
reset_encrypt_config()
|
||||
seen: dict[str, bytes] = {}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
if "kng_studytrack_rw.js" in str(request.url):
|
||||
return httpx.Response(
|
||||
200,
|
||||
text='url:"/kng/services/KngComService.svc/GetEncryptRequest"',
|
||||
)
|
||||
seen["content_type"] = request.headers["content-type"].encode()
|
||||
seen["body"] = request.content
|
||||
return httpx.Response(200, json={"Status": "OK", "Data": "b" * 32})
|
||||
|
||||
with httpx.Client(transport=httpx.MockTransport(handler)) as client:
|
||||
digest = fetch_encryption(
|
||||
client,
|
||||
'{"knowledgeId":"k1"}',
|
||||
origin="https://euniv.zte.com.cn",
|
||||
user_agent="UA",
|
||||
)
|
||||
assert digest == "b" * 32
|
||||
assert seen["content_type"] == b"text/json"
|
||||
assert seen["body"] == b'{"body":"{\\"knowledgeId\\":\\"k1\\"}"}'
|
||||
|
||||
|
||||
def test_client_has_no_hardcoded_study_md5_key() -> None:
|
||||
for module in (client_mod, autostudy_mod):
|
||||
source = inspect.getsource(module)
|
||||
assert "60db8c0a-72d8-4b8f-bb45-c4c3f29a2696" not in source
|
||||
assert "STUDY_MD5_KEY" not in source
|
||||
assert "study_encryption" not in source
|
||||
Reference in New Issue
Block a user