@@ -185,21 +185,154 @@ class DNSManager {
185185 return
186186 }
187187
188- // Format DNS servers with port if specified
189- let formattedPrimary = formatDNSWithPort ( primary)
190- let formattedSecondary = secondary. isEmpty ? " " : formatDNSWithPort ( secondary )
188+ // Check if primary or secondary contains a port specification
189+ let primaryHasPort = primary. contains ( " : " )
190+ let secondaryHasPort = ! secondary. isEmpty && secondary . contains ( " : " )
191191
192+ // If no custom ports are specified, use the standard network setup method
193+ if !primaryHasPort && !secondaryHasPort {
194+ // Standard DNS servers without ports
195+ var servers = [ primary]
196+ if !secondary. isEmpty {
197+ servers. append ( secondary)
198+ }
199+
200+ setStandardDNS ( services: services, servers: servers, completion: completion)
201+ return
202+ }
203+
204+ // For DNS servers with custom ports, we need to modify the resolver configuration
205+ let resolverContent = createResolverContent ( primary, secondary)
206+
207+ // We'll use the existing executeWithAuthentication method which properly handles
208+ // authentication with Touch ID or admin password
209+ let createDirCmd = " sudo mkdir -p /etc/resolver "
210+ executeWithAuthentication ( command: createDirCmd) { dirSuccess in
211+ if !dirSuccess {
212+ print ( " Failed to create resolver directory " )
213+ completion ( false )
214+ return
215+ }
216+
217+ // Now write the resolver content
218+ let writeFileCmd = " echo ' \( resolverContent) ' | sudo tee /etc/resolver/custom > /dev/null "
219+ self . executeWithAuthentication ( command: writeFileCmd) { fileSuccess in
220+ if !fileSuccess {
221+ print ( " Failed to write resolver configuration " )
222+ completion ( false )
223+ return
224+ }
225+
226+ // Set permissions
227+ let permCmd = " sudo chmod 644 /etc/resolver/custom "
228+ self . executeWithAuthentication ( command: permCmd) { permSuccess in
229+ if !permSuccess {
230+ print ( " Failed to set resolver file permissions " )
231+ completion ( false )
232+ return
233+ }
234+
235+ // Also set standard DNS servers to ensure proper resolution
236+ let standardServers = self . formatDNSWithoutPorts ( primary, secondary)
237+ self . setStandardDNS ( services: services, servers: standardServers, completion: completion)
238+ }
239+ }
240+ }
241+ }
242+
243+ private func createResolverContent( _ primary: String , _ secondary: String ) -> String {
244+ var resolverContent = " # Custom DNS configuration with port \n "
245+
246+ // Add nameserver entries with port specification
247+ if primary. contains ( " : " ) {
248+ let components = primary. components ( separatedBy: " : " )
249+ if components. count == 2 , let port = Int ( components [ 1 ] ) {
250+ resolverContent += " nameserver \( components [ 0 ] ) \n "
251+ resolverContent += " port \( port) \n "
252+ }
253+ } else {
254+ resolverContent += " nameserver \( primary) \n "
255+ }
256+
257+ if !secondary. isEmpty {
258+ if secondary. contains ( " : " ) {
259+ let components = secondary. components ( separatedBy: " : " )
260+ if components. count == 2 , let port = Int ( components [ 1 ] ) {
261+ resolverContent += " nameserver \( components [ 0 ] ) \n "
262+ resolverContent += " port \( port) \n "
263+ }
264+ } else {
265+ resolverContent += " nameserver \( secondary) \n "
266+ }
267+ }
268+
269+ return resolverContent
270+ }
271+
272+ func disableDNS( completion: @escaping ( Bool ) -> Void ) {
273+ let services = findActiveServices ( )
274+ guard !services. isEmpty else {
275+ completion ( false )
276+ return
277+ }
278+
279+ // Remove any custom resolver configuration
280+ let removeResolverCmd = " sudo rm -f /etc/resolver/custom "
281+
282+ executeWithAuthentication ( command: removeResolverCmd) { _ in
283+ // Continue with normal DNS reset regardless of resolver removal success
284+ let dispatchGroup = DispatchGroup ( )
285+ var allSucceeded = true
286+
287+ for service in services {
288+ dispatchGroup. enter ( )
289+
290+ let command = " /usr/sbin/networksetup -setdnsservers ' \( service) ' empty "
291+
292+ self . executeWithAuthentication ( command: command) { success in
293+ if !success {
294+ allSucceeded = false
295+ }
296+ dispatchGroup. leave ( )
297+ }
298+ }
299+
300+ dispatchGroup. notify ( queue: . main) {
301+ completion ( allSucceeded)
302+ }
303+ }
304+ }
305+
306+ // Helper method to get DNS addresses without port specifications
307+ private func formatDNSWithoutPorts( _ primary: String , _ secondary: String ) -> [ String ] {
308+ var servers : [ String ] = [ ]
309+
310+ // Extract IP address without port
311+ if primary. contains ( " : " ) {
312+ servers. append ( primary. components ( separatedBy: " : " ) [ 0 ] )
313+ } else {
314+ servers. append ( primary)
315+ }
316+
317+ if !secondary. isEmpty {
318+ if secondary. contains ( " : " ) {
319+ servers. append ( secondary. components ( separatedBy: " : " ) [ 0 ] )
320+ } else {
321+ servers. append ( secondary)
322+ }
323+ }
324+
325+ return servers
326+ }
327+
328+ // Helper method to set standard DNS settings
329+ private func setStandardDNS( services: [ String ] , servers: [ String ] , completion: @escaping ( Bool ) -> Void ) {
192330 let dispatchGroup = DispatchGroup ( )
193331 var allSucceeded = true
194332
195333 for service in services {
196334 dispatchGroup. enter ( )
197335
198- var servers = [ formattedPrimary]
199- if !formattedSecondary. isEmpty {
200- servers. append ( formattedSecondary)
201- }
202-
203336 let dnsArgs = servers. joined ( separator: " " )
204337 let dnsCommand = " /usr/sbin/networksetup -setdnsservers ' \( service) ' \( dnsArgs) "
205338 let ipv6Command = " /usr/sbin/networksetup -setv6off ' \( service) '; /usr/sbin/networksetup -setv6automatic ' \( service) ' "
@@ -235,34 +368,6 @@ class DNSManager {
235368 return dnsServer
236369 }
237370
238- func disableDNS( completion: @escaping ( Bool ) -> Void ) {
239- let services = findActiveServices ( )
240- guard !services. isEmpty else {
241- completion ( false )
242- return
243- }
244-
245- let dispatchGroup = DispatchGroup ( )
246- var allSucceeded = true
247-
248- for service in services {
249- dispatchGroup. enter ( )
250-
251- let command = " /usr/sbin/networksetup -setdnsservers ' \( service) ' empty "
252-
253- executeWithAuthentication ( command: command) { success in
254- if !success {
255- allSucceeded = false
256- }
257- dispatchGroup. leave ( )
258- }
259- }
260-
261- dispatchGroup. notify ( queue: . main) {
262- completion ( allSucceeded)
263- }
264- }
265-
266371 private func executePrivilegedCommand( arguments: [ String ] ) -> Bool {
267372 let services = findActiveServices ( )
268373 guard !services. isEmpty else { return false }
0 commit comments