Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

Mac fixes: Cloudflare turned off stays off whoever turned it off; account join retries after a network hiccup; menu icons follow light/dark and are cached; usage limits show their numbers; Pair iPhone always shows a fresh code; SSH tunnels aren't orphaned by edits and host certificates aren't mistaken for a changed key. iPhone pairing errors say to check Wi-Fi, Tailscale or Reach from anywhere

leepokai8af7154f4638
diff --git a/README.md b/README.md
index 9893d990..ce16adc4 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,7 @@ brew install --cask leepokai/codync/codync
Both install the same signed, notarized app; the host ships inside it.
-Open Codync in the menu bar → **Install host**. The chat bubble opens the full Codync window; the QR button pairs your iPhone.
+Open Codync in the menu bar → **Install host**. **Open Codync** opens the full window; **Pair iPhone…** shows the QR code.
**Linux** — native GTK 4 / libadwaita app plus the host:
diff --git a/apps/ios/Views/PairingView.swift b/apps/ios/Views/PairingView.swift
index cd3255d4..7ad74f3f 100644
--- a/apps/ios/Views/PairingView.swift
+++ b/apps/ios/Views/PairingView.swift
@@ -202,8 +202,15 @@ private struct ScanPage: View {
.background(Palette.surface, in: RoundedRectangle(cornerRadius: 12))
if let error {
- Text(error).font(.footnote).foregroundStyle(Palette.danger)
- .transition(.opacity)
+ VStack(alignment: .leading, spacing: 6) {
+ Text(error).font(.footnote).foregroundStyle(Palette.danger)
+ // The usual cause: the phone has no road to the computer yet.
+ Label("Check that this iPhone and the computer are on the same Wi-Fi or both on Tailscale, or that “Reach from anywhere” is on in Codync on the computer.",
+ systemImage: "wifi.exclamationmark")
+ .font(.footnote)
+ .foregroundStyle(Palette.secondary)
+ }
+ .transition(.opacity)
}
// The other way in: computers on your Google account show up and ask for access themselves.
diff --git a/apps/macos/App/CodyncMacApp.swift b/apps/macos/App/CodyncMacApp.swift
index c7c98f30..005ab7f3 100644
--- a/apps/macos/App/CodyncMacApp.swift
+++ b/apps/macos/App/CodyncMacApp.swift
@@ -18,8 +18,6 @@ struct CodyncMacApp: App {
var body: some Scene {
MenuBarExtra {
MenuView()
- .frame(width: 340)
- .modalHost()
.environment(host)
.environment(account)
} label: {
@@ -37,7 +35,22 @@ struct CodyncMacApp: App {
NSApp.activate()
}
}
- .menuBarExtraStyle(.window)
+ .menuBarExtraStyle(.menu)
+
+ Window("Pair iPhone", id: "pairing") {
+ Group {
+ if let store = host.store {
+ PairingPanel(store: store)
+ .id(store.computer.id)
+ } else {
+ Text("Connect to this Mac's host before pairing your iPhone.")
+ .padding()
+ }
+ }
+ .frame(width: 340)
+ .environment(host)
+ }
+ .windowResizability(.contentSize)
Window("Codync", id: "chat") {
ChatWindow()
@@ -50,279 +63,240 @@ struct CodyncMacApp: App {
}
}
+/// A system menu: macOS owns layout, selection, keyboard navigation and submenus.
struct MenuView: View {
@Environment(HostController.self) private var host
@Environment(\.openWindow) private var openWindow
- @State private var showPairing = false
- @State private var showSettings = false
@AppStorage(SharedStore.usageIconStyleKey, store: UserDefaults(suiteName: SharedStore.appGroup))
private var usageIconStyle = UsageIconStyle.character.rawValue
- @Environment(\.accessibilityReduceMotion) private var reduceMotion
var body: some View {
- VStack(alignment: .leading, spacing: 0) {
- header
- VStack(spacing: 8) {
- if showPairing, let store = host.store {
- PairingPanel(store: store)
- .tile()
- // Grows out of the QR button that opened it, and goes back into it.
- .transition(.scale(0.97, anchor: .topTrailing).combined(with: .opacity))
- } else {
- content
- }
+ Text(status)
+ Button("Open Codync") { open("chat") }
+ .keyboardShortcut("o")
+ if host.state == .running {
+ Button("Pair iPhone…") {
+ host.requestPairing()
+ open("pairing")
}
- .padding(.horizontal, 10)
- footer
+ .disabled(host.store == nil)
}
- .background(Palette.background)
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: showPairing)
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: host.state)
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: host.approvals.map(\.id))
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: host.roster.isEmpty)
- }
-
- // MARK: header
-
- private var header: some View {
- HStack(spacing: 10) {
- CodyncMark()
- .frame(width: 28, height: 28)
- .foregroundStyle(Palette.text)
- .accessibilityHidden(true)
- VStack(alignment: .leading, spacing: 2) {
- Text("Codync").font(.headline).foregroundStyle(Palette.text)
- HStack(spacing: 5) {
- if let color = status.color {
- Circle().fill(color).frame(width: 6, height: 6)
- }
- Text(status.text).font(.caption).foregroundStyle(Palette.secondary)
- }
- .accessibilityElement(children: .combine)
- }
- Spacer()
- if host.state == .running {
- IconButton("Open Codync", systemImage: "bubble.left.and.bubble.right") {
- openWindow(id: "chat")
- NSApp.activate()
- }
- IconButton("Pair iPhone", systemImage: "qrcode", selected: showPairing) {
- showPairing.toggle()
- }
+ Divider()
+ hostItems
+ Divider()
+ Menu("Settings") {
+ Picker("Usage icons", selection: $usageIconStyle) {
+ Text("Character").tag(UsageIconStyle.character.rawValue)
+ Text("Original").tag(UsageIconStyle.original.rawValue)
}
+ Toggle("Open at login", isOn: Binding(
+ get: { host.launchAtLogin },
+ set: { host.setLaunchAtLogin($0) }
+ ))
+ Divider()
+ Button("Restart host") { host.restart() }
+ Button("Open log") { NSWorkspace.shared.open(host.logURL) }
+ Divider()
+ Button("Uninstall host service") { host.uninstall() }
}
- .padding(.horizontal, 14)
- .padding(.top, 14)
- .padding(.bottom, 12)
+ if let version = host.version { Text("Version \(version)") }
+ Button("Quit Codync") { NSApp.terminate(nil) }
+ .keyboardShortcut("q")
}
- /// The header's state line. A dot only when something is going on: blue working, amber needs you, red trouble.
- private var status: (text: String, color: Color?) {
+ private var status: String {
switch host.state {
- case .missingBinary: ("Host not found", Palette.danger)
- case .notInstalled: ("Host not installed", nil)
- case .starting: ("Connecting…", nil)
- case .failed: ("Host problem", Palette.danger)
+ case .missingBinary: "Host not found"
+ case .notInstalled: "Host not installed"
+ case .starting: "Connecting…"
+ case .failed: "Host problem"
case .running:
if !host.approvals.isEmpty {
- ("A device asks for access", Palette.warning)
+ "A device asks for access"
} else if host.needsAttention {
- ("A bot needs you", Palette.warning)
+ "A bot needs you"
} else if host.working > 0 {
- ("\(host.working) bot\(host.working == 1 ? "" : "s") working", Palette.accent)
+ "\(host.working) bot\(host.working == 1 ? "" : "s") working"
} else {
- ("Connected", nil)
+ "Connected"
}
}
}
- // MARK: content
-
- @ViewBuilder private var content: some View {
+ @ViewBuilder private var hostItems: some View {
switch host.state {
case .missingBinary:
- Notice(text: "This build of Codync doesn't include codync-host. Reinstall the app, or install the host with Homebrew:", code: "brew install leepokai/codync/codync-host")
- .tile()
- case .notInstalled:
- VStack(alignment: .leading, spacing: 10) {
- Text("Run your bots on this Mac").font(.callout.weight(.semibold)).foregroundStyle(Palette.text)
- Text("Codync installs a small background service that runs Claude Code, Codex and other agents for your bots — even when this menu is closed.")
- .font(.caption)
- .foregroundStyle(Palette.secondary)
- .fixedSize(horizontal: false, vertical: true)
- Button("Install host") { host.install() }
- .buttonStyle(.primary)
+ Text("Reinstall Codync or install the host with Homebrew.")
+ Button("Copy host install command") {
+ NSPasteboard.general.clearContents()
+ NSPasteboard.general.setString("brew install leepokai/codync/codync-host", forType: .string)
}
- .tile()
+ case .notInstalled:
+ Button("Install host") { host.install() }
case .starting:
- HStack(spacing: 8) {
- Spinner(size: 12)
- Text("Connecting…").font(.caption).foregroundStyle(Palette.secondary)
- }
- .frame(maxWidth: .infinity)
- .tile()
+ Text("Starting the host…")
case let .failed(message):
- VStack(alignment: .leading, spacing: 10) {
- Text(message).font(.caption).foregroundStyle(Palette.danger).textSelection(.enabled)
- HStack {
- Button("Restart host") { host.restart() }
- Button("Open log") { NSWorkspace.shared.open(host.logURL) }
- }
- .buttonStyle(.secondary)
- }
- .tile()
+ Text(message)
+ Button("Restart host") { host.restart() }
+ Button("Open log") { NSWorkspace.shared.open(host.logURL) }
case .running:
if let approval = host.approvals.first {
- ApprovalLine(approval: approval) {
+ Button("Review access request from \(approval.request.deviceName)…") {
host.reviewApprovals()
- openWindow(id: "chat")
- NSApp.activate()
+ open("chat")
}
- .tile()
+ Divider()
}
bots
if host.screen != nil {
- RemoteScreenSection().tile()
+ Divider()
+ RemoteScreenMenu()
}
if !host.usage.providers.isEmpty {
- VStack(spacing: 12) {
- ForEach(host.usage.providers) { UsageBlock(provider: $0) }
+ Divider()
+ ForEach(host.usage.providers) { provider in
+ // Buttons, not text: the menu dims a disabled item's image. They open the app.
+ Button { open("chat") } label: {
+ Label {
+ Text(provider.name)
+ } icon: {
+ menuIcon(ProviderMascot(provider, size: 16, style: UsageIconStyle(rawValue: usageIconStyle) ?? .character),
+ key: "provider|\(provider.id)|\(usageIconStyle)|\((provider.tightest?.percent ?? 0) >= 90)")
+ }
+ }
+ // A bar per limit, with its numbers as the item's title (what VoiceOver reads too).
+ ForEach(provider.windows) { window in
+ Button { open("chat") } label: {
+ Label {
+ Text(usageLine(window))
+ } icon: {
+ menuIcon(UsageBar(window: window, tint: provider.tint),
+ key: "bar|\(provider.id)|\(Int(window.percent.rounded()))")
+ }
+ }
+ }
}
- .tile()
}
}
}
@ViewBuilder private var bots: some View {
- if host.roster.isEmpty {
- HStack(spacing: 12) {
- CharacterAvatar(shape: "cloud", color: "gray", size: 32)
- VStack(alignment: .leading, spacing: 2) {
- Text("No bots yet").font(.callout.weight(.medium)).foregroundStyle(Palette.text)
- Text("Create one from Codync on your iPhone.").font(.caption).foregroundStyle(Palette.secondary)
- }
- Spacer(minLength: 0)
- }
- .tile()
+ let roster = host.roster.filter { !$0.bot.isGroup }
+ if roster.isEmpty {
+ Text("No bots yet")
} else {
- ScrollView {
- VStack(spacing: 0) {
- // The menu shows what the bots are doing; group chats open in the window.
- ForEach(host.roster.filter { !$0.bot.isGroup }) { item in
- BotLine(bot: item.bot, computer: host.accounts.computers.count > 1 ? item.computer : nil) { host.stop(item) }
- .contentShape(Rectangle())
- .onTapGesture {
- host.accounts.selection = item.ref
- openWindow(id: "chat")
- NSApp.activate()
- }
+ ForEach(roster) { item in
+ Menu {
+ Text(item.bot.needsInput ? "Needs your response" : item.bot.isWorking
+ ? (item.bot.activity.isEmpty ? "Working…" : item.bot.activity)
+ : (item.bot.lastMessage ?? item.bot.folderName))
+ Button("Open conversation") {
+ host.accounts.selection = item.ref
+ open("chat")
+ }
+ if item.bot.isWorking {
+ Button("Stop task") { host.stop(item) }
+ }
+ } label: {
+ Label {
+ Text(host.accounts.computers.count > 1 ? "\(item.bot.name) · \(item.computer.name)" : item.bot.name)
+ } icon: {
+ menuIcon(AvatarWithStatus(bot: item.bot, size: 20), key: avatarKey(item.bot))
}
}
- .padding(4)
}
- // ScrollView has no intrinsic height inside a MenuBarExtra window.
- .frame(height: min(CGFloat(host.roster.count) * 46 + 8, 320))
- .tile(padding: 0)
}
}
- // MARK: footer
+ private func open(_ id: String) {
+ openWindow(id: id)
+ NSApp.activate()
+ }
- /// Rarely used, so quiet: settings (with Open at login) and Quit.
- private var footer: some View {
- HStack(spacing: 2) {
- if let version = host.version {
- Text("v\(version)").font(.caption2.monospacedDigit()).foregroundStyle(Palette.tertiary).padding(.leading, 6)
- }
- Spacer()
- // The DropdownMenu shape, but kept on IconButton so the gear hovers like its neighbours.
- IconButton("Settings", systemImage: "gearshape", selected: showSettings) { showSettings.toggle() }
- .codyncMenu(isPresented: $showSettings) {
- [
- MenuItem("Usage icon · Character", selected: usageIconStyle == UsageIconStyle.character.rawValue) {
- usageIconStyle = UsageIconStyle.character.rawValue
- },
- MenuItem("Usage icon · Original", selected: usageIconStyle == UsageIconStyle.original.rawValue) {
- usageIconStyle = UsageIconStyle.original.rawValue
- },
- MenuItem("Open at login", selected: host.launchAtLogin) { host.setLaunchAtLogin(!host.launchAtLogin) },
- MenuItem("Restart host", divider: true) { host.restart() },
- MenuItem("Open log") { NSWorkspace.shared.open(host.logURL) },
- MenuItem("Uninstall host service", divider: true) { host.uninstall() },
- ]
- }
- IconButton("Quit Codync", systemImage: "power") { NSApp.terminate(nil) }
- }
- .padding(.horizontal, 10)
- .padding(.vertical, 8)
+ private func usageLine(_ window: UsageWindow) -> String {
+ let reset = window.resetDate.map { " · resets in \(RelativeTime.until($0))" } ?? ""
+ return "\(window.title) · \(Int(window.percent.rounded()))%\(reset)"
+ }
+
+ /// Everything the avatar image shows; the activity text changes often and isn't drawn.
+ private func avatarKey(_ bot: Bot) -> String {
+ "bot|\(bot.id)|\(bot.avatarShape)|\(bot.avatarColor)|\(bot.status)|\(bot.needsInput)|\(bot.unread > 0)"
+ }
+
+ /// Rendered icons by what they show and the appearance: a bot's status change redraws one image,
+ /// not the whole menu, and nothing is drawn again while the menu is closed and nothing changed.
+ @MainActor private static let iconCache = NSCache<NSString, NSImage>()
+
+ /// NSMenu items only take text and an image, so custom drawing goes in as a rendered image.
+ private func menuIcon(_ view: some View, key: String) -> Image {
+ // The menu follows the system appearance; this view's environment doesn't. Reading
+ // `host.menuIsDark` here redraws the menu when the system switches.
+ let dark = host.menuIsDark
+ let cacheKey = "\(key)|\(dark)" as NSString
+ if let cached = Self.iconCache.object(forKey: cacheKey) { return Image(nsImage: cached) }
+ let appearance = NSAppearance(named: dark ? .darkAqua : .aqua) ?? NSApp.effectiveAppearance
+ let renderer = ImageRenderer(content: view.environment(\.colorScheme, dark ? .dark : .light))
+ renderer.scale = NSScreen.main?.backingScaleFactor ?? 2
+ var image: NSImage?
+ appearance.performAsCurrentDrawingAppearance { image = renderer.nsImage }
+ guard let image else { return Image(systemName: "circle") }
+ Self.iconCache.setObject(image, forKey: cacheKey)
+ return Image(nsImage: image)
}
}
-private extension View {
- /// A Control Center–style module: one group of related things on a raised surface.
- func tile(padding: CGFloat = 12) -> some View {
- self.padding(padding)
- .frame(maxWidth: .infinity, alignment: .leading)
- .background(Palette.surface, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
+/// One usage limit's bar in the provider's color: amber from 70%, red from 90%.
+private struct UsageBar: View {
+ let window: UsageWindow
+ let tint: Color
+
+ var body: some View {
+ let color = window.percent >= 90 ? Palette.danger : window.percent >= 70 ? Palette.warning : tint
+ ZStack(alignment: .leading) {
+ Capsule().fill(Color.primary.opacity(0.12))
+ if window.percent > 0 {
+ Capsule().fill(color).frame(width: max(5, 60 * min(1, window.percent / 100)))
+ }
+ }
+ .frame(width: 60, height: 5)
+ .frame(height: 16)
}
}
-/// Remote screen: see and control this Mac from the iPhone, and let bots use it.
-/// One line when all is well; it only grows to say what's missing.
-private struct RemoteScreenSection: View {
+private struct RemoteScreenMenu: View {
@Environment(HostController.self) private var host
- @Environment(\.accessibilityReduceMotion) private var reduceMotion
var body: some View {
let screen = host.screen ?? ScreenState()
- VStack(alignment: .leading, spacing: 10) {
- HStack(spacing: 10) {
- Image(systemName: "display")
- .font(.system(size: 17, weight: .regular))
- .foregroundStyle(screen.enabled ? Palette.text : Palette.secondary)
- .frame(width: 30, height: 30)
- .accessibilityHidden(true)
- VStack(alignment: .leading, spacing: 1) {
- Text("Remote screen").font(.callout.weight(.medium)).foregroundStyle(Palette.text)
- Text(subtitle(screen))
- .font(.caption)
- .foregroundStyle(screen.enabled && screen.viewers > 0 ? Palette.text : Palette.secondary)
- .lineLimit(2)
- .fixedSize(horizontal: false, vertical: true)
- }
- Spacer(minLength: 8)
- Toggle(isOn: Binding(get: { screen.enabled }, set: { host.setRemoteScreen($0) })) { EmptyView() }
- .toggleStyle(.codync)
- .fixedSize()
- .accessibilityLabel("Remote screen")
- }
+ Menu("Remote screen") {
+ Toggle("Enable remote screen", isOn: Binding(
+ get: { host.screen?.enabled == true },
+ set: { host.setRemoteScreen($0) }
+ ))
+ Text(subtitle(screen))
if screen.enabled {
if host.screenAgentNeedsApproval {
- PermissionLine(title: "Codync Screen in Login Items") { host.openLoginItemsSettings() }
+ Button("Allow Codync Screen in Login Items…") { host.openLoginItemsSettings() }
} else if screen.connected {
if !screen.capture {
- PermissionLine(title: "Screen recording") {
+ Button("Allow Screen Recording…") {
open("x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture")
}
}
if !screen.input {
- PermissionLine(title: "Accessibility (mouse and keyboard)") {
+ Button("Allow Accessibility…") {
open("x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")
}
}
}
}
- if let error = host.screenError {
- Text(error).font(.caption).foregroundStyle(Palette.danger).fixedSize(horizontal: false, vertical: true)
- }
+ if let error = host.screenError { Text(error) }
}
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: screen)
- .animation(Motion.reduced(Motion.layout, reduceMotion), value: host.screenError)
}
private func subtitle(_ screen: ScreenState) -> String {
guard screen.enabled else { return "Control this Mac from your iPhone" }
- if host.screenAgentNeedsApproval { return "Needs one more step" }
+ if host.screenAgentNeedsApproval { return "Needs approval in System Settings" }
if !screen.connected { return "Starting Codync Screen…" }
if !screen.capture || !screen.input { return "Needs permission" }
if screen.viewers > 0 { return screen.viewers == 1 ? "Your iPhone is viewing" : "\(screen.viewers) viewers" }
@@ -336,125 +310,10 @@ private struct RemoteScreenSection: View {
}
}
-/// A permission that's still missing, with the way to fix it.
-private struct PermissionLine: View {
- let title: String
- let fix: () -> Void
-
- var body: some View {
- HStack(spacing: 8) {
- Image(systemName: "exclamationmark.circle.fill")
- .foregroundStyle(Palette.warning)
- .accessibilityHidden(true)
- Text(title).font(.caption).foregroundStyle(Palette.text)
- Spacer()
- IconButton("Open System Settings", systemImage: "gearshape", action: fix)
- }
- .padding(.leading, 8)
- .padding(.vertical, 2)
- .background(Palette.warning.opacity(0.1), in: RoundedRectangle(cornerRadius: 9, style: .continuous))
- .accessibilityElement(children: .combine)
- .accessibilityValue("Not allowed")
- }
-}
-
-/// One provider's limits: its character and name, then a thin bar per limit in its color.
-private struct UsageBlock: View {
- let provider: UsageProvider
- @AppStorage(SharedStore.usageIconStyleKey, store: UserDefaults(suiteName: SharedStore.appGroup))
- private var usageIconStyle = UsageIconStyle.character.rawValue
-
- var body: some View {
- VStack(alignment: .leading, spacing: 7) {
- HStack(spacing: 6) {
- ProviderMascot(provider, size: 15, style: UsageIconStyle(rawValue: usageIconStyle) ?? .character)
- Text(provider.name).font(.caption.weight(.semibold)).foregroundStyle(Palette.text)
- Spacer()
- Text("updated \(RelativeTime.short(Date(milliseconds: provider.updatedAt)))")
- .font(.caption2)
- .foregroundStyle(Palette.tertiary)
- }
- ForEach(provider.windows) { w in
- HStack(spacing: 8) {
- Text(w.title).font(.caption).foregroundStyle(Palette.secondary).lineLimit(1)
- .frame(width: 58, alignment: .leading)
- ThinBar(percent: w.percent, tint: provider.tint)
- Text("\(Int(w.percent.rounded()))%")
- .font(.caption.monospacedDigit().weight(.medium))
- .foregroundStyle(Palette.text)
- .frame(width: 34, alignment: .trailing)
- Text(w.resetDate.map { RelativeTime.until($0) } ?? "")
- .font(.caption2.monospacedDigit())
- .foregroundStyle(Palette.tertiary)
- .frame(width: 48, alignment: .trailing)
- }
- .help(w.resetsShort() ?? w.label)
- .accessibilityElement(children: .combine)
- }
- }
- }
-}
-
-/// 4pt track in the provider's color; amber from 70%, red from 90%. Empty stays empty.
-private struct ThinBar: View {
- let percent: Double
- let tint: Color
-
- var body: some View {
- let color = percent >= 90 ? Palette.danger : percent >= 70 ? Palette.warning : tint
- GeometryReader { geo in
- ZStack(alignment: .leading) {
- Capsule().fill(Palette.text.opacity(0.08))
- if percent > 0 {
- Capsule().fill(color).frame(width: max(4, geo.size.width * min(1, percent / 100)))
- }
- }
- }
- .frame(height: 4)
- }
-}
-
-private struct BotLine: View {
- let bot: Bot
- /// Shown when bots from more than one computer are listed.
- let computer: Computer?
- let stop: () -> Void
- @State private var hovering = false
-
- var body: some View {
- HStack(spacing: 10) {
- AvatarWithStatus(bot: bot, size: 30)
- VStack(alignment: .leading, spacing: 1) {
- HStack(spacing: 4) {
- Text(bot.name).font(.callout.weight(.medium)).foregroundStyle(Palette.text)
- if let computer {
- ComputerBadge(computer, size: 13)
- Text(computer.name).font(.caption2).foregroundStyle(Palette.tertiary).lineLimit(1)
- }
- }
- Text(bot.isWorking ? (bot.activity.isEmpty ? "Working…" : bot.activity) : (bot.lastMessage ?? bot.folderName))
- .font(.caption)
- .foregroundStyle(bot.needsInput ? Palette.warning : bot.isWorking ? Palette.accent : Palette.secondary)
- .lineLimit(1)
- }
- Spacer()
- if bot.isWorking && hovering {
- IconButton("Stop", systemImage: "stop.fill", action: stop)
- } else {
- Text(RelativeTime.short(Date(milliseconds: bot.lastAt))).font(.caption2).foregroundStyle(Palette.tertiary)
- }
- }
- .padding(.horizontal, 8)
- .padding(.vertical, 6)
- .background(hovering ? Palette.text.opacity(0.06) : .clear, in: RoundedRectangle(cornerRadius: 10, style: .continuous))
- .animation(Motion.hover, value: hovering)
- .onHover { hovering = $0 }
- }
-}
-
/// A one-time pairing QR (v3) from a computer this Mac manages: its keys, a code, and how to reach it.
struct PairingPanel: View {
let store: BotStore
+ @Environment(HostController.self) private var host
@State private var info: PairingInfo?
@State private var error: String?
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@@ -507,7 +366,15 @@ struct PairingPanel: View {
.padding(16)
.animation(Motion.reduced(Motion.layout, reduceMotion), value: info?.pairingUrl)
.animation(Motion.reduced(Motion.layout, reduceMotion), value: error)
- .task(id: store.computer.id) { await load() }
+ // A new code each time "Pair iPhone…" is chosen (the window stays open between uses).
+ .task(id: "\(store.computer.id)#\(host.pairingRequest)") { await load() }
+ // And a fresh one when this code expires while it's on screen.
+ .task(id: info?.expiresAt) {
+ guard let expires = info?.expiresAt else { return }
+ let wait = Date(milliseconds: expires).timeIntervalSinceNow
+ try? await Task.sleep(for: .seconds(max(1, wait)))
+ if !Task.isCancelled { await load() }
+ }
}
private func load() async {
@@ -534,66 +401,3 @@ struct PairingPanel: View {
return image
}
}
-
-/// A pending access request in the menu; the decision happens in the approval sheet.
-private struct ApprovalLine: View {
- let approval: Approval
- let review: () -> Void
-
- var body: some View {
- HStack(spacing: 10) {
- Image(systemName: "person.badge.key")
- .foregroundStyle(Palette.warning)
- .accessibilityHidden(true)
- VStack(alignment: .leading, spacing: 1) {
- Text("\(approval.request.deviceName) asks for access").font(.callout.weight(.medium)).foregroundStyle(Palette.text)
- Text(approval.store.hostName).font(.caption).foregroundStyle(Palette.secondary)
- }
- Spacer()
- Button("Review", action: review).buttonStyle(.secondary)
- }
- }
-}
-
-private struct Notice: View {
- let text: String
- let code: String
-
- var body: some View {
- VStack(alignment: .leading, spacing: 8) {
- Text(text).font(.caption).foregroundStyle(Palette.secondary).fixedSize(horizontal: false, vertical: true)
- Text(code)
- .font(.caption.monospaced())
- .textSelection(.enabled)
- .padding(8)
- .frame(maxWidth: .infinity, alignment: .leading)
- .background(Palette.codeBackground, in: RoundedRectangle(cornerRadius: 6))
- }
- .padding(12)
- }
-}
-
-/// The app icon's face drawn as vectors: a 9×9 dot grid lit from the upper left,
-/// eyes as missing dots (same geometry as the app icon, without its black tile).
-private struct CodyncMark: View {
- private static let cells = 9
- private static let eyes: Set<[Int]> = [[3, 3], [3, 5], [4, 3], [4, 5]]
-
- var body: some View {
- Canvas { context, size in
- let n = Double(Self.cells)
- let step = size.width / n
- for row in 0..<Self.cells {
- for col in 0..<Self.cells where !Self.eyes.contains([row, col]) {
- let u = (Double(col) + 0.5) / n * 2 - 1
- let v = (Double(row) + 0.5) / n * 2 - 1
- guard hypot(u, v) <= 1.18 else { continue }
- let shade = 0.55 + 0.45 * min(1, max(0, 0.7 - 0.3 * u - 0.4 * v))
- let r = step * 0.36 * shade
- let c = CGPoint(x: (Double(col) + 0.5) * step, y: (Double(row) + 0.5) * step)
- context.fill(Path(ellipseIn: CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)), with: .foreground)
- }
- }
- }
- }
-}
diff --git a/apps/macos/App/HostController.swift b/apps/macos/App/HostController.swift
index 5414d316..271562e7 100644
--- a/apps/macos/App/HostController.swift
+++ b/apps/macos/App/HostController.swift
@@ -3,6 +3,7 @@ import CodyncKit
import CodyncUI
import Foundation
import Observation
+import os
import ServiceManagement
/// A device asking a computer this Mac manages (itself or over SSH) for access.
@@ -19,6 +20,8 @@ struct Approval: Identifiable {
}
}
+private let log = Logger(subsystem: "com.pokai.Codync", category: "Host")
+
/// Manages the local codync-host (binary, background service) and owns the `AccountStore`
/// the menu and the chat window share: this Mac over loopback, SSH computers through their
/// tunnels, and the account's other computers over the encrypted channel.
@@ -133,15 +136,29 @@ final class HostController {
var logURL: URL { Self.dataDir.appending(path: "host.log") }
+ /// The menu's images are drawn for one appearance; this follows the system's so they're redrawn.
+ private(set) var menuIsDark = false
+ @ObservationIgnored private var appearanceObservation: NSKeyValueObservation?
+ /// Bumped by "Pair iPhone…": the pairing window stays open between uses, so it fetches a fresh code.
+ private(set) var pairingRequest = 0
+
+ func requestPairing() { pairingRequest += 1 }
+
func start() {
+ menuIsDark = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua
+ appearanceObservation = NSApp.observe(\.effectiveAppearance) { [weak self] app, _ in
+ let dark = app.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua
+ Task { @MainActor in self?.menuIsDark = dark }
+ }
refresh()
ssh.connectAll()
watchAutoClaim()
watchCloudDefault()
}
- /// Cloudflare is on by default, so a Mac without Tailscale is reachable away from home; turning it
- /// off (privacy: Wi-Fi and Tailscale only) is remembered and never undone behind the user's back.
+ /// Cloudflare is on by default, so a Mac without Tailscale is reachable away from home. It's turned on
+ /// once per host identity, the first time it's seen off; after that, off stays off, whoever turned it
+ /// off (this app's switch, `codync-host cloud --disable`), and it's never undone behind the user's back.
private func watchCloudDefault() {
let target = withObservationTracking {
cloudDefaultTarget
@@ -150,19 +167,38 @@ final class HostController {
}
guard let target, let client = target.client, !enablingCloud else { return }
enablingCloud = true
+ Self.markCloudDefaultApplied(target.computer.id)
Task {
- do { _ = try await enableCloud(client, store: target) } catch { accounts.lastError = error.localizedDescription }
+ do {
+ _ = try await enableCloud(client, store: target)
+ } catch {
+ log.error("turning Cloudflare on by default failed: \(error.localizedDescription, privacy: .public)")
+ }
enablingCloud = false
}
}
private var enablingCloud = false
- private static let cloudTurnedOffKey = "cloudTurnedOff"
+ /// Host identities whose Cloudflare default has been applied (or found already on).
+ private static let cloudDefaultKey = "cloudDefaultApplied"
+
+ private static func cloudDefaultApplied(_ id: ComputerID) -> Bool {
+ UserDefaults.standard.stringArray(forKey: cloudDefaultKey)?.contains(id) ?? false
+ }
+
+ private static func markCloudDefaultApplied(_ id: ComputerID) {
+ let ids = UserDefaults.standard.stringArray(forKey: cloudDefaultKey) ?? []
+ if !ids.contains(id) { UserDefaults.standard.set(ids + [id], forKey: cloudDefaultKey) }
+ }
private var cloudDefaultTarget: BotStore? {
- guard account.cloudURL != nil, let store, store.connection == .online,
- let status = store.cloud, !status.enabled,
- !UserDefaults.standard.bool(forKey: Self.cloudTurnedOffKey) else { return nil }
+ guard account.cloudURL != nil, let store, store.connection == .online, let status = store.cloud,
+ !Self.cloudDefaultApplied(store.computer.id) else { return nil }
+ // Already on: nothing to apply, and a later "off" is the user's.
+ if status.enabled {
+ Self.markCloudDefaultApplied(store.computer.id)
+ return nil
+ }
return store
}
@@ -281,10 +317,12 @@ final class HostController {
/// §4.2 A: makes a computer this Mac manages part of the signed-in account. Its relay is turned on first,
/// since joining the account is about reaching it from anywhere.
- func claim(_ store: BotStore) async {
+ /// `quiet`: the automatic join, which logs a failure and retries instead of showing it.
+ @discardableResult
+ func claim(_ store: BotStore, quiet: Bool = false) async -> Bool {
guard let cloud, let userID = account.userID, let client = store.client else {
- accounts.lastError = "Sign in first."
- return
+ if !quiet { accounts.lastError = "Sign in first." }
+ return false
}
Self.setKeptOut(store.computer.id, false, userID: userID)
do {
@@ -294,8 +332,14 @@ final class HostController {
_ = try await cloud.completeClaim(challenge.claimId, signed: signed)
await accounts.refreshCloud()
if store.computer.id == self.store?.computer.id { await forgetEarlierIdentities(of: store.computer.id) }
+ return true
} catch {
- accounts.lastError = error.localizedDescription
+ if quiet {
+ log.error("joining the account failed: \(error.localizedDescription, privacy: .public)")
+ } else {
+ accounts.lastError = error.localizedDescription
+ }
+ return false
}
}
@@ -313,7 +357,13 @@ final class HostController {
guard let pending, !autoClaiming else { return }
autoClaiming = true
Task {
- await claim(pending)
+ // A failed join (network hiccup) is retried with backoff for as long as it's still wanted.
+ var delay = 30.0
+ while await !claim(pending, quiet: true) {
+ try? await Task.sleep(for: .seconds(delay))
+ delay = min(delay * 2, 600)
+ guard autoClaimTarget?.computer.id == pending.computer.id else { break }
+ }
autoClaiming = false
}
}
@@ -322,8 +372,9 @@ final class HostController {
/// This Mac's host, online, in no account yet, for a signed-in user who hasn't removed it.
private var autoClaimTarget: BotStore? {
+ // Only with Cloudflare on: joining needs it, and turning it back on is never done silently.
guard let userID = account.userID, let store, store.connection == .online,
- let status = store.cloud, status.owner == nil,
+ let status = store.cloud, status.enabled, status.owner == nil,
!Self.keptOut(userID).contains(store.computer.id) else { return nil }
return store
}
@@ -362,7 +413,7 @@ final class HostController {
func setCloud(_ store: BotStore, enabled: Bool) async {
guard let client = store.client else { return }
- if store.computer.id == self.store?.computer.id { UserDefaults.standard.set(!enabled, forKey: Self.cloudTurnedOffKey) }
+ Self.markCloudDefaultApplied(store.computer.id)
do {
_ = enabled ? try await enableCloud(client, store: store) : try await client.setCloud(enabled: false)
} catch {
diff --git a/apps/macos/App/SSHTunnel.swift b/apps/macos/App/SSHTunnel.swift
index dc26d649..b76014d4 100644
--- a/apps/macos/App/SSHTunnel.swift
+++ b/apps/macos/App/SSHTunnel.swift
@@ -124,12 +124,12 @@ enum SSH {
}
/// `type key` of each host key line (known_hosts or ssh-keyscan output); comments and revoked keys skipped.
- static func hostKeys(_ text: String) -> Set<String> {
+ static func hostKeys(_ text: String, certAuthorities: Bool = true) -> Set<String> {
Set(text.split(whereSeparator: \.isNewline).compactMap { line in
var fields = line.split(whereSeparator: \.isWhitespace)
guard let first = fields.first, !first.hasPrefix("#") else { return nil }
if first.hasPrefix("@") {
- guard first == "@cert-authority" else { return nil }
+ guard certAuthorities, first == "@cert-authority" else { return nil }
fields.removeFirst()
}
return fields.count >= 3 ? "\(fields[1]) \(fields[2])" : nil
@@ -456,7 +456,9 @@ final class SSHComputers {
if !resolved.usesProxy {
let scan = await ProcessRunner.run(SSH.keyscan, SSH.keyscanArguments(resolved))
let current = SSH.hostKeys(scan.text)
- if !current.isEmpty, current.isDisjoint(with: recorded) {
+ // Only plain host keys can be compared: a `@cert-authority` key is never what keyscan returns.
+ let pinned = await recordedKeys(resolved.knownHostsName, certAuthorities: false)
+ if !current.isEmpty, !pinned.isEmpty, current.isDisjoint(with: pinned) {
return .stop(.failed("The host key of \(profile.host) changed. This can mean someone is intercepting the connection. Codync won't connect until the old key is removed from known_hosts."))
}
}
@@ -516,7 +518,13 @@ final class SSHComputers {
}
// /health is public, so the answer alone doesn't prove it came through ssh: another
// local user could have taken the port. Only a listener ssh owns gets the token.
- guard await Self.sshListens(pid: process.processIdentifier, port: port), process.isRunning else {
+ let listens = await Self.sshListens(pid: process.processIdentifier, port: port)
+ // Disconnected, removed or edited meanwhile: this attempt must not register its tunnel.
+ guard !Task.isCancelled else {
+ process.terminate()
+ return .stop(.idle)
+ }
+ guard listens, process.isRunning else {
log.error("tunnel port \(port) answered by something other than ssh")
process.terminate()
lastProblem = "Another program answered on the tunnel's local port."
@@ -534,11 +542,11 @@ final class SSHComputers {
return .retry("Couldn't open the tunnel to \(profile.host). \(lastProblem)")
}
- private func recordedKeys(_ name: String) async -> Set<String> {
+ private func recordedKeys(_ name: String, certAuthorities: Bool = true) async -> Set<String> {
var keys = Set<String>()
for file in SSH.knownHostsFiles(home: home) where FileManager.default.fileExists(atPath: file) {
let found = await ProcessRunner.run(SSH.keygen, ["-F", name, "-f", file])
- if found.status == 0 { keys.formUnion(SSH.hostKeys(found.text)) }
+ if found.status == 0 { keys.formUnion(SSH.hostKeys(found.text, certAuthorities: certAuthorities)) }
}
return keys
}