From be0e5a80104b3b07bfdee8a7af33baf03dcce2c5 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Mon, 11 May 2026 03:46:23 +0800 Subject: [PATCH] Make live IBKR 2FA autofill target focused input --- 2fa_bot.py | 20 +++++++++++--------- tests/test_docker_compose_ports.sh | 2 ++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/2fa_bot.py b/2fa_bot.py index a2c8ddf..a349800 100644 --- a/2fa_bot.py +++ b/2fa_bot.py @@ -24,7 +24,7 @@ TYPE_DELAY_MS = 100 PRE_ENTER_DELAY = 1 XDOTOOL_TIMEOUT = 10 -MIN_TOTP_SECONDS_REMAINING = 8 +MIN_TOTP_SECONDS_REMAINING = 15 MAX_AUTOFILL_SUBMISSIONS_RAW = os.environ.get("IBKR_2FA_MAX_SUBMISSIONS", "1") # Window titles to search for 2FA prompts. Live IBKR accounts can show mobile @@ -207,6 +207,15 @@ def focus_input_area(candidate): run_xdotool(["windowfocus", "--sync", candidate.window_id]) run_xdotool(["mousemove", "--window", candidate.window_id, str(x), str(y)]) run_xdotool(["click", "1"]) + time.sleep(0.2) + + +def type_totp_into_active_window(code): + """Type into the focused control; Java dialogs can ignore direct window events.""" + run_xdotool(["key", "ctrl+a", "BackSpace"]) + run_xdotool(["type", "--delay", str(TYPE_DELAY_MS), code], sensitive=True) + time.sleep(PRE_ENTER_DELAY) + run_xdotool(["key", "Return"]) def submit_totp(candidate): @@ -246,14 +255,7 @@ def submit_totp(candidate): focus_input_area(candidate) code = get_totp() - - run_xdotool(["key", "--window", candidate.window_id, "ctrl+a", "BackSpace"]) - run_xdotool( - ["type", "--window", candidate.window_id, "--delay", str(TYPE_DELAY_MS), code], - sensitive=True, - ) - time.sleep(PRE_ENTER_DELAY) - run_xdotool(["key", "--window", candidate.window_id, "Return"]) + type_totp_into_active_window(code) autofill_submission_count += 1 log.info("Auto-fill submitted, waiting for gateway response...") diff --git a/tests/test_docker_compose_ports.sh b/tests/test_docker_compose_ports.sh index 9c43401..1c597e6 100644 --- a/tests/test_docker_compose_ports.sh +++ b/tests/test_docker_compose_ports.sh @@ -43,3 +43,5 @@ grep -Fq ' - IB_GATEWAY_CONC_GC_THREADS=${IB_GATEWAY_CONC_GC_THREADS:-1}' " grep -Fq ' - ACCEPT_API_FROM_IP=${ACCEPT_API_FROM_IP:?Set ACCEPT_API_FROM_IP to your Cloud Run egress subnet or connector CIDR}' "$compose_file" grep -Fq 'INPUT_CLICK_POSITION = (0.50, 0.40)' "$repo_dir/2fa_bot.py" +grep -Fq 'MIN_TOTP_SECONDS_REMAINING = 15' "$repo_dir/2fa_bot.py" +grep -Fq 'def type_totp_into_active_window(code):' "$repo_dir/2fa_bot.py"