Skip to content

Commit ec0f70e

Browse files
authored
feat(client): get_cookie (#25)
* Fix a bug * add get_pskey(maybe) * add csrf_token
1 parent f1617e2 commit ec0f70e

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lagrange/client/client.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
from lagrange.info import AppInfo, DeviceInfo, SigInfo
1717
from lagrange.pb.message.msg_push import MsgPushBody
1818
from lagrange.pb.message.send import SendMsgRsp
19-
from lagrange.pb.service.comm import SendGrpBotHD, SendNudge
19+
from lagrange.pb.service.comm import (
20+
GetClientKeyRsp,
21+
GetCookieRsp,
22+
SendGrpBotHD,
23+
SendNudge,
24+
)
2025
from lagrange.pb.service.friend import (
2126
GetFriendListRsp,
2227
GetFriendListUin,
@@ -52,6 +57,7 @@
5257
from lagrange.pb.service.oidb import OidbRequest, OidbResponse
5358
from lagrange.pb.highway.comm import IndexNode
5459
from lagrange.utils.binary.protobuf import proto_decode, proto_encode
60+
from lagrange.utils.httpcat import HttpCat
5561
from lagrange.utils.log import log
5662
from lagrange.utils.operator import timestamp
5763

@@ -593,3 +599,40 @@ async def get_group_last_seq(self, grp_id: int) -> int:
593599
if not rsp.body.args.seq:
594600
raise AssertionError("No message found")
595601
return rsp.body.args.seq
602+
603+
async def _get_client_key(self) -> str:
604+
return GetClientKeyRsp.decode(
605+
(await self.send_oidb_svc(0x102A, 1, proto_encode({}))).data
606+
).client_key
607+
608+
def _gtk_1(self, skey_or_pskey: str):
609+
_hash = 5381
610+
_len = len(skey_or_pskey)
611+
for i in range(_len):
612+
_hash += (_hash << 5) + ord(skey_or_pskey[i])
613+
return _hash & 2147483647
614+
615+
async def get_cookies(self, domains: list[str]) -> List[str]:
616+
"""pskey"""
617+
return [
618+
i.value.decode()
619+
for i in GetCookieRsp.decode(
620+
(
621+
await self.send_oidb_svc(
622+
0x102A,
623+
0,
624+
proto_encode({1: domains}), # type: ignore
625+
)
626+
).data
627+
).urls
628+
]
629+
630+
async def get_skey(self) -> str:
631+
jump = "https%3A%2F%2Fh5.qzone.qq.com%2Fqqnt%2Fqzoneinpcqq%2Ffriend%3Frefresh%3D0%26clientuin%3D0%26darkMode%3D0&keyindex=19&random=2599"
632+
url = f"https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin={self.uin}&clientkey={await self._get_client_key()}&u1={jump}"
633+
resp = await HttpCat.request("GET", url, follow_redirect=False)
634+
return resp.cookies["skey"]
635+
636+
async def get_csrf_token(self) -> int:
637+
skey = await self.get_skey()
638+
return self._gtk_1(skey)

lagrange/pb/service/comm.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ class SendGrpBotHD(ProtoStruct):
2121
IDD: int = proto_field(7, default=0)
2222
grp_id: int = proto_field(8, default=None)
2323
grp_type: int = proto_field(9, default=0) # 0guild 1grp 2C2C(need grp_id==None)
24+
25+
26+
class Propertys(ProtoStruct):
27+
key: str = proto_field(1)
28+
value: bytes = proto_field(2)
29+
30+
31+
class GetCookieRsp(ProtoStruct):
32+
urls: list[Propertys] = proto_field(1)
33+
34+
35+
class GetClientKeyRsp(ProtoStruct):
36+
f2: int = proto_field(2)
37+
client_key: str = proto_field(3)
38+
expiration: int = proto_field(4)

0 commit comments

Comments
 (0)