feat: add remote reboot support

This commit is contained in:
2026-04-19 16:53:17 +02:00
parent 92040e5c5e
commit afbb425e3b
11 changed files with 381 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ import Foundation
enum APIVersion: String, CaseIterable {
case v2_12 = "2.12"
case v2_13 = "2.13"
case v2_14 = "2.14"
static func from(versionString: String) -> APIVersion? {
if let version = APIVersion(rawValue: versionString) {
@@ -24,7 +25,8 @@ enum APIVersion: String, CaseIterable {
switch (major, minor) {
case (2, 12): return .v2_12
case (2, 13...): return .v2_13
case (2, 13): return .v2_13
case (2, 14...): return .v2_14
default: return nil
}
}
@@ -36,6 +38,7 @@ protocol AnyServerAPI {
func fetchMemoryData() async throws -> Any
func fetchUtilizationData() async throws -> Any
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
func restartServer(apiKey: String) async throws
}
private struct AnyServerAPIWrapper<T: ServerAPIProtocol>: AnyServerAPI {
@@ -64,6 +67,10 @@ private struct AnyServerAPIWrapper<T: ServerAPIProtocol>: AnyServerAPI {
func fetchServerSummary(apiKey: String) async throws -> ServerInfo {
return try await wrapped.fetchServerSummary(apiKey: apiKey)
}
func restartServer(apiKey: String) async throws {
try await wrapped.restartServer(apiKey: apiKey)
}
}
class APIFactory {
@@ -73,6 +80,8 @@ class APIFactory {
return AnyServerAPIWrapper(APIv2_12(baseURL: baseURL))
case .v2_13:
return AnyServerAPIWrapper(APIv2_13(baseURL: baseURL))
case .v2_14:
return AnyServerAPIWrapper(APIv2_14(baseURL: baseURL))
}
}
@@ -104,7 +113,7 @@ class APIFactory {
}
}
return AnyServerAPIWrapper(APIv2_13(baseURL: baseURL))
return AnyServerAPIWrapper(APIv2_14(baseURL: baseURL))
}
}

View File

@@ -18,6 +18,7 @@ protocol ServerAPIProtocol {
func fetchMemory() async throws -> MemoryType
func fetchUtilization() async throws -> UtilizationType
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
func restartServer(apiKey: String) async throws
}
struct SystemInfo: Codable {
@@ -36,6 +37,15 @@ class BaseAPIClient {
}
func performRequest<T: Codable>(_ request: URLRequest, responseType: T.Type) async throws -> T {
let (data, _) = try await performDataRequest(request)
return try JSONDecoder().decode(T.self, from: data)
}
func performRequestWithoutBody(_ request: URLRequest) async throws {
_ = try await performDataRequest(request)
}
private func performDataRequest(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
@@ -43,25 +53,61 @@ class BaseAPIClient {
}
guard 200...299 ~= httpResponse.statusCode else {
throw APIError.httpError(httpResponse.statusCode)
throw APIError.httpError(
httpResponse.statusCode,
BaseAPIClient.extractErrorMessage(from: data)
)
}
return try JSONDecoder().decode(T.self, from: data)
return (data, httpResponse)
}
private static func extractErrorMessage(from data: Data) -> String? {
guard !data.isEmpty else { return nil }
if let envelope = try? JSONDecoder().decode(APIErrorEnvelope.self, from: data) {
let parts = [envelope.code, envelope.message]
.compactMap { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
if !parts.isEmpty {
return parts.joined(separator: " ")
}
}
if let text = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines),
!text.isEmpty {
return text
}
return nil
}
}
enum APIError: Error, LocalizedError {
case invalidURL
case invalidResponse
case httpError(Int)
case httpError(Int, String?)
case decodingError(Error)
case unsupportedFeature(String)
var errorDescription: String? {
switch self {
case .invalidURL: return "Invalid URL"
case .invalidResponse: return "Invalid response"
case .httpError(let code): return "HTTP Error: \(code)"
case .httpError(let code, let message):
if let message, !message.isEmpty {
return "HTTP Error: \(code)\n\(message)"
}
return "HTTP Error: \(code)"
case .decodingError(let error): return "Decoding error: \(error.localizedDescription)"
case .unsupportedFeature(let feature): return "\(feature) is not supported by this host"
}
}
}
private struct APIErrorEnvelope: Decodable {
let code: String?
let message: String?
}

View File

@@ -3,8 +3,19 @@ import UserNotifications
enum PingService {
private static var previousPingStates: [String: Bool] = [:]
private static var suppressedUntil: [String: Date] = [:]
static func suppressChecks(for hostname: String, duration: TimeInterval) {
suppressedUntil[hostname] = Date().addingTimeInterval(duration)
}
static func ping(hostname: String, apiKey: String, notificationsEnabled: Bool = true) async -> Bool {
if let suppressedUntil = suppressedUntil[hostname], suppressedUntil > Date() {
return false
} else {
suppressedUntil.removeValue(forKey: hostname)
}
guard let url = URL(string: "https://\(hostname)/api/v2/ping") else {
print("❌ [PingService] Invalid URL for \(hostname)")
return false
@@ -18,9 +29,6 @@ enum PingService {
do {
let (data, response) = try await URLSession.shared.data(for: request)
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
if let responseString = String(data: data, encoding: .utf8) {
print("❌ [PingService] HTTP \(httpResponse.statusCode): \(responseString)")
}
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
return false
}
@@ -33,7 +41,6 @@ enum PingService {
return false
}
} catch {
print("❌ [PingService] Error pinging \(hostname): \(error)")
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
return false
}

View File

@@ -185,6 +185,10 @@ struct ServerInfo: Codable, Hashable, Equatable {
].filter { !$0.isEmpty }
return components.isEmpty ? nil : components.joined(separator: "")
}
var supportsRestartCommand: Bool {
ServerInfo.version(apiVersion, isAtLeast: "2.14")
}
}
// MARK: - Helpers & Sample Data
@@ -226,6 +230,27 @@ extension ServerInfo {
return normalized
}
private static func version(_ value: String, isAtLeast minimum: String) -> Bool {
let lhs = value
.split(separator: ".")
.compactMap { Int($0) }
let rhs = minimum
.split(separator: ".")
.compactMap { Int($0) }
let count = max(lhs.count, rhs.count)
for index in 0..<count {
let left = index < lhs.count ? lhs[index] : 0
let right = index < rhs.count ? rhs[index] : 0
if left != right {
return left > right
}
}
return true
}
static let placeholder = ServerInfo(
hostname: "preview.example.com",
ipAddresses: ["192.168.1.1", "fe80::1"],

View File

@@ -181,7 +181,7 @@ class APIv2_12: BaseAPIClient, ServerAPIProtocol {
}
guard httpResponse.statusCode == 200 else {
throw APIError.httpError(httpResponse.statusCode)
throw APIError.httpError(httpResponse.statusCode, nil)
}
let decoder = JSONDecoder()
@@ -189,6 +189,10 @@ class APIv2_12: BaseAPIClient, ServerAPIProtocol {
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
return envelope.toDomain()
}
func restartServer(apiKey: String) async throws {
throw APIError.unsupportedFeature("Server reboot")
}
}
// MARK: - Server Summary Mapping

View File

@@ -181,7 +181,7 @@ class APIv2_13: BaseAPIClient, ServerAPIProtocol {
}
guard httpResponse.statusCode == 200 else {
throw APIError.httpError(httpResponse.statusCode)
throw APIError.httpError(httpResponse.statusCode, nil)
}
let decoder = JSONDecoder()
@@ -189,6 +189,10 @@ class APIv2_13: BaseAPIClient, ServerAPIProtocol {
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
return envelope.toDomain()
}
func restartServer(apiKey: String) async throws {
throw APIError.unsupportedFeature("Server reboot")
}
}
// MARK: - Server Summary Mapping

View File

@@ -0,0 +1,30 @@
//
// APIv2_14.swift
// iKeyMon
//
// Created by tracer on 19.04.26.
//
import Foundation
class APIv2_14: APIv2_13 {
private enum Endpoint: String {
case serverReboot = "/api/v2/server/reboot"
func url(baseURL: URL) -> URL {
baseURL.appendingPathComponent(rawValue)
}
}
override func restartServer(apiKey: String) async throws {
var request = URLRequest(url: Endpoint.serverReboot.url(baseURL: baseURL))
request.httpMethod = "POST"
request.setValue(apiKey, forHTTPHeaderField: "X-API-KEY")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 30
request.httpBody = #"{"confirm":true}"#.data(using: .utf8)
try await performRequestWithoutBody(request)
}
}