punchnet-macos/Tun/Punchnet/DNSUtil.swift
2025-12-10 13:20:15 +08:00

122 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

////
//// DNSUtil.swift
//// punchnet
////
//// Created by on 2025/12/9.
////
//
//import Foundation
//import Network
//
//struct DNSUtil {
// static let dnsServers: [String] = ["100.100.100.100"]
// // dns
// static let dnsDestIpAddr: UInt32 = 1684300900
//
// // dns
// static func isDnsRequestPacket(ipPacket: IPPacket) -> Bool {
// return ipPacket.header.destination == dnsDestIpAddr
// }
//
// // DNS Header
// struct DNSHeader {
// var id: UInt16
// var flags: UInt16
// var qdCount: UInt16
// var anCount: UInt16
// var nsCount: UInt16
// var arCount: UInt16
// }
//
// // DNS Question
// struct DNSQuestion {
// var name: String
// var type: UInt16
// var qclass: UInt16
// }
//
//
//
// // DNS Label
// func parseName(from data: Data, offset: inout Int) -> String {
// var labels: [String] = []
// while true {
// let length = Int(data[offset])
// offset += 1
// if length == 0 {
// break
// }
// let labelData = data[offset..<(offset + length)]
// if let label = String(data: labelData, encoding: .utf8) {
// labels.append(label)
// }
// offset += length
// }
// return labels.joined(separator: ".")
// }
//
// // DNS
// func parseDNSRequest(_ data: Data) -> (DNSHeader, [DNSQuestion])? {
// guard data.count >= 12 else { return nil } // DNS Header 12
//
// let header = DNSHeader(
// id: data.uint16(at: 0),
// flags: data.uint16(at: 2),
// qdCount: data.uint16(at: 4),
// anCount: data.uint16(at: 6),
// nsCount: data.uint16(at: 8),
// arCount: data.uint16(at: 10)
// )
//
// var offset = 12
// var questions: [DNSQuestion] = []
//
// for _ in 0..<header.qdCount {
// let name = parseName(from: data, offset: &offset)
// let type = data.uint16(at: offset)
// offset += 2
// let qclass = data.uint16(at: offset)
// offset += 2
//
// let question = DNSQuestion(name: name, type: type, qclass: qclass)
// questions.append(question)
// }
//
// return (header, questions)
// }
//
// //
// let dnsPacket: [UInt8] = [
// 0x12, 0x34, // Transaction ID
// 0x01, 0x00, // Flags
// 0x00, 0x01, // QDCOUNT
// 0x00, 0x00, // ANCOUNT
// 0x00, 0x00, // NSCOUNT
// 0x00, 0x00, // ARCOUNT
// 0x03, 0x77, 0x77, 0x77, // w w w
// 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, // g o o g l e
// 0x03, 0x63, 0x6f, 0x6d, // c o m
// 0x00, // End of name
// 0x00, 0x01, // QTYPE A
// 0x00, 0x01 // QCLASS IN
// ]
//
// if let data = Data(exactly: dnsPacket), let (header, questions) = parseDNSRequest(data) {
// print("Transaction ID: \(header.id)")
// print("Flags: \(header.flags)")
// print("Questions count: \(header.qdCount)")
// for q in questions {
// print("Question: \(q.name), type: \(q.type), class: \(q.qclass)")
// }
// }
//
//}
//
//// Helper
//private extension Data {
// func uint16(at offset: Int) -> UInt16 {
// let subdata = self[offset..<offset+2]
// return subdata.withUnsafeBytes { $0.load(as: UInt16.self).bigEndian }
// }
//}