I’m making an attempt to setup an extension utilizing DNSProxyProvider that intercepts the DNS site visitors on UDP and inserts our customized machine identifier and ship it to our customized DNS Server which provides us the response which I ahead to the requesting shopper.
I’ve been capable of append the identifier with the area identify when sending out request to our customized DNS and I get the response again simply high-quality however when I attempt to write the response to the udpflow I get this error in Console Logs.
Error Area=NEAppProxyFlowErrorDomain Code=9 "The datagram was too massive" UserInfo={NSLocalizedDescription=The datagram was too massive}
Here’s what I’ve tried thus far.
- Truncating the datagram dimension to lower than 10 bytes.
- Sending in dummy Knowledge object whereas making an attempt to write down to the movement.
3)Double checking the Signing and Capabilities, for Targets, the App and Community Extension.
Hooked up beneath is code from my NEDNSProxyProvider. The DNS request is course of within the handleNewFlow perform which calls processUDPFlow
override func handleNewFlow(_ movement: NEAppProxyFlow) -> Bool {
if movement is NEAppProxyTCPFlow {
NSLog("BDDNSProxyProvider : Is TCP Movement...")
} else if let udpFlow = movement as? NEAppProxyUDPFlow {
NSLog("BDDNSProxyProvider: handleNewFlow : (udpFlow)")
processUDPFlow(udpFlow) // < --
}
return true
}
Within the code beneath I concatenate area identify within the request with deviceId and ship it to our server. Even have the Logs strains in, please ignore them.
// Learn incoming DNS packets from the shopper
personal func processUDPFlow(_ udpFlow: NEAppProxyUDPFlow) {
self.udpAppProxyFlow = udpFlow
udpFlow.readDatagrams { datagrams, error in
if let error = error {
NSLog("Error studying datagrams: (error.localizedDescription)")
return
}
guard let datagrams = datagrams else {
NSLog("No datagrams obtained.")
return
}
// Ahead every DNS packet to the customized DNS server
for (index, packet) in datagrams.enumerated() {
let dnsMessage = self.parseDNSMessage(from: packet.0)
NSLog("tDatagram Header: (dnsMessage.header)")
for query in dnsMessage.questions {
NSLog("tDatagram Query: (query.identify), Kind: (query.kind), Class: (query.klass)")
}
for reply in dnsMessage.solutions {
NSLog("tDatagram Reply: (reply.identify), Kind: (reply.kind), Knowledge: (reply.knowledge)")
}
let oldDomain = self.extractDomainName(from: packet.0)!
let packetWithNewDomain = self.replaceDomainName(in: packet.0, with: "827-(oldDomain)") // func to append machine ID (827)
NSLog("Packet's new area (self.extractDomainName(from: packetWithNewDomain ?? packet.0) ?? "Discovered nil")")
self.sendToCustomDNSServer(packetWithNewDomain!) { responseDatagram in
guard let responseDatagram = responseDatagram else {
NSLog("Didn't get a response from the customized DNS server")
return
}
let tDatagram = (responseDatagram, packet.1)
udpFlow.writeDatagrams([tDatagram]) { error in
if let error = error {
NSLog("Failed to write down DNS response again to shopper: (error)")
} else {
NSLog("Efficiently wrote DNS response again to shopper.")
}
}
}
}
// Proceed Studying Datagrams
self.processUDPFlow(udpFlow)
}
}
Following is the perform I take advantage of to exchange domainName
func extractDomainName(from datagram: Knowledge) -> String? {
// Make sure the datagram has sufficient knowledge for a DNS header
guard datagram.depend > 12 else { return nil }
// Begin studying after the header (12 bytes)
var offset = 12
var domainName = ""
whereas offset < datagram.depend {
// Learn the size of the following label
let size = Int(datagram[offset])
offset += 1
// Examine for the null terminator (finish of area identify)
if size == 0 {
break
}
// Guarantee there's sufficient knowledge for the label
guard offset + size <= datagram.depend else { return nil }
// Extract the label as a string
if let label = String(knowledge: datagram[offset..<offset + length], encoding: .utf8) {
// Append the label to the area identify
domainName += domainName.isEmpty ? label : "." + label
}
offset += size
}
return domainName.isEmpty ? nil : domainName
}
Every thing is falling into place apart from this final Error I get when I attempt to write again to movement. What am I lacking right here and the way can I resolve this challenge?
Any assist can be appreciated.
Thanks