Linux + iPhone Continuity / iMessage / SMS
OTP autofill improvements
Zack Bartelb5bd89febfbd
diff --git a/extension/src/background/background.js b/extension/src/background/background.jsindex 4d5a886..1cf249e 100644--- a/extension/src/background/background.js+++ b/extension/src/background/background.js@@ -30,6 +30,11 @@ if (typeof chrome !== 'undefined' && chrome.runtime) {});sendResponse({ status: "requested" });}++ if (request.action === "consume_otp") {+ sendToNativeHost({ command: "consume_otp" });+ sendResponse({ status: "consumed" });+ }return true;});}diff --git a/extension/src/content/autofill.js b/extension/src/content/autofill.jsindex 4afaddf..afb9a3f 100644--- a/extension/src/content/autofill.js+++ b/extension/src/content/autofill.js@@ -103,19 +103,27 @@ if (typeof document === 'undefined') {let otpInterval = null;function requestOtp() {- chrome.runtime.sendMessage({- action: "request_otp_for_site",- url: window.location.hostname+ chrome.runtime.sendMessage({+ action: "request_otp_for_site",+ url: window.location.hostname});}+// Tell the daemon the code was used so it isn't re-served to the next page.+function consumeOtp() {+ chrome.runtime.sendMessage({ action: "consume_otp" });+}+function hasOtpFields() {if (detectSplitOtp(document)) return true;return findOtpInputs(document).length > 0;}// Check every 2 seconds for up to 2 minutes-if (hasOtpFields()) {+let otpFlowStarted = false;+function startOtpFlow() {+ if (otpFlowStarted) return;+ otpFlowStarted = true;console.log("Found potential OTP fields on page, requesting OTP from Tether");requestOtp();@@ -126,8 +134,8 @@ if (hasOtpFields()) {const regular = findOtpInputs(document);// Stop trying after 2 minutes or if the user manually filled the field- if (attempts > 60 ||- (splits && splits[0].value !== "") ||+ if (attempts > 60 ||+ (splits && splits[0].value !== "") ||(regular.length > 0 && regular[0].value.length > 3)) {clearInterval(otpInterval);return;@@ -136,6 +144,22 @@ if (hasOtpFields()) {}, 2000);}+if (hasOtpFields()) {+ startOtpFlow();+} else {+ // The OTP field often renders after load (SPA route, or after the user enters+ // their email/phone). Watch for it instead of only checking once at load.+ const observer = new MutationObserver(() => {+ if (hasOtpFields()) {+ observer.disconnect();+ startOtpFlow();+ }+ });+ observer.observe(document.documentElement, { childList: true, subtree: true });+ // ponytail: bound the watcher so it doesn't run forever on non-OTP pages+ setTimeout(() => observer.disconnect(), 120000);+}+// Listen for OTPs sent from the daemonchrome.runtime.onMessage.addListener((request, sender, sendResponse) => {if (request.action === "fill_otp" && request.otp) {@@ -152,6 +176,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {splitFields[i].dispatchEvent(new Event('change', { bubbles: true }));}if (otpInterval) clearInterval(otpInterval);+ consumeOtp();} else {const regularFields = findOtpInputs(document);// Fill when empty, or overwrite a stale/partial value — but skip if the code@@ -166,6 +191,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {// Clean up the intervalif (otpInterval) clearInterval(otpInterval);+ consumeOtp();}}}diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex a56cafa..a594f9d 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -15,6 +15,7 @@#include "tether/discovery.hpp"#include "tether/file_transfer.hpp"#include "tether/wayland.hpp"+#include <chrono>#include <cstring>#include <filesystem>#include <fstream>@@ -55,6 +56,9 @@ namespace tether {static constexpr size_t kMaxRecentReceivedFiles = 16;static std::map<int, ConnectedClientSnapshot> connected_remote_clients;static std::string g_current_otp;+ static std::chrono::steady_clock::time_point g_otp_set_at;+ // 5-min TTL backstop; consume-on-fill is the primary guard+ static constexpr auto kOtpTtl = std::chrono::minutes(5);void register_client_fd(int fd) { active_sessions[fd] = {fd, nullptr}; }@@ -71,6 +75,14 @@ namespace tether {local_subscribers.insert(fd);}+ // Returns the stored OTP, clearing it first if it has aged past the TTL.+ static std::string current_otp_if_fresh() {+ if (!g_current_otp.empty() && std::chrono::steady_clock::now() - g_otp_set_at >= kOtpTtl) {+ g_current_otp.clear();+ }+ return g_current_otp;+ }+void unregister_local_subscriber(int fd) {std::lock_guard<std::mutex> lock(g_subscribers_mutex);local_subscribers.erase(fd);@@ -387,6 +399,19 @@ namespace tether {if (write(client_fd, payload.c_str(), payload.size()) < 0) {debug::log(ERR, "net write error\n");}+ // Replay any current OTP so a freshly reconnected extension+ // (MV3 service worker restart) gets it without waiting for a+ // new_otp push it may have missed while disconnected.+ std::string otp = current_otp_if_fresh();+ if (!otp.empty()) {+ nlohmann::json event;+ event["command"] = "otp_available";+ event["otp"] = otp;+ std::string otp_payload = event.dump() + "\n";+ if (write(client_fd, otp_payload.c_str(), otp_payload.size()) < 0) {+ debug::log(ERR, "net write error\n");+ }+ }continue;} else if (j.contains("command") && j["command"] == "unsubscribe") {unregister_local_subscriber(client_fd);@@ -418,6 +443,7 @@ namespace tether {}} else if (j.contains("command") && j["command"] == "new_otp" && j.contains("otp")) {g_current_otp = j["otp"];+ g_otp_set_at = std::chrono::steady_clock::now();// Push the code to local subscribers (browser extension, GTK, etc.)// so the browser fills it without depending on its polling timing.nlohmann::json event;@@ -432,12 +458,20 @@ namespace tether {} else if (j.contains("command") && j["command"] == "request_otp") {nlohmann::json resp;resp["command"] = "otp_available";- resp["otp"] = g_current_otp;+ resp["otp"] = current_otp_if_fresh();std::string payload = resp.dump() + "\n";if (write(client_fd, payload.c_str(), payload.size()) < 0) {debug::log(ERR, "net write error\n");}continue;+ } else if (j.contains("command") && j["command"] == "consume_otp") {+ // The extension filled the code; clear it so it isn't re-served.+ g_current_otp.clear();+ std::string payload = "{\"status\":\"ok\"}\n";+ if (write(client_fd, payload.c_str(), payload.size()) < 0) {+ debug::log(ERR, "net write error\n");+ }+ continue;} else if (j.contains("command") && j["command"] == "file_start") {if (broadcast_tcp_message(msg) == 0) {nlohmann::json resp;@@ -845,6 +879,7 @@ namespace tether {// OTP sent from a mobile client (iPhone Share Extension) over mTLS.// Store it in the global vault so the browser extension can retrieve it.g_current_otp = j["otp"].get<std::string>();+ g_otp_set_at = std::chrono::steady_clock::now();// Notify local subscribers (GTK, browser ext, etc.) using the// otp_available shape the browser extension listens for.nlohmann::json event;