diff --git a/ClashX.xcodeproj/project.pbxproj b/ClashX.xcodeproj/project.pbxproj index 95301f63e..4f50052ce 100644 --- a/ClashX.xcodeproj/project.pbxproj +++ b/ClashX.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 0D318CDAC9464E01DAD1F92B /* Pods_ClashX.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */; }; 4913C82321157D0200F6B87C /* Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4913C82221157D0200F6B87C /* Notification.swift */; }; + 491E6203258A424D00313AEF /* CommonUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 491E61FD258A424500313AEF /* CommonUtils.m */; }; 492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4868210EE6B9004554A0 /* ApiRequest.swift */; }; 492C4871210EF62E004554A0 /* ClashConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492C4870210EF62E004554A0 /* ClashConfig.swift */; }; 493A9F282453E60400D35296 /* ProxyDelayHistoryMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 493A9F272453E60400D35296 /* ProxyDelayHistoryMenu.swift */; }; @@ -120,6 +121,8 @@ /* Begin PBXFileReference section */ 0358BF8F0CBD7FF9A7EA4E30 /* Pods_ClashX.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ClashX.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 4913C82221157D0200F6B87C /* Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notification.swift; sourceTree = ""; }; + 491E61FC258A424500313AEF /* CommonUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonUtils.h; sourceTree = ""; }; + 491E61FD258A424500313AEF /* CommonUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommonUtils.m; sourceTree = ""; }; 492C4868210EE6B9004554A0 /* ApiRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = ""; }; 492C4870210EF62E004554A0 /* ClashConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConfig.swift; sourceTree = ""; }; 493A9F272453E60400D35296 /* ProxyDelayHistoryMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyDelayHistoryMenu.swift; sourceTree = ""; }; @@ -463,6 +466,8 @@ F9A7C06A2306E874007163C7 /* ProxyConfigHelper */ = { isa = PBXGroup; children = ( + 491E61FC258A424500313AEF /* CommonUtils.h */, + 491E61FD258A424500313AEF /* CommonUtils.m */, 498960722340F21C00AFB7EC /* com.west2online.ClashX.ProxyConfigHelper.entitlements */, F9A7C06B2306E874007163C7 /* main.m */, F935B2F12307C802009E4D33 /* Helper-Launchd.plist */, @@ -713,6 +718,7 @@ files = ( F935B2F42307CD32009E4D33 /* ProxyConfigHelper.m in Sources */, F9A7C06C2306E874007163C7 /* main.m in Sources */, + 491E6203258A424D00313AEF /* CommonUtils.m in Sources */, F935B2FA23083EE6009E4D33 /* ProxySettingTool.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -963,7 +969,7 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "$(SRCROOT)/ProxyConfigHelper/Helper-Info.plist"; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 1.5; + MARKETING_VERSION = 1.6; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; OTHER_LDFLAGS = ( @@ -994,7 +1000,7 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "$(SRCROOT)/ProxyConfigHelper/Helper-Info.plist"; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 1.5; + MARKETING_VERSION = 1.6; MTL_FAST_MATH = YES; OTHER_LDFLAGS = ( "-sectcreate", diff --git a/ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme b/ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme index bb1879137..5fe716f38 100644 --- a/ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme +++ b/ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme @@ -27,8 +27,6 @@ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> - - - - + + - - + +NS_ASSUME_NONNULL_BEGIN + +@interface CommonUtils : NSObject ++ (NSString *)runCommand:(NSString *)path args:(nullable NSArray *)args; +@end + +NS_ASSUME_NONNULL_END diff --git a/ProxyConfigHelper/CommonUtils.m b/ProxyConfigHelper/CommonUtils.m new file mode 100644 index 000000000..b38aefed3 --- /dev/null +++ b/ProxyConfigHelper/CommonUtils.m @@ -0,0 +1,31 @@ +// +// CommonUtils.m +// ClashX +// +// Created by yicheng on 2020/4/2. +// Copyright © 2020 west2online. All rights reserved. +// + +#import "CommonUtils.h" + +@implementation CommonUtils ++ (NSString *)runCommand:(NSString *)path args:(nullable NSArray *)args { + NSTask *task = [[NSTask alloc] init]; + [task setLaunchPath:path]; + [task setArguments:args]; + + NSPipe *pipe = [NSPipe pipe]; + [task setStandardOutput: pipe]; + + NSFileHandle *file = [pipe fileHandleForReading]; + + [task launch]; + + NSData *data = [file readDataToEndOfFile]; + NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; +#if DEBUG + NSLog(@"%@",output); +#endif + return output; +} +@end diff --git a/ProxyConfigHelper/Helper-Info.plist b/ProxyConfigHelper/Helper-Info.plist index 4c5dda643..6c30d9112 100755 --- a/ProxyConfigHelper/Helper-Info.plist +++ b/ProxyConfigHelper/Helper-Info.plist @@ -9,7 +9,7 @@ CFBundleName com.west2online.ClashX.ProxyConfigHelper CFBundleShortVersionString - 1.5 + 1.6 CFBundleVersion 2 SMAuthorizedClients diff --git a/ProxyConfigHelper/ProxyConfigHelper.m b/ProxyConfigHelper/ProxyConfigHelper.m index 6cef676c2..a3f49ad05 100644 --- a/ProxyConfigHelper/ProxyConfigHelper.m +++ b/ProxyConfigHelper/ProxyConfigHelper.m @@ -91,15 +91,19 @@ - (void)getVersion:(stringReplyBlock)reply { - (void)enableProxyWithPort:(int)port socksPort:(int)socksPort error:(stringReplyBlock)reply { - ProxySettingTool *tool = [ProxySettingTool new]; - [tool enableProxyWithport:port socksPort:socksPort]; - reply(nil); + dispatch_async(dispatch_get_main_queue(), ^{ + ProxySettingTool *tool = [ProxySettingTool new]; + [tool enableProxyWithport:port socksPort:socksPort]; + reply(nil); + }); } - (void)disableProxy:(stringReplyBlock)reply { - ProxySettingTool *tool = [ProxySettingTool new]; - [tool disableProxy]; - reply(nil); + dispatch_async(dispatch_get_main_queue(), ^{ + ProxySettingTool *tool = [ProxySettingTool new]; + [tool disableProxy]; + reply(nil); + }); } @@ -107,14 +111,18 @@ - (void)restoreProxyWithCurrentPort:(int)port socksPort:(int)socksPort info:(NSDictionary *)dict error:(stringReplyBlock)reply { - ProxySettingTool *tool = [ProxySettingTool new]; - [tool restoreProxySettint:dict currentPort:port currentSocksPort:socksPort]; - reply(nil); + dispatch_async(dispatch_get_main_queue(), ^{ + ProxySettingTool *tool = [ProxySettingTool new]; + [tool restoreProxySettint:dict currentPort:port currentSocksPort:socksPort]; + reply(nil); + }); } - (void)getCurrentProxySetting:(dictReplyBlock)reply { - NSDictionary *info = [ProxySettingTool currentProxySettings]; - reply(info); + dispatch_async(dispatch_get_main_queue(), ^{ + NSDictionary *info = [ProxySettingTool currentProxySettings]; + reply(info); + }); } diff --git a/ProxyConfigHelper/ProxySettingTool.m b/ProxyConfigHelper/ProxySettingTool.m index dad02f03e..ff65922e2 100644 --- a/ProxyConfigHelper/ProxySettingTool.m +++ b/ProxyConfigHelper/ProxySettingTool.m @@ -9,10 +9,7 @@ #import "ProxySettingTool.h" #import #import - -#include -#include -#include +#import "CommonUtils.h" @interface ProxySettingTool() @property (nonatomic, assign) AuthorizationRef authRef; @@ -108,25 +105,16 @@ - (void)dealloc { + (NSString *)getUserHomePath { - SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR("com.west2online.ClashX.ProxyConfigHelper"), NULL, NULL); - CFStringRef CopyCurrentConsoleUsername(SCDynamicStoreRef store); - CFStringRef result; - uid_t uid; - result = SCDynamicStoreCopyConsoleUser(store, &uid, NULL); - if ((result != NULL) && CFEqual(result, CFSTR("loginwindow"))) { - CFRelease(result); - result = NULL; - CFRelease(store); + NSString *userName = [CommonUtils runCommand:@"/usr/bin/stat" args:@[@"-f",@"%Su",@"/dev/console"]]; + userName = [userName stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; + if (!userName) { return nil; } - if (result != NULL) { - CFRelease(result); - result = NULL; + NSString *path = [NSString stringWithFormat:@"/Users/%@", userName]; + if([NSFileManager.defaultManager fileExistsAtPath:path]) { + return path; } - CFRelease(store); - char *dir = getpwuid(uid)->pw_dir; - NSString *path = [NSString stringWithUTF8String:dir]; - return path; + return nil; } @@ -253,41 +241,6 @@ - (AuthorizationFlags)authFlags { return authFlags; } -/* -- (NSString *)setupAuth:(NSData *)authData { - if (authData.length == 0 || authData.length != kAuthorizationExternalFormLength) { - return @"PrivilegedTaskRunnerHelper: Authorization data is malformed"; - } - AuthorizationRef authRef; - - OSStatus status = AuthorizationCreateFromExternalForm([authData bytes],&authRef); - if (status != errAuthorizationSuccess) { - return @"AuthorizationCreateFromExternalForm fail"; - } - - NSString *authName = @"com.west2online.ClashX.ProxyConfigHelper.config"; - AuthorizationItem authItem = {authName.UTF8String, 0, NULL, 0}; - AuthorizationRights authRight = {1, &authItem}; - - AuthorizationFlags authFlags = [self authFlags]; - - status = AuthorizationCopyRights(authRef, &authRight, nil, authFlags, nil); - if (status != errAuthorizationSuccess) { - AuthorizationFree(authRef, authFlags); - return @"AuthorizationCopyRights fail"; - } - - OSStatus authErr = AuthorizationCreate(nil, kAuthorizationEmptyEnvironment, authFlags, &authRef); - - if (authErr != noErr) { - AuthorizationFree(authRef, authFlags); - return @"AuthorizationCreate fail"; - } - self.authRef = authRef; - return nil; -} - */ - - (void)localAuth { OSStatus myStatus; AuthorizationFlags myFlags = [self authFlags];