feat: add pac and filterInterface interface in proxyConfigHelper
This commit is contained in:
parent
9364197586
commit
106e2cd5b2
@ -9,4 +9,10 @@
|
||||
enum Settings {
|
||||
@UserDefault("mmdbDownloadUrl", defaultValue: "")
|
||||
static var mmdbDownloadUrl:String
|
||||
|
||||
@UserDefault("filterInterface", defaultValue: true)
|
||||
static var filterInterface:Bool
|
||||
|
||||
@UserDefault("usePacMode", defaultValue: false)
|
||||
static var usePacMode:Bool
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ class SystemProxyManager: NSObject {
|
||||
func enableProxy() {
|
||||
let port = ConfigManager.shared.currentConfig?.usedHttpPort ?? 0
|
||||
let socketPort = ConfigManager.shared.currentConfig?.usedSocksPort ?? 0
|
||||
SystemProxyManager.shared.enableProxy(port: port, socksPort: socketPort)
|
||||
enableProxy(port: port, socksPort: socketPort)
|
||||
}
|
||||
|
||||
func enableProxy(port: Int, socksPort: Int) {
|
||||
@ -57,7 +57,11 @@ class SystemProxyManager: NSObject {
|
||||
return
|
||||
}
|
||||
Logger.log("enableProxy", level: .debug)
|
||||
helper?.enableProxy(withPort: Int32(port), socksPort: Int32(socksPort), error: { error in
|
||||
helper?.enableProxy(withPort: Int32(port),
|
||||
socksPort: Int32(socksPort),
|
||||
pac:nil,
|
||||
filterInterface: Settings.filterInterface,
|
||||
error: { error in
|
||||
if let error = error {
|
||||
Logger.log("enableProxy \(error)", level: .error)
|
||||
}
|
||||
@ -74,7 +78,7 @@ class SystemProxyManager: NSObject {
|
||||
Logger.log("disableProxy", level: .debug)
|
||||
|
||||
if disableRestoreProxy || forceDisable {
|
||||
helper?.disableProxy { error in
|
||||
helper?.disableProxy(withFilterInterface: Settings.filterInterface) { error in
|
||||
if let error = error {
|
||||
Logger.log("disableProxy \(error)", level: .error)
|
||||
}
|
||||
@ -83,7 +87,7 @@ class SystemProxyManager: NSObject {
|
||||
return
|
||||
}
|
||||
|
||||
helper?.restoreProxy(withCurrentPort: Int32(port), socksPort: Int32(socksPort), info: savedProxyInfo, error: { error in
|
||||
helper?.restoreProxy(withCurrentPort: Int32(port), socksPort: Int32(socksPort), info: savedProxyInfo, filterInterface: Settings.filterInterface, error: { error in
|
||||
if let error = error {
|
||||
Logger.log("restoreProxy \(error)", level: .error)
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
<key>CFBundleName</key>
|
||||
<string>com.west2online.ClashX.ProxyConfigHelper</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.6</string>
|
||||
<string>1.7</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>2</string>
|
||||
<key>SMAuthorizedClients</key>
|
||||
|
@ -89,19 +89,21 @@ ProxyConfigRemoteProcessProtocol
|
||||
}
|
||||
|
||||
- (void)enableProxyWithPort:(int)port
|
||||
socksPort:(int)socksPort
|
||||
error:(stringReplyBlock)reply {
|
||||
socksPort:(int)socksPort
|
||||
pac:(NSString *)pac
|
||||
filterInterface:(BOOL)filterInterface
|
||||
error:(stringReplyBlock)reply {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
ProxySettingTool *tool = [ProxySettingTool new];
|
||||
[tool enableProxyWithport:port socksPort:socksPort];
|
||||
[tool enableProxyWithport:port socksPort:socksPort pacUrl:pac filterInterface:filterInterface];
|
||||
reply(nil);
|
||||
});
|
||||
}
|
||||
|
||||
- (void)disableProxy:(stringReplyBlock)reply {
|
||||
- (void)disableProxyWithFilterInterface:(BOOL)filterInterface reply:(stringReplyBlock)reply {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
ProxySettingTool *tool = [ProxySettingTool new];
|
||||
[tool disableProxy];
|
||||
[tool disableProxyWithfilterInterface:filterInterface];
|
||||
reply(nil);
|
||||
});
|
||||
}
|
||||
@ -110,10 +112,11 @@ ProxyConfigRemoteProcessProtocol
|
||||
- (void)restoreProxyWithCurrentPort:(int)port
|
||||
socksPort:(int)socksPort
|
||||
info:(NSDictionary *)dict
|
||||
filterInterface:(BOOL)filterInterface
|
||||
error:(stringReplyBlock)reply {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
ProxySettingTool *tool = [ProxySettingTool new];
|
||||
[tool restoreProxySettint:dict currentPort:port currentSocksPort:socksPort];
|
||||
[tool restoreProxySettint:dict currentPort:port currentSocksPort:socksPort filterInterface:filterInterface];
|
||||
reply(nil);
|
||||
});
|
||||
}
|
||||
|
@ -19,13 +19,17 @@ typedef void(^dictReplyBlock)(NSDictionary *);
|
||||
|
||||
- (void)enableProxyWithPort:(int)port
|
||||
socksPort:(int)socksPort
|
||||
pac:(NSString *)pac
|
||||
filterInterface:(BOOL)filterInterface
|
||||
error:(stringReplyBlock)reply;
|
||||
|
||||
- (void)disableProxy:(stringReplyBlock)reply;
|
||||
- (void)disableProxyWithFilterInterface:(BOOL)filterInterface
|
||||
reply:(stringReplyBlock)reply;
|
||||
|
||||
- (void)restoreProxyWithCurrentPort:(int)port
|
||||
socksPort:(int)socksPort
|
||||
info:(NSDictionary *)dict
|
||||
filterInterface:(BOOL)filterInterface
|
||||
error:(stringReplyBlock)reply;
|
||||
|
||||
- (void)getCurrentProxySetting:(dictReplyBlock)reply;
|
||||
|
@ -12,10 +12,15 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface ProxySettingTool : NSObject
|
||||
|
||||
- (void)enableProxyWithport:(int)port socksPort:(int)socksPort;
|
||||
- (void)disableProxy;
|
||||
- (void)enableProxyWithport:(int)port socksPort:(int)socksPort
|
||||
pacUrl:(NSString *)pacUrl
|
||||
filterInterface:(BOOL)filterInterFace;
|
||||
- (void)disableProxyWithfilterInterface:(BOOL)filterInterFace;
|
||||
|
||||
- (void)restoreProxySettint:(NSDictionary *)savedInfo currentPort:(int)port currentSocksPort:(int)socksPort;
|
||||
- (void)restoreProxySettint:(NSDictionary *)savedInfo
|
||||
currentPort:(int)port
|
||||
currentSocksPort:(int)socksPort
|
||||
filterInterface:(BOOL)filterInterface;
|
||||
+ (NSMutableDictionary<NSString *,NSDictionary *> *)currentProxySettings;
|
||||
|
||||
@end
|
||||
|
@ -27,25 +27,31 @@
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
- (void)enableProxyWithport:(int)port socksPort:(int)socksPort {
|
||||
- (void)enableProxyWithport:(int)port socksPort:(int)socksPort
|
||||
pacUrl:(NSString *)pacUrl
|
||||
filterInterface:(BOOL)filterInterface {
|
||||
|
||||
[self applySCNetworkSettingWithRef:^(SCPreferencesRef ref) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref devices:^(NSString *key, NSDictionary *dict) {
|
||||
[self enableProxySettings:ref interface:key port:port socksPort:socksPort];
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref filterInterface:filterInterface devices:^(NSString *key, NSDictionary *dict) {
|
||||
[self enableProxySettings:ref interface:key port:port socksPort:socksPort pac:pacUrl];
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)disableProxy {
|
||||
- (void)disableProxyWithfilterInterface:(BOOL)filterInterface {
|
||||
[self applySCNetworkSettingWithRef:^(SCPreferencesRef ref) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref devices:^(NSString *key, NSDictionary *dict) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref filterInterface:filterInterface devices:^(NSString *key, NSDictionary *dict) {
|
||||
[self disableProxySetting:ref interface:key];
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)restoreProxySettint:(NSDictionary *)savedInfo currentPort:(int)port currentSocksPort:(int)socksPort {
|
||||
- (void)restoreProxySettint:(NSDictionary *)savedInfo
|
||||
currentPort:(int)port
|
||||
currentSocksPort:(int)socksPort
|
||||
filterInterface:(BOOL)filterInterface{
|
||||
[self applySCNetworkSettingWithRef:^(SCPreferencesRef ref) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref devices:^(NSString *key, NSDictionary *dict) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref filterInterface:filterInterface devices:^(NSString *key, NSDictionary *dict) {
|
||||
NSDictionary *proxySetting = savedInfo[key];
|
||||
if (![proxySetting isKindOfClass:[NSDictionary class]]) {
|
||||
proxySetting = nil;
|
||||
@ -88,7 +94,7 @@
|
||||
+ (NSMutableDictionary<NSString *,NSDictionary *> *)currentProxySettings {
|
||||
__block NSMutableDictionary<NSString *,NSDictionary *> *info = [NSMutableDictionary dictionary];
|
||||
SCPreferencesRef ref = SCPreferencesCreate(nil, CFSTR("ClashX"), nil);
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref devices:^(NSString *key, NSDictionary *dev) {
|
||||
[ProxySettingTool getDiviceListWithPrefRef:ref filterInterface:YES devices:^(NSString *key, NSDictionary *dev) {
|
||||
NSDictionary *proxySettings = dev[(__bridge NSString *)kSCEntNetProxies];
|
||||
info[key] = [proxySettings copy];
|
||||
}];
|
||||
@ -141,11 +147,14 @@
|
||||
return ignoreList;
|
||||
}
|
||||
|
||||
- (NSDictionary *)getProxySetting:(BOOL)enable port:(int) port socksPort: (int)socksPort {
|
||||
- (NSDictionary *)getProxySetting:(BOOL)enable port:(int) port
|
||||
socksPort: (int)socksPort pac:(NSString *)pac {
|
||||
|
||||
NSMutableDictionary *proxySettings = [NSMutableDictionary dictionary];
|
||||
|
||||
NSString *ip = enable ? @"127.0.0.1" : @"";
|
||||
NSInteger enableInt = enable ? 1 : 0;
|
||||
NSInteger enablePac = [pac length] > 0;
|
||||
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesHTTPProxy] = ip;
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesHTTPEnable] = @(enableInt);
|
||||
@ -165,6 +174,13 @@
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesSOCKSPort] = nil;
|
||||
}
|
||||
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesProxyAutoConfigEnable] = @(enablePac);
|
||||
if (enablePac) {
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesProxyAutoConfigURLString] = pac;
|
||||
} else {
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesProxyAutoConfigURLString] = nil;
|
||||
}
|
||||
|
||||
if (enable) {
|
||||
proxySettings[(__bridge NSString *)kCFNetworkProxiesExceptionsList] = [self getIgnoreList];
|
||||
} else {
|
||||
@ -184,16 +200,17 @@
|
||||
- (void)enableProxySettings:(SCPreferencesRef)prefs
|
||||
interface:(NSString *)interfaceKey
|
||||
port:(int) port
|
||||
socksPort:(int) socksPort {
|
||||
socksPort:(int) socksPort
|
||||
pac:(NSString *)pac {
|
||||
|
||||
NSDictionary *proxySettings = [self getProxySetting:YES port:port socksPort:socksPort];
|
||||
NSDictionary *proxySettings = [self getProxySetting:YES port:port socksPort:socksPort pac:pac];
|
||||
[self setProxyConfig:prefs interface:interfaceKey proxySetting:proxySettings];
|
||||
|
||||
}
|
||||
|
||||
- (void)disableProxySetting:(SCPreferencesRef)prefs
|
||||
interface:(NSString *)interfaceKey {
|
||||
NSDictionary *proxySettings = [self getProxySetting:NO port:0 socksPort:0];
|
||||
NSDictionary *proxySettings = [self getProxySetting:NO port:0 socksPort:0 pac:nil];
|
||||
[self setProxyConfig:prefs interface:interfaceKey proxySetting:proxySettings];
|
||||
}
|
||||
|
||||
@ -206,12 +223,14 @@
|
||||
(__bridge CFDictionaryRef)proxySettings);
|
||||
}
|
||||
|
||||
+ (void)getDiviceListWithPrefRef:(SCPreferencesRef)ref devices:(void(^)(NSString *, NSDictionary *))callback {
|
||||
+ (void)getDiviceListWithPrefRef:(SCPreferencesRef)ref
|
||||
filterInterface:(BOOL)filterInterface
|
||||
devices:(void(^)(NSString *, NSDictionary *))callback {
|
||||
NSDictionary *sets = (__bridge NSDictionary *)SCPreferencesGetValue(ref, kSCPrefNetworkServices);
|
||||
for (NSString *key in [sets allKeys]) {
|
||||
NSMutableDictionary *dict = [sets objectForKey:key];
|
||||
NSString *hardware = [dict valueForKeyPath:@"Interface.Hardware"];
|
||||
if ([hardware isEqualToString:@"AirPort"]
|
||||
if (!filterInterface || [hardware isEqualToString:@"AirPort"]
|
||||
|| [hardware isEqualToString:@"Wi-Fi"]
|
||||
|| [hardware isEqualToString:@"Ethernet"]
|
||||
) {
|
||||
|
Loading…
Reference in New Issue
Block a user