feat: add configurable delayed password relogin
This commit is contained in:
@@ -33,19 +33,42 @@ def sdk_headers(device):
|
||||
|
||||
|
||||
class PasswordLogin:
|
||||
def __init__(self, catalog, device, *, sdk_url=SDK_LOGIN_URL, center_url=None, channel="DYD", trace=None):
|
||||
def __init__(self, catalog, device, *, sdk_url=SDK_LOGIN_URL, center_url=None, channel="DYD", trace=None,
|
||||
cancel_guard=None):
|
||||
self.catalog, self.device = catalog, device
|
||||
self.trace = trace
|
||||
self.sdk_url = sdk_url
|
||||
self.center_url = center_url or catalog.data["server_info"]["ANDROID"].get(channel, DEFAULT_CENTER)
|
||||
self.channel = channel
|
||||
self.cancel_guard = cancel_guard
|
||||
|
||||
def _check_cancel(self):
|
||||
if self.cancel_guard is not None:
|
||||
self.cancel_guard()
|
||||
|
||||
def _guard_http(self, http):
|
||||
if self.cancel_guard is None:
|
||||
return
|
||||
|
||||
def guarded(method):
|
||||
def dispatch(*args, **kwargs):
|
||||
self._check_cancel()
|
||||
return method(*args, **kwargs)
|
||||
return dispatch
|
||||
|
||||
# Requests may spend time encoding/preparing after TraceLog.begin.
|
||||
# Guard send too, including redirected SDK/center requests.
|
||||
for name in ("post", "send"):
|
||||
setattr(http, name, guarded(getattr(http, name)))
|
||||
|
||||
def authenticate(self, account, password):
|
||||
self._check_cancel()
|
||||
if not account.strip() or not password:
|
||||
raise ApiError("请填写账号和密码")
|
||||
# Cookies remain in this process and are discarded on closing the SDK session.
|
||||
with requests.Session() as http:
|
||||
http.trust_env = False
|
||||
self._guard_http(http)
|
||||
payload = {"account": account.strip(), "password": password}
|
||||
params = {"versionCode": "1.0.1", "buildNumber": "2016122900", "appChannel": "taptap"}
|
||||
headers = sdk_headers(self.device)
|
||||
@@ -55,6 +78,7 @@ class PasswordLogin:
|
||||
try:
|
||||
response = http.post(self.sdk_url, data=payload, params=params,
|
||||
headers=headers, timeout=(5, 15), allow_redirects=False)
|
||||
self._check_cancel()
|
||||
try:
|
||||
data = response.json()
|
||||
except ValueError as error:
|
||||
@@ -100,8 +124,11 @@ class PasswordLogin:
|
||||
uid, sdk_token = str(users[0]["userId"]), str(users[0]["token"])
|
||||
client = Client(Session(key=self.catalog.data["game_setting"].get("xyz1", Session().key)), trace=self.trace)
|
||||
try:
|
||||
self._guard_http(client.http)
|
||||
self._check_cancel()
|
||||
game = client.sdk_login(self.center_url, uid, sdk_token, self.channel, "ANDROID", self.device,
|
||||
tj_channel=self.catalog.data.get("tjchannel", {}).get("tjchannel", "unknow"))
|
||||
self._check_cancel()
|
||||
except Exception:
|
||||
client.close()
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user