diff --git a/Tun/Punchnet/DNSUtil.swift b/Tun/Punchnet/DNSUtil.swift new file mode 100644 index 0000000..e2a89b0 --- /dev/null +++ b/Tun/Punchnet/DNSUtil.swift @@ -0,0 +1,121 @@ +//// +//// 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.. UInt16 { +// let subdata = self[offset..