122 lines
3.6 KiB
Swift
122 lines
3.6 KiB
Swift
////
|
||
//// 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 }
|
||
// }
|
||
//}
|