diff options
| author | Dustin Mierau <dustin@mierau.me> | 2025-11-08 12:33:00 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2025-11-08 12:33:00 -0800 |
| commit | 286c408370681b022deaabd254d499aefec28add (patch) | |
| tree | 8dd76ca8e362513c34a444f921debcc28eab6a76 /Hotline/Models | |
| parent | 2f332ee497af925db1a2583135e9dfcdaff84794 (diff) | |
Some work on making it possible to connect to servers with an IPv6 address. Improve connect form a bit. Add Copy Link to context menu for servers and trackers.
Diffstat (limited to 'Hotline/Models')
| -rw-r--r-- | Hotline/Models/Server.swift | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/Hotline/Models/Server.swift b/Hotline/Models/Server.swift index 7d38752..3ad3375 100644 --- a/Hotline/Models/Server.swift +++ b/Hotline/Models/Server.swift @@ -15,7 +15,12 @@ struct Server: Codable { return self.address } else { - return "\(self.address):\(String(self.port))" + // 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))" + } } } @@ -50,10 +55,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) } } |