2022-01-29 18:59:42 +01:00
|
|
|
import SwiftUI
|
|
|
|
import CryptoKit
|
|
|
|
|
|
|
|
let server = Client(server: URL(string: "https://christophhagen.de/sesame/")!)
|
|
|
|
|
|
|
|
struct ContentView: View {
|
2022-04-09 17:43:33 +02:00
|
|
|
|
|
|
|
@AppStorage("counter")
|
|
|
|
var nextMessageCounter: Int = 0
|
2022-01-29 18:59:42 +01:00
|
|
|
|
2022-04-09 17:43:33 +02:00
|
|
|
@State
|
|
|
|
var state: ClientState = .noKeyAvailable
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var timer: Timer?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var hasActiveRequest = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var responseTime: Date? = nil
|
|
|
|
|
2022-01-29 18:59:42 +01:00
|
|
|
var isPerformingRequests: Bool {
|
2022-04-09 17:43:33 +02:00
|
|
|
hasActiveRequest ||
|
|
|
|
state == .waitingForResponse
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
|
|
|
|
var buttonBackground: Color {
|
2022-04-13 14:55:47 +02:00
|
|
|
state.allowsAction ?
|
2022-04-09 17:43:33 +02:00
|
|
|
.white.opacity(0.2) :
|
|
|
|
.gray.opacity(0.2)
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
|
|
|
|
let buttonBorderWidth: CGFloat = 3
|
|
|
|
|
|
|
|
var buttonColor: Color {
|
2022-04-13 14:55:47 +02:00
|
|
|
state.allowsAction ? .white : .gray
|
2022-04-09 17:43:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 14:55:47 +02:00
|
|
|
private let buttonWidth: CGFloat = 250
|
2022-04-09 17:43:33 +02:00
|
|
|
|
2022-01-29 18:59:42 +01:00
|
|
|
var body: some View {
|
2022-04-09 17:43:33 +02:00
|
|
|
GeometryReader { geo in
|
|
|
|
VStack(spacing: 20) {
|
|
|
|
Spacer()
|
2022-04-13 14:55:47 +02:00
|
|
|
if state.requiresDescription {
|
2022-04-09 17:43:33 +02:00
|
|
|
Text(state.description)
|
|
|
|
.padding()
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-13 14:55:47 +02:00
|
|
|
Button(state.actionText, action: mainButtonPressed)
|
2022-04-09 17:43:33 +02:00
|
|
|
.frame(width: buttonWidth, height: buttonWidth, alignment: .center)
|
|
|
|
.background(buttonBackground)
|
|
|
|
.cornerRadius(buttonWidth / 2)
|
|
|
|
.overlay(RoundedRectangle(cornerRadius: buttonWidth / 2).stroke(lineWidth: buttonBorderWidth).foregroundColor(buttonColor))
|
|
|
|
.foregroundColor(buttonColor)
|
|
|
|
.font(.title)
|
2022-04-13 14:55:47 +02:00
|
|
|
.disabled(!state.allowsAction)
|
|
|
|
.padding(.bottom, (geo.size.width-buttonWidth) / 2)
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
.onAppear {
|
|
|
|
if KeyManagement.hasKey {
|
|
|
|
state = .requestingStatus
|
|
|
|
}
|
|
|
|
startRegularStatusUpdates()
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
.onDisappear {
|
|
|
|
endRegularStatusUpdates()
|
|
|
|
}
|
|
|
|
.frame(width: geo.size.width, height: geo.size.height)
|
2022-04-13 14:55:47 +02:00
|
|
|
.background(state.color)
|
|
|
|
.animation(.easeInOut, value: state.color)
|
2022-04-09 17:43:33 +02:00
|
|
|
}
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func mainButtonPressed() {
|
2022-04-09 17:43:33 +02:00
|
|
|
guard let key = KeyManagement.key?.remote else {
|
|
|
|
generateKey()
|
|
|
|
return
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
sendMessage(using: key)
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
2022-04-09 17:43:33 +02:00
|
|
|
func sendMessage(using key: SymmetricKey) {
|
|
|
|
let count = UInt32(nextMessageCounter)
|
|
|
|
let now = Date()
|
|
|
|
let content = Message.Content(
|
|
|
|
time: now.timestamp,
|
|
|
|
id: count)
|
|
|
|
let message = content.authenticate(using: key)
|
2022-01-29 18:59:42 +01:00
|
|
|
state = .waitingForResponse
|
2022-04-09 17:43:33 +02:00
|
|
|
print("Sending message \(count)")
|
2022-01-29 18:59:42 +01:00
|
|
|
Task {
|
2022-04-09 17:43:33 +02:00
|
|
|
let (newState, message) = try await server.send(message)
|
|
|
|
responseTime = now
|
2022-01-29 18:59:42 +01:00
|
|
|
state = newState
|
2022-04-09 17:43:33 +02:00
|
|
|
if let message = message {
|
|
|
|
processResponse(message, sendTime: now)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func processResponse(_ message: Message, sendTime: Date) {
|
|
|
|
guard let key = KeyManagement.key?.device else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard message.isValid(using: key) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nextMessageCounter = Int(message.content.id)
|
|
|
|
print("Next counter is \(message.content.id)")
|
|
|
|
let now = Date()
|
|
|
|
let total = now.timeIntervalSince(sendTime)
|
|
|
|
print("Total time: \(Int(total * 1000)) ms")
|
|
|
|
let deviceTime = Date(timestamp: message.content.time)
|
|
|
|
let time1 = deviceTime.timeIntervalSince(sendTime)
|
|
|
|
let time2 = now.timeIntervalSince(deviceTime)
|
|
|
|
if time1 < 0 {
|
|
|
|
print("Device time behind by at least \(Int(-time1 * 1000)) ms behind")
|
|
|
|
} else if time2 < 0 {
|
|
|
|
print("Device time behind by at least \(Int(-time2 * 1000)) ms ahead")
|
|
|
|
} else {
|
|
|
|
print("Device time synchronized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func startRegularStatusUpdates() {
|
|
|
|
guard timer == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: checkDeviceStatus)
|
|
|
|
timer!.fire()
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
|
|
|
|
private func endRegularStatusUpdates() {
|
|
|
|
timer?.invalidate()
|
|
|
|
timer = nil
|
|
|
|
}
|
2022-01-29 18:59:42 +01:00
|
|
|
|
2022-04-09 17:43:33 +02:00
|
|
|
func checkDeviceStatus(_ timer: Timer) {
|
|
|
|
guard !hasActiveRequest else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hasActiveRequest = true
|
2022-01-29 18:59:42 +01:00
|
|
|
print("Checking device status")
|
|
|
|
Task {
|
2022-04-09 17:43:33 +02:00
|
|
|
let newState = await server.deviceStatus()
|
|
|
|
hasActiveRequest = false
|
|
|
|
switch state {
|
|
|
|
case .noKeyAvailable:
|
|
|
|
return
|
|
|
|
case .requestingStatus, .deviceNotAvailable, .ready:
|
|
|
|
state = newState
|
|
|
|
case .waitingForResponse:
|
|
|
|
return
|
|
|
|
case .messageRejected, .openSesame, .internalError:
|
|
|
|
guard let time = responseTime else {
|
|
|
|
state = newState
|
2022-01-29 18:59:42 +01:00
|
|
|
return
|
2022-04-09 17:43:33 +02:00
|
|
|
}
|
|
|
|
responseTime = nil
|
|
|
|
// Wait at least 5 seconds after these states have been reached before changing the
|
|
|
|
let elapsed = Date.now.timeIntervalSince(time)
|
|
|
|
guard elapsed < 5 else {
|
|
|
|
state = newState
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let secondsToWait = Int(elapsed.rounded(.up))
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(secondsToWait)) {
|
2022-01-29 18:59:42 +01:00
|
|
|
state = newState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 17:43:33 +02:00
|
|
|
func generateKey() {
|
|
|
|
print("Regenerate key")
|
|
|
|
KeyManagement.generateNewKeys()
|
|
|
|
state = .requestingStatus
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func shareKey() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
ContentView()
|
|
|
|
.previewDevice("iPhone 8")
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
|
|
|
|
extension Date {
|
|
|
|
|
|
|
|
var timestamp: UInt32 {
|
|
|
|
UInt32(timeIntervalSince1970.rounded())
|
|
|
|
}
|
|
|
|
|
|
|
|
init(timestamp: UInt32) {
|
|
|
|
self.init(timeIntervalSince1970: TimeInterval(timestamp))
|
|
|
|
}
|
|
|
|
}
|