diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-11-27 23:38:04 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-11-27 23:38:04 +0100 |
| commit | 213710bf5bd6413c747bf126db50816ef5de5a6e (patch) | |
| tree | 912f8cf87955a08077c92fea8ad934f50b7ab975 /Hotline/Models/Server.swift | |
| parent | f466b21dc02f78c984ba6748e703f6780a7a0db4 (diff) | |
| parent | 6a95b53616a4abfa306ddce43151cf4fefbd20ed (diff) | |
Merge remote-tracking branch 'upstream/main'
Diffstat (limited to 'Hotline/Models/Server.swift')
| -rw-r--r-- | Hotline/Models/Server.swift | 66 |
1 files changed, 57 insertions, 9 deletions
diff --git a/Hotline/Models/Server.swift b/Hotline/Models/Server.swift index a3e3dde..bcac81f 100644 --- a/Hotline/Models/Server.swift +++ b/Hotline/Models/Server.swift @@ -9,13 +9,22 @@ struct Server: Codable { var port: Int var login: String var password: String - var autoconnect: Bool = false - - init( - name: String?, description: String?, address: String, - port: Int = HotlinePorts.DefaultServerPort, users: Int = 0, login: String? = nil, - password: String? = nil, autoconnect: Bool? = false - ) { + + var displayAddress: String { + if self.port == HotlinePorts.DefaultServerPort { + return self.address + } + else { + // Wrap IPv6 addresses in brackets when displaying with port + if self.address.contains(":") { + return "[\(self.address)]:\(String(self.port))" + } else { + return "\(self.address):\(String(self.port))" + } + } + } + + init(name: String?, description: String?, address: String, port: Int = HotlinePorts.DefaultServerPort, users: Int = 0, login: String? = nil, password: String? = nil) { self.name = name self.description = description self.address = address.lowercased() @@ -47,10 +56,49 @@ struct Server: Codable { } static func parseServerAddressAndPort(_ address: String) -> (String, Int) { - let url = URL(string: "hotline://\(address)") + let trimmed = address.trimmingCharacters(in: .whitespacesAndNewlines) + + // Check if this looks like an IPv6 address (contains colons but no port delimiter) + // IPv6 addresses can be: + // - fe80::1234 + // - [fe80::1234]:5500 (with port) + // - 2001:db8::1 + // - [2001:db8::1]:6500 (with port) + + // If it starts with [, it's bracketed IPv6 with optional port + if trimmed.hasPrefix("[") { + // Find the closing bracket + if let closeBracketIndex = trimmed.firstIndex(of: "]") { + let hostEndIndex = trimmed.index(after: closeBracketIndex) + let host = String(trimmed[trimmed.index(after: trimmed.startIndex)..<closeBracketIndex]) + + // Check if there's a port after the bracket + if hostEndIndex < trimmed.endIndex && trimmed[closeBracketIndex...].contains(":") { + let portString = trimmed[trimmed.index(after: hostEndIndex)...] + if let port = Int(portString) { + return (host.lowercased(), port) + } + } + + return (host.lowercased(), HotlinePorts.DefaultServerPort) + } + } + + // Check if it's an IPv6 address without brackets + // IPv6 addresses have multiple colons, IPv4:port has only one + // Note: IPv6 may also have a zone identifier like fe80::1%en1 + let colonCount = trimmed.filter { $0 == ":" }.count + if colonCount > 1 { + // This is likely an IPv6 address without a port + // Keep it as-is, including any zone identifier (e.g., %en1 for link-local) + return (trimmed.lowercased(), HotlinePorts.DefaultServerPort) + } + + // Otherwise use URL parsing for IPv4 or hostnames + let url = URL(string: "hotline://\(trimmed)") let port = url?.port ?? HotlinePorts.DefaultServerPort let host = url?.host(percentEncoded: false) ?? "" - return (host.lowercased().trimmingCharacters(in: .whitespacesAndNewlines), port) + return (host.lowercased(), port) } } |