Linux + iPhone Continuity / iMessage / SMS
fix pairing bug
Zack Bartel9868d2937b20
diff --git a/apple/Tether/Protocol/TetherMessage.swift b/apple/Tether/Protocol/TetherMessage.swiftindex a4b3696..277a7c6 100644--- a/apple/Tether/Protocol/TetherMessage.swift+++ b/apple/Tether/Protocol/TetherMessage.swift@@ -24,6 +24,7 @@ enum TetherCommand: String, Codable, Sendable {case clipboardContent = "clipboard_content"case fileStatus = "file_status"case pairPending = "pair_pending"+ case pairAccepted = "pair_accepted"case error = "error"}@@ -82,6 +83,11 @@ extension TetherMessage {TetherMessage(command: TetherCommand.pairRequest.rawValue, deviceName: deviceName)}+ /// Create a `pair_accepted` message (replying to a request).+ static var pairAccepted: TetherMessage {+ TetherMessage(command: TetherCommand.pairAccepted.rawValue)+ }+/// Create a `file_start` message.static func fileStart(filename: String, size: Int64, transferId: String) -> TetherMessage {TetherMessage(diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swiftindex 3b3c4a2..76abbc2 100644--- a/apple/Tether/ViewModels/TetherViewModel.swift+++ b/apple/Tether/ViewModels/TetherViewModel.swift@@ -610,11 +610,9 @@ final class TetherViewModel {let name = connectedDeviceName ?? "Desktop"certificateManager.addKnownHost(fingerprint: serverFP, name: name)- // If we received an inbound pair_request, we must respond back with pair_accepted!- // But the original tether daemon protocol relies on disconnecting if rejected, and- // the client isn't expected to send pair_accepted, the desktop daemon sends pair_accepted.- // Let's send a simple OK or just leave it. If the connection drops we assume rejected.- // Actually, let's just save it.+ // Let's send pair_accepted to tell the daemon we successfully paired!+ connection.send(.pairAccepted)+showPairingSheet = falseappState = .connected}diff --git a/src/core/include/tether/client.hpp b/src/core/include/tether/client.hppindex 5d897eb..39870cf 100644--- a/src/core/include/tether/client.hpp+++ b/src/core/include/tether/client.hpp@@ -18,6 +18,7 @@ namespace tether {// Base Level payload loopstd::string send_and_wait(const std::string& payload);+ std::string get_peer_fingerprint() const;// High Level Featuresstd::string get_clipboard(std::string& err_out);diff --git a/src/core/src/client.cpp b/src/core/src/client.cppindex bd6e6e4..dbe98f8 100644--- a/src/core/src/client.cpp+++ b/src/core/src/client.cpp@@ -54,6 +54,11 @@ bool Client::is_connected() const {return sock_ >= 0;}+std::string Client::get_peer_fingerprint() const {+ if (!ssl_) return "";+ return Crypto::get_peer_fingerprint(ssl_);+}+int Client::connect_unix(bool retry) {int sock = socket(AF_UNIX, SOCK_STREAM, 0);if (sock < 0) return -1;diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex dd53e0d..0bb0a1e 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -470,8 +470,23 @@ namespace tether {char hostname[256] = {};gethostname(hostname, sizeof(hostname) - 1);std::string err;- remote.pair(hostname, err);- // The user must accept on the remote side, and then the GTK app handles it.+ std::string response = remote.pair(hostname, err);++ if (response.find("\"pair_accepted\"") != std::string::npos) {+ std::string print = remote.get_peer_fingerprint();+ if (!print.empty()) {+ // The remote device accepted! Let's save its fingerprint.+ // Since we dialed outbound, we don't necessarily know its mDNS name here+ // But GTK can resolve it. For now let's just use the host IP/name.+ Crypto::instance().add_known_host(target_host, print);++ nlohmann::json event;+ event["command"] = "pair_accepted";+ event["fingerprint"] = print;+ event["device_name"] = target_host;+ broadcast_local_event(event.dump());+ }+ }}}).detach();} else if (j.contains("command") && j["command"] == "discover") {diff --git a/src/gtk/main.cpp b/src/gtk/main.cppindex 86351eb..16b21da 100644--- a/src/gtk/main.cpp+++ b/src/gtk/main.cpp@@ -352,7 +352,17 @@ namespace {if (command == "pair_request_received" || command == "untrusted_client_connected") {tether::DiscoveredDevice req;req.fingerprint = event.value("fingerprint", "");- req.name = event.value("device_name", "Unknown Device");++ std::string resolved_name = event.value("device_name", "Unknown Device");+ if (resolved_name == "Unknown Device") {+ for (const auto& d : g_app.discovered_devices) {+ if (d.fingerprint == req.fingerprint && !d.name.empty()) {+ resolved_name = d.name;+ break;+ }+ }+ }+ req.name = resolved_name;req.addresses.push_back({event.value("address", ""), 5134});bool exists = false;for (const auto& dev : g_app.pending_pairing_requests) {@@ -364,6 +374,20 @@ namespace {}return;}+ if (command == "pair_accepted") {+ std::string fp = event.value("fingerprint", "");+ if (!fp.empty()) {+ g_app.connected_fps.push_back(fp);+ g_app.pending_pairing_requests.erase(+ std::remove_if(g_app.pending_pairing_requests.begin(), g_app.pending_pairing_requests.end(), [&](const tether::DiscoveredDevice& d) {+ return d.fingerprint == fp;+ }), g_app.pending_pairing_requests.end()+ );+ refresh_device_list();+ update_right_pane();+ }+ return;+ }if (command == "discovery_result") {g_app.discovered_devices.clear();if (event.contains("devices") && event["devices"].is_array()) {