diff --git a/Android/build.sh b/Android/build.sh index 76d8a96..4e142d1 100755 --- a/Android/build.sh +++ b/Android/build.sh @@ -24,7 +24,7 @@ platform=${ANDROID_PLATFORM:-35} jar=$sdk/platforms/android-$platform/android.jar [ -f "$jar" ] || { echo "No $jar: install the platform with sdkmanager \"platforms;android-$platform\"." >&2; exit 1; } tools=$(ls -d "$sdk"/build-tools/* | sort -V | tail -1) -version=${APP_VERSION:-0.9.14} +version=${APP_VERSION:-0.9.15} IFS=. read -r major minor patch <<<"$version.0.0" code=$((major * 10000 + minor * 100 + ${patch%%[^0-9]*})) diff --git a/App/Mesh.swift b/App/Mesh.swift index 8ed296e..5b15803 100644 --- a/App/Mesh.swift +++ b/App/Mesh.swift @@ -994,7 +994,10 @@ final class MeshStore: ObservableObject { /// another member when none does. enum MeshFetch { /// `request` as built for `peer`'s address in the list. A machine that isn't a member is asked as it is. - static func data(for request: URLRequest, peer: String, relay: Bool = true, hops: Int = MeshLogic.maxHops - 1) async throws -> (Data, URLResponse) { + /// `direct` tries the member's own addresses even when it was lately read through another, and waits the + /// request's whole time for them: for what can't be passed on as it is (a POST, which a relay doesn't carry). + static func data(for request: URLRequest, peer: String, relay: Bool = true, hops: Int = MeshLogic.maxHops - 1, + direct: Bool = false) async throws -> (Data, URLResponse) { let route = await MeshStore.shared.route(for: peer) guard let member = route.member, let url = request.url else { var plain = request @@ -1002,16 +1005,16 @@ enum MeshFetch { return try await HTTPFetch.data(for: plain) } var failure: Error = URLError(.cannotConnectToHost) - if route.tryDirect { + if route.tryDirect || direct { for candidate in route.candidates { guard let place = FleetStore.url(for: candidate), var parts = URLComponents(url: url, resolvingAgainstBaseURL: false) else { continue } parts.host = place.host parts.port = place.port - guard let direct = parts.url else { continue } + guard let straight = parts.url else { continue } if MeshStore.blocked.contains(MeshLogic.normal(candidate).lowercased()) { continue } var asked = request - asked.url = direct - if !route.settled { asked.timeoutInterval = min(request.timeoutInterval, 2) } + asked.url = straight + if !route.settled, !direct { asked.timeoutInterval = min(request.timeoutInterval, 2) } MeshLogic.sign(&asked, id: route.myID, token: route.myToken) do { let (data, response) = try await HTTPFetch.data(for: asked) diff --git a/App/StatusJSON.swift b/App/StatusJSON.swift index 1d45e6b..ae5ff2e 100644 --- a/App/StatusJSON.swift +++ b/App/StatusJSON.swift @@ -440,7 +440,7 @@ struct LocalResponse { extension StatusReport { /// Tokenmeter's version. The Mac app reads it from its Info.plist, which build-library-app.sh writes; the /// Linux service has no Info.plist, so it uses `releaseVersion`. Keep that in step with the build scripts. - static let releaseVersion = "0.9.14" + static let releaseVersion = "0.9.15" static var appVersion: String { Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? releaseVersion } diff --git a/Linux/MeshUpdates.swift b/Linux/MeshUpdates.swift index 4cb5c78..c93cc3f 100644 --- a/Linux/MeshUpdates.swift +++ b/Linux/MeshUpdates.swift @@ -118,7 +118,13 @@ final class UpdateStore { return TokenmeterService.json(shared.answer()) } endpoint.route("GET", "/mesh/update") { request in - guard MeshStore.shared.member(signing: request) != nil else { return .text("Members of this mesh only.\n", code: 403) } + guard let member = MeshStore.shared.member(signing: request) else { return .text("Members of this mesh only.\n", code: 403) } + // Told to look (`look=1`) or to install (`install=1`) through another member: a relay passes on reads + // only, so a member this one's teller can't reach directly hears it this way. + if request.value("look") == "1" || request.value("install") == "1" { + let teller = request.value("from").flatMap { MeshStore.shared.state.keys[$0] } ?? MeshStore.shared.state.keys[member.id] + return TokenmeterService.json(shared.told(install: request.value("install") == "1", first: teller)) + } return TokenmeterService.json(shared.handOver(platform: request.value("platform") ?? ServicePackage.platform)) } endpoint.route("GET", "/mesh/update/package") { request in @@ -132,10 +138,8 @@ final class UpdateStore { } endpoint.route("POST", "/mesh/update") { request in guard let member = MeshStore.shared.member(signing: request) else { return .text("Members of this mesh only.\n", code: 403) } - let install = request.fields["install"] as? Bool == true - let preferred = MeshStore.shared.state.keys[member.id] - Task { await shared.check(auto: !install, install: install, first: preferred) } - return TokenmeterService.json(shared.handOver(platform: ServicePackage.platform)) + return TokenmeterService.json(shared.told(install: request.fields["install"] as? Bool == true, + first: MeshStore.shared.state.keys[member.id])) } MeshStore.shared.settings = { shared.settings } MeshStore.shared.onSettings = { theirs in shared.take(theirs) } @@ -374,6 +378,15 @@ final class UpdateStore { private var summed: [String: (changed: Date, sum: String)] = [:] + /// A member said to look, or to install: this machine does, and answers at once how it stands. Without a + /// package: the teller reads only how it stands, and making this machine's own package (a Mac copies its + /// Tokenmeter.app into it) took longer than the teller waits, so the teller took it for unreachable. + func told(install: Bool, first: String?) -> HandOver { + Task { await self.check(auto: !install, install: install, first: first) } + return HandOver(version: "", sha256: "", running: StatusReport.appVersion, phase: phase, problem: problem, + found: offer.map { MacService.newer($0.version, than: StatusReport.appVersion) ? $0.version : "" } ?? "") + } + private func handOver(platform: String) -> HandOver { var answer = HandOver(version: "", sha256: "", running: StatusReport.appVersion, phase: phase, problem: problem, found: offer.map { MacService.newer($0.version, than: StatusReport.appVersion) ? $0.version : "" } ?? "") @@ -959,13 +972,29 @@ final class UpdateStore { request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = try? JSONSerialization.data(withJSONObject: ["version": version, "install": install]) let asked = request - if let (data, response) = try? await MeshFetch.data(for: asked, peer: key, relay: false), (response as? HTTPURLResponse)?.statusCode == 200, - let answer = try? JSONDecoder().decode(HandOver.self, from: data) { + // Its own addresses first, waiting the whole time even when it was lately read through another member; + // then through another member, as a read (a relay doesn't pass a POST on). A member reached only that + // way (a Mac the others reach and this machine doesn't) used to be left out without a word. + var answered: HandOver? + if let (data, response) = try? await MeshFetch.data(for: asked, peer: key, relay: false, direct: true), + (response as? HTTPURLResponse)?.statusCode == 200 { + answered = try? JSONDecoder().decode(HandOver.self, from: data) + } + if answered == nil, let read = self.url(key, "/mesh/update", [URLQueryItem(name: "platform", value: ServicePackage.platform), + URLQueryItem(name: install ? "install" : "look", value: "1"), + URLQueryItem(name: "from", value: mesh.state.machineID)]) { + var relayed = URLRequest(url: read) + relayed.timeoutInterval = 8 + let passed = relayed + if let (data, response) = try? await MeshFetch.data(for: passed, peer: key), (response as? HTTPURLResponse)?.statusCode == 200 { + answered = try? JSONDecoder().decode(HandOver.self, from: data) + } + } + if let answer = answered { notes[member.id] = Note(version: answer.running, phase: "checking", problem: "", found: answer.found ?? "") } else { - // A member that reaches it passes it on. notes[member.id] = Note(version: notes[member.id]?.version ?? member.version, phase: "idle", - problem: install ? "not reached from here; a member that reaches it tells it" : "") + problem: install ? "not reached from here; a member that reaches it tells it" : "not reached from here or through another machine") } } } diff --git a/Linux/kde/app/AppVersion.js b/Linux/kde/app/AppVersion.js index 1d09cd5..171afd2 100644 --- a/Linux/kde/app/AppVersion.js +++ b/Linux/kde/app/AppVersion.js @@ -3,4 +3,4 @@ // update keeps saying the version it is, while the service reports its own (backend.snapshot.version). // scripts/package-linux.sh and package-flatpak.sh write it from the package's version; keep it in step with // StatusReport.releaseVersion when bumping (a unit test checks it). -var version = "0.9.14" +var version = "0.9.15" diff --git a/Linux/kde/mock/machines.json b/Linux/kde/mock/machines.json index af14fa1..6ea4447 100644 --- a/Linux/kde/mock/machines.json +++ b/Linux/kde/mock/machines.json @@ -7,7 +7,7 @@ "report": { "schema": 1, "app": "Tokenmeter", - "version": "0.9.14", + "version": "0.9.15", "host": "studio", "chip": "Apple M3 Ultra", "time": "2026-11-13T19:00:00Z", diff --git a/Linux/kde/mock/settings.json b/Linux/kde/mock/settings.json index db3cba1..a9e8e59 100644 --- a/Linux/kde/mock/settings.json +++ b/Linux/kde/mock/settings.json @@ -15,7 +15,7 @@ "addresses": [ "192.168.1.20" ], - "version": "0.9.14", + "version": "0.9.15", "shareHistory": false, "footprint": { "cpuPercent": 0.4, diff --git a/Linux/kde/plasmoid/metadata.json b/Linux/kde/plasmoid/metadata.json index 8a84114..947738b 100644 --- a/Linux/kde/plasmoid/metadata.json +++ b/Linux/kde/plasmoid/metadata.json @@ -7,7 +7,7 @@ "Icon": "tokenmeter", "Category": "System Information", "License": "Apache-2.0", - "Version": "0.9.14", + "Version": "0.9.15", "Website": "https://github.com/tokenmeter1/Tokenmeter" }, "X-Plasma-API-Minimum-Version": "6.0", diff --git a/Linux/tests/mesh.sh b/Linux/tests/mesh.sh index 710cd26..f52d6fd 100644 --- a/Linux/tests/mesh.sh +++ b/Linux/tests/mesh.sh @@ -42,9 +42,11 @@ online='sorted(m["name"] for m in d["members"] if m["online"])' # cy can't reach ada itself, as on another part of the network: it reads ada through bo, further down. serve ada 7761 serve bo 7762 -serve cy 7763 TOKENMETER_MESH_BLOCK="$address:7761,127.0.0.1:7761" +serve cy 7763 TOKENMETER_MESH_BLOCK="$address:7761,127.0.0.1:7761" TOKENMETER_UPDATES_FOLDER="$work/cy-updates" serve di 7764 for url in $ada $bo $cy $di; do until_is $url/mesh 'len(d["members"])' 1 "a service didn't start"; done +# None looks for updates by itself (a minute after starting), so a look below is one it was told to do. +for url in $ada $bo $cy $di; do curl -sf -X POST $url/updates -d '{"updateCheck":"off","updateSource":"folder"}' >/dev/null || fail "update settings"; done curl -sf $ada/mesh | field 'd["name"], d["open"], len(d["id"])' | grep -q "('ada', True, 64)" || fail "ada's own mesh" # They hear each other's announcements: each lists the three others as found, alone, to add. One that was @@ -99,6 +101,16 @@ until_is $ada/mesh "$online" "['ada', 'bo', 'cy']" "ada doesn't see cy online" # cy can't reach ada, so it reads it through bo and says so. until_is $cy/mesh '[(m["online"], m["via"]) for m in d["members"] if m["name"] == "ada"]' "[(True, 'bo')]" "cy doesn't read ada through bo" until_is $cy/machines 'sorted((m["report"]["host"], m.get("via")) for m in d["machines"] if m["online"])' "[('ada', 'bo'), ('bo', None)]" "cy's list of machines" +# Check Now on cy, whose updates folder holds a later package for another system: the members behind are told to +# look, ada too, which cy reaches only through bo (a relay passes reads on, not a POST; ada used to be left out). +mkdir -p "$work/cy-updates" +: >"$work/cy-updates/tokenmeter-service-99.0.0-macos-arm64.tar.gz" +curl -sf $ada/updates | field 'd["lastCheck"]' | grep -qx '0\(\.0\)\?' || fail "ada had looked for updates before it was told to" +curl -sf -X POST $cy/updates -d '{"check":true}' >/dev/null || fail "Check Now on cy" +until_is $cy/updates 'd["problem"].endswith("Update the Others installs it on them now.")' True "cy's Check Now didn't tell the members behind" +until_is $ada/updates 'd["lastCheck"] > 0' True "ada, reached only through bo, wasn't told to look" +curl -sf $cy/updates | field '[m["problem"] for m in d["members"] if m["name"] == "ada"]' | grep -qx "\['']" || fail "cy says it didn't reach ada" +rm "$work/cy-updates/tokenmeter-service-99.0.0-macos-arm64.tar.gz" # di asks cy. Ignored there, it is in everyone's Ignored list, and bo can allow it after all. HOME="$work/di" "$tokenmeter" mesh --port 7764 | grep -q "Found on this network" || fail "tokenmeter mesh doesn't list what it found" diff --git a/build-library-app.sh b/build-library-app.sh index 00b486a..d2809eb 100755 --- a/build-library-app.sh +++ b/build-library-app.sh @@ -18,7 +18,7 @@ for word in "$@"; do esac done app="$project_dir/Tokenmeter.app" -version=${APP_VERSION:-0.9.14} +version=${APP_VERSION:-0.9.15} build=${APP_BUILD:-1} # SwiftUI's @State is a compiler macro in the macOS 26 SDK. The Command Line Tools ship that SDK without diff --git a/scripts/package-flatpak.sh b/scripts/package-flatpak.sh index 35ab0f6..765cbc3 100755 --- a/scripts/package-flatpak.sh +++ b/scripts/package-flatpak.sh @@ -7,7 +7,7 @@ # Needs flatpak and flatpak-builder; the KDE runtime and SDK are fetched from Flathub. set -eu project_dir=$(cd "$(dirname "$0")/.." && pwd) -version=${APP_VERSION:-0.9.14} +version=${APP_VERSION:-0.9.15} arch=$(uname -m) id=io.github.tokenmeter1.tokenmeter name="Tokenmeter-$version-linux-$arch" diff --git a/scripts/package-linux.sh b/scripts/package-linux.sh index d1f72f1..2b76328 100755 --- a/scripts/package-linux.sh +++ b/scripts/package-linux.sh @@ -4,7 +4,7 @@ # and install.sh. Needs the Swift toolchain; run it on Linux. set -eu project_dir=$(cd "$(dirname "$0")/.." && pwd) -version=${APP_VERSION:-0.9.14} +version=${APP_VERSION:-0.9.15} arch=$(uname -m) name="Tokenmeter-$version-linux-$arch" diff --git a/scripts/package-service.sh b/scripts/package-service.sh index 7042774..49b04cf 100755 --- a/scripts/package-service.sh +++ b/scripts/package-service.sh @@ -18,7 +18,7 @@ # On a Mac, SIGN_IDENTITY signs the program with a Developer ID; without it the program is ad-hoc signed. set -eu project_dir=$(cd "$(dirname "$0")/.." && pwd) -version=${APP_VERSION:-0.9.14} +version=${APP_VERSION:-0.9.15} case "$(uname -s)" in Darwin) os=macos ;; Linux) os=linux ;; diff --git a/scripts/package.sh b/scripts/package.sh index d5af922..faa91b6 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -16,7 +16,7 @@ set -euo pipefail project_dir=${0:A:h:h} app="$project_dir/Tokenmeter.app" dist="$project_dir/dist" -export APP_VERSION=${APP_VERSION:-0.9.14} +export APP_VERSION=${APP_VERSION:-0.9.15} identity=${SIGN_IDENTITY:-} profile=${NOTARY_PROFILE:-}