Skip to content

Commit

Permalink
fix: fix daemon crash in some case
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Dec 17, 2020
1 parent 16167b1 commit 3cb5d25
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 75 deletions.
10 changes: 8 additions & 2 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
491E61FC258A424500313AEF /* CommonUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommonUtils.h; sourceTree = "<group>"; };
491E61FD258A424500313AEF /* CommonUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommonUtils.m; sourceTree = "<group>"; };
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>"; };
493A9F272453E60400D35296 /* ProxyDelayHistoryMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyDelayHistoryMenu.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions ClashX.xcodeproj/xcshareddata/xcschemes/ClashX.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,8 +36,8 @@
ReferencedContainer = "container:ClashX.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -61,8 +59,6 @@
ReferencedContainer = "container:ClashX.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
17 changes: 17 additions & 0 deletions ProxyConfigHelper/CommonUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CommonUtils.h
// ClashX
//
// Created by yicheng on 2020/4/2.
// Copyright © 2020 west2online. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface CommonUtils : NSObject
+ (NSString *)runCommand:(NSString *)path args:(nullable NSArray *)args;
@end

NS_ASSUME_NONNULL_END
31 changes: 31 additions & 0 deletions ProxyConfigHelper/CommonUtils.m
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ProxyConfigHelper/Helper-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleName</key>
<string>com.west2online.ClashX.ProxyConfigHelper</string>
<key>CFBundleShortVersionString</key>
<string>1.5</string>
<string>1.6</string>
<key>CFBundleVersion</key>
<string>2</string>
<key>SMAuthorizedClients</key>
Expand Down
30 changes: 19 additions & 11 deletions ProxyConfigHelper/ProxyConfigHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,38 @@ - (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);
});
}


- (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);
});
}


Expand Down
63 changes: 8 additions & 55 deletions ProxyConfigHelper/ProxySettingTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
#import "ProxySettingTool.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import <AppKit/AppKit.h>

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#import "CommonUtils.h"

@interface ProxySettingTool()
@property (nonatomic, assign) AuthorizationRef authRef;
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 3cb5d25

Please sign in to comment.