forked from alvincrisuy/clashX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02dfb50
commit c563805
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Network Change Notifier | ||
* | ||
* Made by François 'ftiff' Levaux-Tiffreau - [email protected] | ||
* | ||
* | ||
* Simply modify "changed" | ||
* | ||
*/ | ||
|
||
import Cocoa | ||
import SystemConfiguration | ||
import Foundation | ||
|
||
class NetworkChangeNotifier { | ||
static func start(){ | ||
let changed: SCDynamicStoreCallBack = {_,_,_ in | ||
print("Network configuration changed") | ||
NotificationCenter.default.post(name: kSystemNetworkStatusDidChange, object: nil) | ||
} | ||
|
||
var dynamicContext = SCDynamicStoreContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil) | ||
let dcAddress = withUnsafeMutablePointer(to:&dynamicContext, {UnsafeMutablePointer<SCDynamicStoreContext>($0)}) | ||
|
||
if let dynamicStore = SCDynamicStoreCreate(kCFAllocatorDefault, "io.fti.networkconfigurationchanged" as CFString, changed, dcAddress){ | ||
let keys: [CFString] = ["State:/Network/Global/IPv4" as CFString] | ||
|
||
SCDynamicStoreSetNotificationKeys(dynamicStore, keys as CFArray, nil) | ||
|
||
let loop = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynamicStore, 0) | ||
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode) | ||
|
||
CFRunLoopRun() | ||
} | ||
} | ||
|
||
static func currentSystemProxySetting() -> (UInt,UInt,UInt) { | ||
let proxiesSetting = CFNetworkCopySystemProxySettings()?.takeRetainedValue() as! [String:AnyObject] | ||
let httpProxy = proxiesSetting[kCFNetworkProxiesHTTPPort as String] as? UInt ?? 0 | ||
let socksProxy = proxiesSetting[kCFNetworkProxiesSOCKSPort as String]as? UInt ?? 0 | ||
let httpsProxy = proxiesSetting[kCFNetworkProxiesHTTPSPort as String]as? UInt ?? 0 | ||
return (httpProxy,httpsProxy,socksProxy) | ||
} | ||
} | ||
|