Skip to content

Commit 46abbfc

Browse files
authored
Make live IBKR 2FA autofill target focused input (#47)
1 parent 67e2eb4 commit 46abbfc

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

2fa_bot.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
TYPE_DELAY_MS = 100
2525
PRE_ENTER_DELAY = 1
2626
XDOTOOL_TIMEOUT = 10
27-
MIN_TOTP_SECONDS_REMAINING = 8
27+
MIN_TOTP_SECONDS_REMAINING = 15
2828
MAX_AUTOFILL_SUBMISSIONS_RAW = os.environ.get("IBKR_2FA_MAX_SUBMISSIONS", "1")
2929

3030
# Window titles to search for 2FA prompts. Live IBKR accounts can show mobile
@@ -207,6 +207,15 @@ def focus_input_area(candidate):
207207
run_xdotool(["windowfocus", "--sync", candidate.window_id])
208208
run_xdotool(["mousemove", "--window", candidate.window_id, str(x), str(y)])
209209
run_xdotool(["click", "1"])
210+
time.sleep(0.2)
211+
212+
213+
def type_totp_into_active_window(code):
214+
"""Type into the focused control; Java dialogs can ignore direct window events."""
215+
run_xdotool(["key", "ctrl+a", "BackSpace"])
216+
run_xdotool(["type", "--delay", str(TYPE_DELAY_MS), code], sensitive=True)
217+
time.sleep(PRE_ENTER_DELAY)
218+
run_xdotool(["key", "Return"])
210219

211220

212221
def submit_totp(candidate):
@@ -246,14 +255,7 @@ def submit_totp(candidate):
246255

247256
focus_input_area(candidate)
248257
code = get_totp()
249-
250-
run_xdotool(["key", "--window", candidate.window_id, "ctrl+a", "BackSpace"])
251-
run_xdotool(
252-
["type", "--window", candidate.window_id, "--delay", str(TYPE_DELAY_MS), code],
253-
sensitive=True,
254-
)
255-
time.sleep(PRE_ENTER_DELAY)
256-
run_xdotool(["key", "--window", candidate.window_id, "Return"])
258+
type_totp_into_active_window(code)
257259

258260
autofill_submission_count += 1
259261
log.info("Auto-fill submitted, waiting for gateway response...")

tests/test_docker_compose_ports.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ grep -Fq ' - IB_GATEWAY_CONC_GC_THREADS=${IB_GATEWAY_CONC_GC_THREADS:-1}' "
4343
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"
4444

4545
grep -Fq 'INPUT_CLICK_POSITION = (0.50, 0.40)' "$repo_dir/2fa_bot.py"
46+
grep -Fq 'MIN_TOTP_SECONDS_REMAINING = 15' "$repo_dir/2fa_bot.py"
47+
grep -Fq 'def type_totp_into_active_window(code):' "$repo_dir/2fa_bot.py"

0 commit comments

Comments
 (0)