Skip to content

Commit

Permalink
Fix:由于tk的线程不安全性无法进行两步验证的问题
Browse files Browse the repository at this point in the history
Signed-off-by: Sadam·Sadik <[email protected]>
  • Loading branch information
Haoke98 committed Feb 28, 2024
1 parent 87da897 commit 815dfc1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
9 changes: 7 additions & 2 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, tk_ctx, apple_id,
verify=True,
client_id=None,
with_family=True, ):
self.tk_ctx = tk_ctx
if china_account:
self.HOME_ENDPOINT = "https://www.icloud.com.cn"
self.SETUP_ENDPOINT = "https://setup.icloud.com.cn/setup/ws/1"
Expand All @@ -42,8 +43,12 @@ def __init__(self, tk_ctx, apple_id,
def two_factor_authenticate(self):
if self.requires_2fa:
logging.warning("Two-factor authentication required.")
# 第一步:向用户请求输入第一个验证代码
verification_code = simpledialog.askstring("Two Factor Authentication", "Enter Verification Code 1:")
# 第一步:向用户请求输入第一个验证代码 (由于tk的线程不安全性, 必须在主线程上运行, 所以需要用线程通信和间接触发的形式转交给主线程处理)
# 使用主线程的方法来获取用户输入
self.tk_ctx.event_generate('<<Request2FA>>', when="tail")
# 等待用户输入的结果
verification_code = self.tk_ctx.authCodeQueue.get() # This will block until the item is available
logging.info("Verification Code: {}".format(verification_code))
result = self.validate_2fa_code(verification_code)
logging.info("Code validation result: %s" % result)

Expand Down
4 changes: 2 additions & 2 deletions frames/image_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def cache_assets_thumb(self):
while True:
dlt = datetime.datetime.now() - createdAt
if len(self.assets) == 0:
logging.info("Waiting for assets to be set[{}]....{}".format(len(self.assets), dlt))
logging.debug("Waiting for assets to be set[{}]....{}".format(len(self.assets), dlt))
time.sleep(2)
else:
for i, asset_info in enumerate(self.assets):
Expand Down Expand Up @@ -137,7 +137,7 @@ def update_labels(self):
b = 0
if len(self.assets) == 0:
dlt = datetime.datetime.now() - createdAt
logging.info("Waiting for assets to be set....{} ".format(dlt))
logging.debug("Waiting for assets to be set....{} ".format(dlt))
time.sleep(5)
continue
for i, asset_info in enumerate(self.assets):
Expand Down
16 changes: 15 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
@disc:
======================================="""
import logging
import queue
import tkinter as tk
from tkinter import simpledialog

import click

Expand All @@ -28,10 +30,13 @@ def __init__(self):
super().__init__()
self.iService = None
self.db_conn, self.db_cursor = init_database()
self.authCodeQueue = queue.Queue()

# 绑定到主窗口中的某处初始化代码,比如__init__
self.bind('<<Request2FA>>', lambda e: self.handle_2fa_request())
self.page_home = HomePage(self, MOCK_ACTIVE)
self.page_login = LoginPage(self)
self.title("PyICloudClient")

self.show_login_page()

def reset_size(self, width: int, height: int):
Expand Down Expand Up @@ -67,6 +72,15 @@ def logout(self):
self.page_home.pack_forget() # 关闭主页窗口
self.show_login_page()

def handle_2fa_request(self):
"""
两步验证处理方法(由于tk的线程不安全性, 必须在主线程上运行)
:return:
"""
verification_code = simpledialog.askstring("Two Factor Authentication", "Enter Verification Code:")
logging.info("Verification Code: {}".format(verification_code))
self.authCodeQueue.put(verification_code) # Put the user input back into the queue


@click.command()
@click.option("--mock", is_flag=True, default=False, help="Open the mock mode.")
Expand Down
2 changes: 1 addition & 1 deletion pages/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def async_update_view(self):
# 设置登陆超时
max_dlt = 60 * 60 * 60
compare_result = dlt > max_dlt
print(f"正在登陆.....{dlt}s, 超时:{compare_result}")
# logging.debug(f"正在登陆.....{dlt}s, 超时:{compare_result}")
self.message_label.config(text="")

def show(self):
Expand Down

0 comments on commit 815dfc1

Please sign in to comment.