chore: delete DNSConfiguration
source: https://github.com/ivanstegic/menu-bar-dns
This commit is contained in:
parent
1c978cddc9
commit
16390f3eaa
|
@ -13,7 +13,6 @@
|
|||
018F88F9286DD0CB004DD0F7 /* DualTitleMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */; };
|
||||
01943259287D19BC008CC51A /* ClashRuleProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01943258287D19BC008CC51A /* ClashRuleProvider.swift */; };
|
||||
019A239628657A7A00AE5698 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 019A239528657A7A00AE5698 /* main.swift */; };
|
||||
01A645D3292C769D00B37FA2 /* DNSConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01A645D2292C759100B37FA2 /* DNSConfiguration.swift */; };
|
||||
01B009AE2854533300B93618 /* geoip.dat.gz in Resources */ = {isa = PBXBuildFile; fileRef = 01B009AC2854533200B93618 /* geoip.dat.gz */; };
|
||||
01B009AF2854533300B93618 /* geosite.dat.gz in Resources */ = {isa = PBXBuildFile; fileRef = 01B009AD2854533300B93618 /* geosite.dat.gz */; };
|
||||
01BC9ABE2928EB5A00F9B177 /* MetaDNS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01BC9ABD2928E5C600F9B177 /* MetaDNS.swift */; };
|
||||
|
@ -143,7 +142,6 @@
|
|||
018F88F8286DD0CB004DD0F7 /* DualTitleMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DualTitleMenuItem.swift; sourceTree = "<group>"; };
|
||||
01943258287D19BC008CC51A /* ClashRuleProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashRuleProvider.swift; sourceTree = "<group>"; };
|
||||
019A239528657A7A00AE5698 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
|
||||
01A645D2292C759100B37FA2 /* DNSConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSConfiguration.swift; sourceTree = "<group>"; };
|
||||
01B009AC2854533200B93618 /* geoip.dat.gz */ = {isa = PBXFileReference; lastKnownFileType = archive.gzip; path = geoip.dat.gz; sourceTree = "<group>"; };
|
||||
01B009AD2854533300B93618 /* geosite.dat.gz */ = {isa = PBXFileReference; lastKnownFileType = archive.gzip; path = geosite.dat.gz; sourceTree = "<group>"; };
|
||||
01BC9ABD2928E5C600F9B177 /* MetaDNS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetaDNS.swift; sourceTree = "<group>"; };
|
||||
|
@ -545,7 +543,6 @@
|
|||
019A239528657A7A00AE5698 /* main.swift */,
|
||||
0162E74E2864B819007218A6 /* MetaTask.swift */,
|
||||
01BC9ABD2928E5C600F9B177 /* MetaDNS.swift */,
|
||||
01A645D2292C759100B37FA2 /* DNSConfiguration.swift */,
|
||||
F935B2F12307C802009E4D33 /* Helper-Launchd.plist */,
|
||||
F935B2EA2307B6BA009E4D33 /* Helper-Info.plist */,
|
||||
F935B2F22307CD32009E4D33 /* ProxyConfigHelper.h */,
|
||||
|
@ -792,7 +789,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
F935B2F42307CD32009E4D33 /* ProxyConfigHelper.m in Sources */,
|
||||
01A645D3292C769D00B37FA2 /* DNSConfiguration.swift in Sources */,
|
||||
0162E74F2864B819007218A6 /* MetaTask.swift in Sources */,
|
||||
019A239628657A7A00AE5698 /* main.swift in Sources */,
|
||||
491E6203258A424D00313AEF /* CommonUtils.m in Sources */,
|
||||
|
|
|
@ -1,104 +0,0 @@
|
|||
//
|
||||
// DNSConfiguration.swift
|
||||
// ClashX
|
||||
|
||||
|
||||
|
||||
import Cocoa
|
||||
import SystemConfiguration
|
||||
|
||||
// https://github.com/ivanstegic/menu-bar-dns/blob/main/Menu%20Bar%20DNS/Menu%20Bar%20DNS/DNSConfiguration.swift
|
||||
|
||||
class DNSConfiguration: NSObject {
|
||||
|
||||
static let DNSConfigurationTypeKey = Bundle.main.bundleIdentifier! + ".dns"
|
||||
static let ServiceTypeWiFi = "IEEE80211"
|
||||
static let ServiceTypeEthernet = "Ethernet"
|
||||
|
||||
static func getnterfaceTypeByServiceIDs(_ services: Array<SCNetworkService>) throws -> Dictionary<String, String> {
|
||||
let allServicesIDsAndInterfaceType = try services.map { service -> (String, String) in
|
||||
guard
|
||||
let id = SCNetworkServiceGetServiceID(service) as String?,
|
||||
let interface = SCNetworkServiceGetInterface(service),
|
||||
let interfaceType = SCNetworkInterfaceGetInterfaceType(interface) as String?
|
||||
else {
|
||||
throw SCCopyLastError()
|
||||
}
|
||||
return (id, interfaceType)
|
||||
}
|
||||
return Dictionary(uniqueKeysWithValues: allServicesIDsAndInterfaceType)
|
||||
}
|
||||
|
||||
static func isConnectedService(_ service : SCNetworkService) throws -> Bool {
|
||||
|
||||
guard
|
||||
let id = SCNetworkServiceGetServiceID(service) as String?
|
||||
else {
|
||||
throw SCCopyLastError()
|
||||
}
|
||||
|
||||
let dynmaicStore = SCDynamicStoreCreate(kCFAllocatorSystemDefault, "DNSSETTING" as CFString, nil, nil)
|
||||
let serviceStateIPv4Key = "State:/Network/Service/\(id)/IPv4" as CFString
|
||||
let value = SCDynamicStoreCopyValue(dynmaicStore, serviceStateIPv4Key) as CFPropertyList?
|
||||
return value != nil
|
||||
}
|
||||
|
||||
static func getDNSForServiceID(_ serviceID:String) -> [String] {
|
||||
let serviceDNSKey = "State:/Network/Service/\(serviceID)/DNS" as CFString
|
||||
let serviceSetupDNSKey = "Setup:/Network/Service/\(serviceID)/DNS" as CFString
|
||||
let dynmaicStore = SCDynamicStoreCreate(kCFAllocatorSystemDefault, "DNSSETTING" as CFString, nil, nil)
|
||||
var allDNSIPAddresses : Array<String> = []
|
||||
|
||||
let dynamicPlist = SCDynamicStoreCopyValue(dynmaicStore, serviceDNSKey)
|
||||
let manualAddressPlist = SCDynamicStoreCopyValue(dynmaicStore, serviceSetupDNSKey)
|
||||
|
||||
if let dnsValues = manualAddressPlist?[kSCPropNetDNSServerAddresses] as? [String] {
|
||||
allDNSIPAddresses += dnsValues
|
||||
}
|
||||
/*
|
||||
if let dhcpValues = dynamicPlist?[kSCPropNetDNSServerAddresses] as? [String] {
|
||||
let uniqueValues = Array(Set(dhcpValues))
|
||||
for dhcpValue in uniqueValues {
|
||||
let newvalue = dhcpValue.appending(" (via DHCP)")
|
||||
allDNSIPAddresses.append(newvalue)
|
||||
}
|
||||
}
|
||||
*/
|
||||
return allDNSIPAddresses
|
||||
}
|
||||
|
||||
static func getAddresses() -> (Array<String>, Array<String>) {
|
||||
var ethernetDNSAddresses : Array<String> = []
|
||||
var WiFiDNSAddresses : Array<String> = []
|
||||
|
||||
do {
|
||||
|
||||
let prefs = SCPreferencesCreate(
|
||||
nil, DNSConfigurationTypeKey as NSString,
|
||||
nil
|
||||
) as SCPreferences?
|
||||
let allServicesCF = SCNetworkServiceCopyAll(prefs!)
|
||||
let allServices = allServicesCF as? [SCNetworkService]
|
||||
|
||||
let allConnectedServices = try allServices?.filter({ (service) -> Bool in
|
||||
return try isConnectedService(service)
|
||||
})
|
||||
let serviceTypeByIDs = try getnterfaceTypeByServiceIDs(allConnectedServices!) as Dictionary<String, String>?
|
||||
for (id, type) in serviceTypeByIDs! {
|
||||
switch (type) {
|
||||
case ServiceTypeWiFi:
|
||||
WiFiDNSAddresses += getDNSForServiceID(id)
|
||||
case ServiceTypeEthernet:
|
||||
ethernetDNSAddresses += getDNSForServiceID(id)
|
||||
default:
|
||||
print("")
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
return ([], [])
|
||||
}
|
||||
return (ethernetDNSAddresses, WiFiDNSAddresses)
|
||||
}
|
||||
|
||||
}
|
|
@ -69,7 +69,7 @@ class MetaDNS: NSObject {
|
|||
func getAllDns() -> [String: [String]] {
|
||||
var re = [String: [String]]()
|
||||
|
||||
guard let prefs = SCPreferencesCreate(nil, "ClashX" as CFString, nil),
|
||||
guard let prefs = SCPreferencesCreate(nil, "com.metacubex.ClashX.ProxyConfigHelper.preferences" as CFString, nil),
|
||||
let values = SCPreferencesGetValue(prefs, kSCPrefNetworkServices) as? [String: AnyObject] else {
|
||||
return re
|
||||
}
|
||||
|
@ -79,12 +79,20 @@ class MetaDNS: NSObject {
|
|||
}.filter {
|
||||
["AirPort", "Wi-Fi", "Ethernet"].contains($0.value)
|
||||
}.forEach {
|
||||
re[$0.key] = DNSConfiguration.getDNSForServiceID($0.key)
|
||||
re[$0.key] = getDNSForServiceID($0.key)
|
||||
}
|
||||
|
||||
return re
|
||||
}
|
||||
|
||||
func getDNSForServiceID(_ serviceID:String) -> [String] {
|
||||
let serviceSetupDNSKey = "Setup:/Network/Service/\(serviceID)/DNS" as CFString
|
||||
let dynmaicStore = SCDynamicStoreCreate(kCFAllocatorSystemDefault, "com.metacubex.ClashX.ProxyConfigHelper.dns" as CFString, nil, nil)
|
||||
|
||||
return SCDynamicStoreCopyValue(dynmaicStore, serviceSetupDNSKey)?[kSCPropNetDNSServerAddresses] as? [String] ?? []
|
||||
}
|
||||
|
||||
|
||||
@objc func flushDnsCache() {
|
||||
CommonUtils.runCommand("/usr/bin/killall", args: ["-HUP", "mDNSResponder"])
|
||||
|
||||
|
@ -133,7 +141,7 @@ class MetaDNS: NSObject {
|
|||
|
||||
let config = SCNetworkProtocolGetConfiguration(protoc)
|
||||
|
||||
var dic = (config as NSDictionary?)?.mutableCopy() as? NSMutableDictionary ?? NSMutableDictionary()
|
||||
let dic = (config as NSDictionary?)?.mutableCopy() as? NSMutableDictionary ?? NSMutableDictionary()
|
||||
|
||||
dic["ServerAddresses"] = dns
|
||||
|
||||
|
|
Loading…
Reference in New Issue