From 59e3a9e29660a4e20eb6d8841273fc50d3eb7c56 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Sat, 21 Jun 2025 11:59:50 +0100 Subject: [PATCH] Hostkey changed warning rewrote hostkey cahnged checker hostkeys sync as well, and the app syncs icloud on launch --- ShhShell/Host/HostsManager.swift | 1 + ShhShell/Views/ConnectionView.swift | 34 +++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/ShhShell/Host/HostsManager.swift b/ShhShell/Host/HostsManager.swift index 2e3d395..1017968 100644 --- a/ShhShell/Host/HostsManager.swift +++ b/ShhShell/Host/HostsManager.swift @@ -41,6 +41,7 @@ class HostsManager: ObservableObject { } func loadSavedHosts() { + userDefaults.synchronize() let decoder = JSONDecoder() guard let data = userDefaults.data(forKey: "savedHosts") else { return } diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index 9425c8a..723f8b0 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -23,6 +23,8 @@ struct ConnectionView: View { @State var privPickerPresented: Bool = false @State var pubPickerPresented: Bool = false + @State var hostKeyChangedAlert: Bool = false + var body: some View { NavigationStack { List { @@ -108,12 +110,10 @@ struct ConnectionView: View { if handler.host.key != nil { Text("Hostkey: \(handler.host.key!.base64EncodedString())") .onChange(of: handler.host.key) { _ in - guard let matchedIndex = hostsManager.getHostIndexMatching(handler.host) else { return } - guard handler.host.key == hostsManager.savedHosts[matchedIndex].key else { - let hostkeyBefore = hostsManager.savedHosts[matchedIndex].key - let currentHostkey = handler.host.key - print("hiiii") - fatalError() + guard let previousKnownHost = hostsManager.getHostMatching(handler.host) else { return } + guard handler.host.key == previousKnownHost.key else { + hostKeyChangedAlert = true + return } } } @@ -129,13 +129,28 @@ struct ConnectionView: View { withAnimation { handler.testExec() } } label: { if let testResult = handler.testSuceeded { - Image(systemName: testResult ? "checkmark.circle" : "xmark.circle") - .modifier(foregroundColorStyle(testResult ? .green : .red)) + Image(systemName: testResult ? "checkmark.circle" : "xmark.circle") + .modifier(foregroundColorStyle(testResult ? .green : .red)) } else { Label("Test Connection", systemImage: "checkmark") } } } + .alert("Hostkey changed", isPresented: $hostKeyChangedAlert) { + Button("Accept New Hostkey", role: .destructive) { + hostsManager.updateHost(handler.host) + } + + Button("Disconnect", role: .cancel) { + handler.disconnect() + handler.host.key = hostsManager.getHostMatching(handler.host)?.key + } + } message: { + Text(""" + Expected \(hostsManager.getHostMatching(handler.host)!.key!.base64EncodedString()) + but recieved \(handler.host.key!.base64EncodedString()) from the server + """) + } .transition(.opacity) .toolbar { ToolbarItem() { @@ -166,8 +181,9 @@ struct ConnectionView: View { } } .onDisappear { - if hostsManager.getHostMatching(handler.host) != handler.host { + guard hostsManager.getHostMatching(handler.host) == handler.host else { hostsManager.updateHost(handler.host) + return } } }