Skip to content

Commit

Permalink
Feature: watch system proxy change
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Oct 2, 2018
1 parent 02dfb50 commit c563805
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4868210EE6B9004554A0 /* ApiRequest.swift */; };
492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4870210EF62E004554A0 /* ClashConfig.swift */; };
4931969B21631E5E00A8E6E7 /* SpeedDataRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4931969A21631E5D00A8E6E7 /* SpeedDataRecorder.swift */; };
493196A2216331F400A8E6E7 /* NetworkChangeNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 493196A1216331F400A8E6E7 /* NetworkChangeNotifier.swift */; };
4949D154213242F600EF85E6 /* Paths.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4949D153213242F600EF85E6 /* Paths.swift */; };
4949D15D2132614B00EF85E6 /* QRCodeUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4949D15C2132614B00EF85E6 /* QRCodeUtil.swift */; };
4952C3BF2115C7CA004A4FA8 /* ProxyMenuItemFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4952C3BE2115C7CA004A4FA8 /* ProxyMenuItemFactory.swift */; };
Expand Down Expand Up @@ -106,6 +107,7 @@
492C4868210EE6B9004554A0 /* ApiRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = "<group>"; };
492C4870210EF62E004554A0 /* ClashConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConfig.swift; sourceTree = "<group>"; };
4931969A21631E5D00A8E6E7 /* SpeedDataRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeedDataRecorder.swift; sourceTree = "<group>"; };
493196A1216331F400A8E6E7 /* NetworkChangeNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkChangeNotifier.swift; sourceTree = "<group>"; };
4949D153213242F600EF85E6 /* Paths.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Paths.swift; sourceTree = "<group>"; };
4949D15C2132614B00EF85E6 /* QRCodeUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeUtil.swift; sourceTree = "<group>"; };
4952C3BE2115C7CA004A4FA8 /* ProxyMenuItemFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyMenuItemFactory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -246,6 +248,7 @@
children = (
49722FE9211F338B00650A41 /* Witness */,
49722FDE211ED2CF00650A41 /* INIParser */,
493196A1216331F400A8E6E7 /* NetworkChangeNotifier.swift */,
);
path = Vendor;
sourceTree = "<group>";
Expand Down Expand Up @@ -606,6 +609,7 @@
files = (
49CF3B5C20CE8068001EBF94 /* ProxyConfigManager.swift in Sources */,
4952C3D02117027C004A4FA8 /* ConfigFileFactory.swift in Sources */,
493196A2216331F400A8E6E7 /* NetworkChangeNotifier.swift in Sources */,
4997732520D251A60009B136 /* SWBApplication.m in Sources */,
49BC061C212931F4005A0FE7 /* AboutViewController.swift in Sources */,
4949D154213242F600EF85E6 /* Paths.swift in Sources */,
Expand Down
25 changes: 24 additions & 1 deletion ClashX/General/ConfigManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import RxSwift
class ConfigManager {

static let shared = ConfigManager()
private let disposeBag = DisposeBag()
var apiPort = "8080"
private init(){refreshApiPort()}
private init(){
refreshApiPort()
setupNetworkNotifier()
}

var currentConfig:ClashConfig?{
get {
Expand Down Expand Up @@ -108,6 +112,25 @@ class ConfigManager {
}
}

func setupNetworkNotifier() {
NetworkChangeNotifier.start()
NotificationCenter
.default
.rx
.notification(kSystemNetworkStatusDidChange)
.subscribeOn(MainScheduler.instance)
.bind{ _ in
let (http,https,socks) = NetworkChangeNotifier.currentSystemProxySetting()
let proxySetted =
http == (self.currentConfig?.port ?? 0) &&
https == (self.currentConfig?.port ?? 0) &&
socks == (self.currentConfig?.socketPort ?? 0)
if (self.proxyPortAutoSet && !proxySetted) {
self.proxyPortAutoSet = proxySetted
}
}.disposed(by: disposeBag)
}



}
1 change: 1 addition & 0 deletions ClashX/Macro/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import Foundation

let kShouldUpDateConfig = Notification.Name("kShouldUpDateConfig")
let kConfigFileChange = Notification.Name("kConfigFileChange")
let kSystemNetworkStatusDidChange = Notification.Name("kSystemNetworkStatusDidChange")

45 changes: 45 additions & 0 deletions ClashX/Vendor/NetworkChangeNotifier.swift
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)
}
}

0 comments on commit c563805

Please sign in to comment.