feat: initial ui implementation

This commit is contained in:
Iain Learmonth 2026-04-13 14:18:42 +01:00
parent 1612ed099c
commit 13254d63c2
9 changed files with 709 additions and 31 deletions

71
dns/BlockedCount.swift Normal file
View file

@ -0,0 +1,71 @@
import SwiftUI
import Foundation
struct BlockedCount: View {
@State private var txtRecord: String = "..."
var body: some View {
Text(txtRecord)
.onAppear {
fetchTXTRecord()
}
}
func parseResponse(data: Data) -> String? {
// This is a DNS wire format response and we make a lot of assumptions
// It is not critical functionality so just let it fail if it fails
guard data.count > 44 else { return nil }
let startIndex = 44
let subData = data.suffix(from: startIndex)
// Find the first space character (ASCII 32)
var endIndex: Int?
for (offset, byte) in subData.enumerated() {
if byte == 32 { // Space character
endIndex = offset
break
}
}
guard let foundEndIndex = endIndex else { return nil }
// Extract bytes between start and space
let numberData = subData.prefix(foundEndIndex)
return String(decoding: numberData, as: UTF8.self)
}
func fetchTXTRecord() {
let dohURL = URL(string: "https://dns.sr2.uk/dns-query?dns=DoQBAAABAAAAAAAABXN0YXRzB2ludmFsaWQAABAAAQ")!
let request = URLRequest(url: dohURL)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
if error != nil {
txtRecord = "Error"
return
}
guard let data = data else {
txtRecord = "Error"
return
}
guard let count = parseResponse(data: data) else {
txtRecord = "Error"
return
}
txtRecord = count
}
}.resume()
}
}
#Preview {
BlockedCount()
}