SPB Git

Repository Public

SSHHostProfile.swift 3.9 KB 101 lines swift

1import Foundation23/// A connectable host shown in the app: either discovered in the SSH config or saved by the user.4public struct SSHHostProfile: Sendable, Codable, Hashable, Identifiable {5    /// Where a profile comes from.6    public enum Source: String, Sendable, Codable, Hashable {7        /// A concrete alias of the OpenSSH config; OpenSSH resolves its options itself.8        case sshConfig9        /// A profile created in the app.10        case manual11    }1213    public var id: UUID14    /// Display name.15    public var label: String16    /// Config alias (`source == .sshConfig`) or hostname / IP address (`source == .manual`).17    public var host: String18    public var user: String?19    public var port: Int?20    public var proxyJump: String?21    /// Optional user-defined group (folder) name.22    public var group: String?23    public var isFavorite: Bool24    public var source: Source2526    public init(27        id: UUID = UUID(),28        label: String,29        host: String,30        user: String? = nil,31        port: Int? = nil,32        proxyJump: String? = nil,33        group: String? = nil,34        isFavorite: Bool = false,35        source: Source = .manual36    ) {37        self.id = id38        self.label = label39        self.host = host40        self.user = user41        self.port = port42        self.proxyJump = proxyJump43        self.group = group44        self.isFavorite = isFavorite45        self.source = source46    }4748    /// Merges the hosts of the SSH config with previously saved profiles.49    ///50    /// Config hosts come first, in config order: connection fields are refreshed from the config while51    /// `id`, `label`, `group` and `isFavorite` of a saved `.sshConfig` profile with the same alias are kept.52    /// Saved `.sshConfig` profiles whose alias disappeared are dropped. Manual profiles follow, untouched.53    /// A config alias never seen before gets a stable identifier derived from the alias.54    public static func merge(configHosts: [SSHConfigHost], saved: [SSHHostProfile]) -> [SSHHostProfile] {55        var savedByAlias: [String: SSHHostProfile] = [:]56        for profile in saved where profile.source == .sshConfig && savedByAlias[profile.host] == nil {57            savedByAlias[profile.host] = profile58        }59        var seen = Set<String>()60        var result: [SSHHostProfile] = []61        for host in configHosts where seen.insert(host.alias).inserted {62            let previous = savedByAlias[host.alias]63            result.append(64                SSHHostProfile(65                    id: previous?.id ?? stableID(forAlias: host.alias),66                    label: previous?.label ?? host.alias,67                    host: host.alias,68                    user: host.user,69                    port: host.port,70                    proxyJump: host.proxyJump,71                    group: previous?.group,72                    isFavorite: previous?.isFavorite ?? false,73                    source: .sshConfig74                )75            )76        }77        result.append(contentsOf: saved.filter { $0.source == .manual })78        return result79    }8081    /// Deterministic identifier for a config alias (non-cryptographic FNV-1a; identity only).82    public static func stableID(forAlias alias: String) -> UUID {83        func fnv(_ seed: UInt64) -> UInt64 {84            var hash = seed85            for byte in ("sshconfig:" + alias).utf8 {86                hash ^= UInt64(byte)87                hash = hash &* 0x0000_0100_0000_01b388            }89            return hash90        }91        var bytes: [UInt8] = []92        for value in [fnv(0xcbf2_9ce4_8422_2325), fnv(0x8422_2325_cbf2_9ce4)] {93            bytes.append(contentsOf: (0..<8).map { UInt8(truncatingIfNeeded: value >> UInt64($0 * 8)) })94        }95        return UUID(uuid: (96            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],97            bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]98        ))99    }100}101
Files main
Menu

Theme