Skip to content

Linux + iPhone Continuity / iMessage / SMS

stubbing out webext

Zack Bartel9c1b2e0897e1
diff --git a/Makefile b/Makefile
index 27f3bab..7dc9fc1 100644
--- a/Makefile
+++ b/Makefile
@@ -34,6 +34,10 @@ fmt:
@find ./src -not -path "*/build/*" \( -name "*.cpp" -o -name "*.hpp" \) -print0 | xargs -0 -r -n 1 clang-format -i
@echo "Done."
+.PHONY: extension
+extension:
+ @./extension/build.sh
+
clean:
rm -rf build
rm -f compile_commands.json
diff --git a/extension/README.md b/extension/README.md
new file mode 100644
index 0000000..9908e7a
--- /dev/null
+++ b/extension/README.md
@@ -0,0 +1,21 @@
+# Tether Extension
+
+This is the cross-platform WebExtension for Tether. The extension spans two different environments with distinct permission requirements:
+1. **Mail Clients (Thunderbird/Betterbird)**: Extracts OTPs from emails.
+2. **Web Browsers (Chrome/Firefox)**: Autofills extracted OTPs on webpages.
+
+Both extensions communicate with the local `tetherd` native daemon via standard Native Messaging APIs.
+
+## Directory Structure
+- `manifest-browser.json`: The Manifest V3 definition for Chrome/Firefox browsers.
+- `manifest-mail.json`: The Manifest V2/V3 definition for Thunderbird/Betterbird.
+- `src/background/`: Service worker / background scripts handling native messaging and state.
+- `src/content/`: Scripts injected into web pages to locate OTP input fields and fill them.
+- `src/mail/`: Thunderbird-specific scripts utilizing `messenger.*` APIs to parse emails for OTP codes.
+- `src/shared/`: Code shared across both environments (e.g., Native Messaging wrappers).
+- `host/`: Contains the Native Messaging Host JSON definition needed to register `tetherd` with browsers.
+
+## How it Works
+1. **In Thunderbird**: The extension monitors incoming emails or displayed messages. `src/mail/extractor.js` parses the text for an OTP and sends it through `src/shared/native.js` directly to the `tetherd` daemon.
+2. **The Daemon (`tetherd`)**: Acts as a state manager and vault.
+3. **In the Browser**: When you navigate to a page asking for an OTP, `src/content/autofill.js` recognizes the field and requests the OTP from `tetherd`. The daemon returns the OTP, which is then injected into the DOM.
diff --git a/extension/build.sh b/extension/build.sh
new file mode 100755
index 0000000..1914f6a
--- /dev/null
+++ b/extension/build.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+set -e
+
+echo "Building Tether Extensions..."
+
+# Ensure build directories exist
+BUILD_DIR="build/extension"
+BROWSER_DIR="$BUILD_DIR/browser"
+MAIL_DIR="$BUILD_DIR/mail"
+
+mkdir -p "$BROWSER_DIR/src/background" "$BROWSER_DIR/src/content"
+mkdir -p "$MAIL_DIR/src/background" "$MAIL_DIR/src/mail"
+
+# Bundle Browser Extension
+echo "Bundling browser extension..."
+npx esbuild extension/src/background/background.js --bundle --outfile="$BROWSER_DIR/src/background/background.js"
+npx esbuild extension/src/content/autofill.js --bundle --outfile="$BROWSER_DIR/src/content/autofill.js"
+cp extension/manifest-browser.json "$BROWSER_DIR/manifest.json"
+
+# Copy static assets safely
+if [ -d "extension/icons" ]; then cp -R extension/icons "$BROWSER_DIR/"; fi
+if [ -d "extension/popup" ]; then cp -R extension/popup "$BROWSER_DIR/"; fi
+
+(cd "$BROWSER_DIR" && zip -qr ../tether-browser-extension.zip .)
+
+# Bundle Mail Extension
+echo "Bundling mail extension..."
+npx esbuild extension/src/background/background.js --bundle --outfile="$MAIL_DIR/src/background/background.js"
+npx esbuild extension/src/mail/extractor.js --bundle --outfile="$MAIL_DIR/src/mail/extractor.js"
+cp extension/manifest-mail.json "$MAIL_DIR/manifest.json"
+
+if [ -d "extension/icons" ]; then cp -R extension/icons "$MAIL_DIR/"; fi
+if [ -d "extension/popup" ]; then cp -R extension/popup "$MAIL_DIR/"; fi
+
+(cd "$MAIL_DIR" && zip -qr ../tether-mail-extension.xpi .)
+
+echo "Extensions successfully built and packaged in $BUILD_DIR"
diff --git a/extension/host/com.tether.extension.json b/extension/host/com.tether.extension.json
new file mode 100644
index 0000000..69dcf35
--- /dev/null
+++ b/extension/host/com.tether.extension.json
@@ -0,0 +1,10 @@
+{
+ "name": "com.tether.extension",
+ "description": "Tether Native Messaging Host",
+ "path": "/usr/local/bin/tetherd",
+ "type": "stdio",
+ "allowed_origins": [
+ "chrome-extension://<EXTENSION-ID-HERE>/",
+ "moz-extension://<EXTENSION-ID-HERE>/"
+ ]
+}
diff --git a/extension/manifest-browser.json b/extension/manifest-browser.json
new file mode 100644
index 0000000..1692b44
--- /dev/null
+++ b/extension/manifest-browser.json
@@ -0,0 +1,39 @@
+{
+ "manifest_version": 3,
+ "name": "Tether Browser Extension",
+ "version": "1.0.0",
+ "description": "Autofills OTPs on webpages using Tether daemon",
+ "permissions": [
+ "nativeMessaging",
+ "activeTab",
+ "scripting",
+ "storage"
+ ],
+ "host_permissions": [
+ "<all_urls>"
+ ],
+ "background": {
+ "service_worker": "src/background/background.js",
+ "type": "module"
+ },
+ "content_scripts": [
+ {
+ "matches": ["<all_urls>"],
+ "js": ["src/content/autofill.js"],
+ "run_at": "document_idle"
+ }
+ ],
+ "action": {
+ "default_popup": "popup/index.html",
+ "default_icon": {
+ "16": "icons/icon16.png",
+ "48": "icons/icon48.png",
+ "128": "icons/icon128.png"
+ }
+ },
+ "icons": {
+ "16": "icons/icon16.png",
+ "48": "icons/icon48.png",
+ "128": "icons/icon128.png"
+ }
+}
diff --git a/extension/manifest-mail.json b/extension/manifest-mail.json
new file mode 100644
index 0000000..03c8784
--- /dev/null
+++ b/extension/manifest-mail.json
@@ -0,0 +1,33 @@
+{
+ "manifest_version": 2,
+ "name": "Tether Mail Extension",
+ "version": "1.0.0",
+ "description": "Extracts OTPs from emails for Tether daemon",
+ "browser_specific_settings": {
+ "gecko": {
+ "strict_min_version": "115.0"
+ }
+ },
+ "permissions": [
+ "nativeMessaging",
+ "messagesRead",
+ "storage"
+ ],
+ "background": {
+ "scripts": ["src/background/background.js", "src/mail/extractor.js"]
+ },
+ "browser_action": {
+ "default_popup": "popup/index.html",
+ "default_icon": {
+ "16": "icons/icon16.png",
+ "48": "icons/icon48.png",
+ "128": "icons/icon128.png"
+ }
+ },
+ "icons": {
+ "16": "icons/icon16.png",
+ "48": "icons/icon48.png",
+ "128": "icons/icon128.png"
+ }
+}
diff --git a/extension/src/background/background.js b/extension/src/background/background.js
new file mode 100644
index 0000000..da21517
--- /dev/null
+++ b/extension/src/background/background.js
@@ -0,0 +1,35 @@
+// Background worker script
+// This runs in the background of the browser or mail client
+
+// Import the native messaging module if supported (MV3 modules)
+// For MV2 (Thunderbird), we might need to load this differently or bundle it.
+import { connectToNativeHost, sendToNativeHost } from '../shared/native.js';
+
+let nativePort = connectToNativeHost();
+
+// Listen for messages from content scripts or the mail extractor
+if (typeof chrome !== 'undefined' && chrome.runtime) {
+ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
+ if (request.action === "found_otp_in_email") {
+ console.log("Found OTP in email, sending to Tether daemon:", request.otp);
+ // Send the extracted OTP to the C++ daemon
+ sendToNativeHost({
+ type: "new_otp",
+ otp: request.otp,
+ source: request.source
+ });
+ sendResponse({ status: "sent_to_daemon" });
+ }
+
+ if (request.action === "request_otp_for_site") {
+ console.log("Content script requesting OTP for:", request.url);
+ // Query the C++ daemon for an OTP
+ sendToNativeHost({
+ type: "request_otp",
+ url: request.url
+ });
+ sendResponse({ status: "requested" });
+ }
+ return true;
+ });
+}
diff --git a/extension/src/content/autofill.js b/extension/src/content/autofill.js
new file mode 100644
index 0000000..894e2cd
--- /dev/null
+++ b/extension/src/content/autofill.js
@@ -0,0 +1,45 @@
+// Content script for browsers (Chrome/Firefox)
+// This runs in the context of webpages and looks for OTP input fields
+
+function findOtpFields() {
+ const inputs = document.querySelectorAll('input[type="text"], input[type="number"], input[autocomplete="one-time-code"]');
+ const otpFields = [];
+
+ for (const input of inputs) {
+ const name = (input.name || '').toLowerCase();
+ const id = (input.id || '').toLowerCase();
+
+ if (name.includes('otp') || id.includes('otp') || name.includes('code') || input.autocomplete === 'one-time-code') {
+ otpFields.push(input);
+ }
+ }
+
+ return otpFields;
+}
+
+// Request OTP from daemon when we detect OTP fields
+const fields = findOtpFields();
+if (fields.length > 0) {
+ console.log("Found potential OTP fields on page, requesting OTP from Tether");
+
+ chrome.runtime.sendMessage({
+ action: "request_otp_for_site",
+ url: window.location.hostname
+ });
+}
+
+// Listen for OTPs sent from the daemon
+chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
+ if (request.action === "fill_otp" && request.otp) {
+ const fields = findOtpFields();
+ if (fields.length > 0) {
+ console.log("Autofilling OTP:", request.otp);
+ // Basic autofill logic
+ fields[0].value = request.otp;
+
+ // Dispatch events so React/Vue/Angular pick up the change
+ fields[0].dispatchEvent(new Event('input', { bubbles: true }));
+ fields[0].dispatchEvent(new Event('change', { bubbles: true }));
+ }
+ }
+});
diff --git a/extension/src/mail/extractor.js b/extension/src/mail/extractor.js
new file mode 100644
index 0000000..336d8cf
--- /dev/null
+++ b/extension/src/mail/extractor.js
@@ -0,0 +1,45 @@
+// Mail script for Thunderbird/Betterbird
+// This script extracts OTP codes from emails
+
+// This example assumes Manifest V2/V3 background script using the WebExtension Mail APIs
+// (messenger.messages, messenger.mailTabs)
+
+if (typeof messenger !== 'undefined') {
+ console.log("Tether Mail Extractor loaded in Thunderbird/Betterbird");
+
+ // Listen for new messages being displayed
+ messenger.messageDisplay.onMessageDisplayed.addListener(async (tabId, message) => {
+ // Get the full message body
+ const full = await messenger.messages.getFull(message.id);
+ const bodyText = extractTextFromParts(full.parts);
+
+ // Simple regex to find 6-8 digit OTP codes
+ const otpRegex = /\b\d{6,8}\b/g;
+ const matches = bodyText.match(otpRegex);
+
+ if (matches && matches.length > 0) {
+ const otp = matches[0]; // Simplistic approach: take the first one
+ console.log("Found OTP in email:", otp);
+
+ // Send it to the background script, which forwards it to the native daemon
+ browser.runtime.sendMessage({
+ action: "found_otp_in_email",
+ otp: otp,
+ source: message.subject
+ });
+ }
+ });
+
+ function extractTextFromParts(parts) {
+ let text = "";
+ if (!parts) return text;
+ for (const part of parts) {
+ if (part.contentType === "text/plain" && part.body) {
+ text += part.body + "\n";
+ } else if (part.parts) {
+ text += extractTextFromParts(part.parts);
+ }
+ }
+ return text;
+ }
+}
diff --git a/extension/src/shared/native.js b/extension/src/shared/native.js
new file mode 100644
index 0000000..20cd1be
--- /dev/null
+++ b/extension/src/shared/native.js
@@ -0,0 +1,49 @@
+// Native messaging connection logic
+let port = null;
+
+export function connectToNativeHost() {
+ const hostName = "com.tether.extension";
+ if (typeof browser !== 'undefined' && browser.runtime && browser.runtime.connectNative) {
+ port = browser.runtime.connectNative(hostName);
+ } else if (typeof chrome !== 'undefined' && chrome.runtime && chrome.runtime.connectNative) {
+ port = chrome.runtime.connectNative(hostName);
+ } else {
+ console.error("Native messaging not supported in this environment");
+ return null;
+ }
+
+ port.onMessage.addListener((message) => {
+ console.log("Received message from Tether daemon:", message);
+ handleNativeMessage(message);
+ });
+
+ port.onDisconnect.addListener(() => {
+ console.log("Disconnected from Tether daemon");
+ port = null;
+ // Implement reconnection logic if needed
+ });
+
+ return port;
+}
+
+export function sendToNativeHost(message) {
+ if (port) {
+ port.postMessage(message);
+ } else {
+ console.error("Not connected to Tether daemon");
+ }
+}
+
+function handleNativeMessage(message) {
+ // Dispatch native messages to other parts of the extension
+ if (message.type === "otp_available") {
+ // Notify content scripts or popup
+ if (typeof chrome !== 'undefined' && chrome.tabs) {
+ chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
+ if (tabs[0]) {
+ chrome.tabs.sendMessage(tabs[0].id, { action: "fill_otp", otp: message.otp });
+ }
+ });
+ }
+ }
+}