Hostkey changed warning

rewrote hostkey cahnged checker
hostkeys sync as well, and the app syncs icloud on launch
This commit is contained in:
neon443
2025-06-21 11:59:50 +01:00
parent d982d018b8
commit 59e3a9e296
2 changed files with 26 additions and 9 deletions

View File

@@ -41,6 +41,7 @@ class HostsManager: ObservableObject {
}
func loadSavedHosts() {
userDefaults.synchronize()
let decoder = JSONDecoder()
guard let data = userDefaults.data(forKey: "savedHosts") else { return }

View File

@@ -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
}
}
}