This commit is contained in:
anlicheng 2025-12-10 13:20:15 +08:00
parent 28219a3bb3
commit 0df890a699
3 changed files with 149 additions and 9 deletions

121
Tun/Punchnet/DNSUtil.swift Normal file
View File

@ -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..<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 }
// }
//}

View File

@ -259,7 +259,7 @@ public class SDLContext: @unchecked Sendable {
}
// tun
await self.didNetworkConfigChanged(devAddr: self.devAddr)
await self.didNetworkConfigChanged(devAddr: self.devAddr, dnsServers: ["100.100.100.100"])
self.aesKey = aesKey
if upgradeType == .normal {
@ -315,7 +315,7 @@ public class SDLContext: @unchecked Sendable {
self.devAddr = changeNetworkCommand.devAddr
// tun
await self.didNetworkConfigChanged(devAddr: self.devAddr)
await self.didNetworkConfigChanged(devAddr: self.devAddr, dnsServers: ["100.100.100.100"])
self.aesKey = aesKey
var commandAck = SDLCommandAck()
@ -433,21 +433,24 @@ public class SDLContext: @unchecked Sendable {
// }
//
private func didNetworkConfigChanged(devAddr: SDLDevAddr, dnsServers: [String]? = nil) async {
private func didNetworkConfigChanged(devAddr: SDLDevAddr, dnsServers: [String]) async {
let netAddress = SDLNetAddress(ip: devAddr.netAddr, maskLen: UInt8(devAddr.netBitLen))
let routes = [Route(dstAddress: netAddress.networkAddress, subnetMask: netAddress.maskAddress)]
let routes = [
Route(dstAddress: netAddress.networkAddress, subnetMask: netAddress.maskAddress),
Route(dstAddress: "100.100.100.100", subnetMask: "255.255.255.255")
]
// Add code here to start the process of connecting the tunnel.
let networkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "8.8.8.8")
networkSettings.mtu = 1460
// DNS
if let dnsServers {
networkSettings.dnsSettings = NEDNSSettings(servers: dnsServers)
} else {
networkSettings.dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "114.114.114.114"])
}
let dnsSettings = NEDNSSettings(servers: dnsServers)
dnsSettings.searchDomains = ["punchnet.ts.net"]
dnsSettings.matchDomains = ["punchnet.ts.net"]
dnsSettings.matchDomainsNoSearch = false
networkSettings.dnsSettings = dnsSettings
self.logger.log("[SDLContext] Tun started at network ip: \(netAddress.ipAddress), mask: \(netAddress.maskAddress)", level: .info)
let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress])
@ -483,6 +486,10 @@ public class SDLContext: @unchecked Sendable {
let (packets, numbers) = await self.provider.packetFlow.readPackets()
for (data, number) in zip(packets, numbers) where number == 2 {
if let packet = IPPacket(data) {
let destIp = packet.header.destination_ip
NSLog("destIp: \(destIp), int: \(packet.header.destination)")
Task.detached {
let dstIp = packet.header.destination
// , ip

12
docs.md Normal file
View File

@ -0,0 +1,12 @@
1. 查看dns的设置
networksetup -getdnsservers Wi-Fi
scutil --dns
2. 修改系统的dns设置
networksetup -setdnsservers Wi-Fi 8.8.8.8 1.1.1.1
恢复为自动获取
networksetup -setdnsservers Wi-Fi empty