Look like working now.

This commit is contained in:
Charlie Qiu
2016-06-10 03:59:27 +08:00
parent 250dfc7b38
commit ccd666bc2a
34 changed files with 1774 additions and 231 deletions

View File

@ -43,6 +43,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
let defaults = NSUserDefaults.standardUserDefaults()
defaults.registerDefaults([
"ShadowsocksOn": true,
"ShadowsocksRunningMode": "auto",
"LocalSocks5.ListenPort": NSNumber(unsignedShort: 1086),
"LocalSocks5.ListenAddress": "localhost",
"LocalSocks5.Timeout": NSNumber(unsignedInteger: 60),
@ -108,13 +109,37 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
updateMainMenu()
SyncSSLocal()
updateRunningModeMenu()
ProxyConfHelper.install()
applyConfig()
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
func applyConfig() {
let defaults = NSUserDefaults.standardUserDefaults()
let isOn = defaults.boolForKey("ShadowsocksOn")
let mode = defaults.stringForKey("ShadowsocksRunningMode")
if isOn {
StartSSLocal()
if mode == "auto" {
autoModeMenuItem.state = 1
globalModeMenuItem.state = 0
ProxyConfHelper.enablePACProxy()
} else if mode == "global" {
autoModeMenuItem.state = 0
globalModeMenuItem.state = 1
ProxyConfHelper.enableGlobalProxy()
}
} else {
StopSSLocal()
ProxyConfHelper.disableProxy()
}
}
@IBAction func toggleRunning(sender: NSMenuItem) {
let defaults = NSUserDefaults.standardUserDefaults()
var isOn = defaults.boolForKey("ShadowsocksOn")
@ -123,11 +148,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
updateMainMenu()
if isOn {
StartSSLocal()
} else {
StopSSLocal()
}
applyConfig()
}
@IBAction func updateGFWList(sender: NSMenuItem) {
@ -173,11 +194,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
@IBAction func selectPACMode(sender: NSMenuItem) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("auto", forKey: "ShadowsocksRunningMode")
updateRunningModeMenu()
applyConfig()
}
@IBAction func selectGlobalMode(sender: NSMenuItem) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("global", forKey: "ShadowsocksRunningMode")
updateRunningModeMenu()
applyConfig()
}
@IBAction func editServerPreferences(sender: NSMenuItem) {
@ -215,6 +242,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
}
func updateRunningModeMenu() {
let defaults = NSUserDefaults.standardUserDefaults()
let mode = defaults.stringForKey("ShadowsocksRunningMode")
if mode == "auto" {
autoModeMenuItem.state = 1
globalModeMenuItem.state = 0
} else if mode == "global" {
autoModeMenuItem.state = 0
globalModeMenuItem.state = 1
}
}
func updateMainMenu() {
let defaults = NSUserDefaults.standardUserDefaults()
let isOn = defaults.boolForKey("ShadowsocksOn")

View File

@ -31,7 +31,7 @@ func generateSSLocalLauchAgentPlist() -> Bool {
let enableUdpRelay = NSUserDefaults.standardUserDefaults().boolForKey("LocalSocks5.EnableUDPRelay")
var arguments = [sslocalPath, "-c", "ss-local-config.json", "--fast-open"]
var arguments = [sslocalPath, "-c", "ss-local-config.json"]
if enableUdpRelay {
arguments.append("-u")
}

View File

@ -66,13 +66,13 @@ func GeneratePACFile() -> Bool {
// Filter empty and comment lines
lines = lines.filter({ (s: String) -> Bool in
if s.isEmpty {
return true
return false
}
let c = s[s.startIndex]
if c == "!" || c == "[" {
return true
return false
}
return false
return true
})
do {

View File

@ -0,0 +1,21 @@
//
// ProxyConfHelper.h
// ShadowsocksX-NG
//
// Created by 邱宇舟 on 16/6/10.
// Copyright © 2016年 qiuyuzhou. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ProxyConfHelper : NSObject
+ (void)install;
+ (void)enablePACProxy;
+ (void)enableGlobalProxy;
+ (void)disableProxy;
@end

View File

@ -0,0 +1,117 @@
//
// ProxyConfHelper.m
// ShadowsocksX-NG
//
// Created by on 16/6/10.
// Copyright © 2016 qiuyuzhou. All rights reserved.
//
#import "ProxyConfHelper.h"
#import "proxy_conf_helper_version.h"
#define kShadowsocksHelper @"/Library/Application Support/ShadowsocksX-NG/proxy_conf_helper"
@implementation ProxyConfHelper
+ (BOOL)isVersionOk {
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:kShadowsocksHelper];
NSArray *args;
args = [NSArray arrayWithObjects:@"-v", nil];
[task setArguments: args];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *fd;
fd = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [fd readDataToEndOfFile];
NSString *str;
str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (![str isEqualToString:kProxyConfHelperVersion]) {
return NO;
}
return YES;
}
+ (void)install {
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:kShadowsocksHelper] || ![self isVersionOk]) {
NSString *helperPath = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"install_helper.sh"];
NSLog(@"run install script: %@", helperPath);
NSDictionary *error;
NSString *script = [NSString stringWithFormat:@"do shell script \"bash %@\" with administrator privileges", helperPath];
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
if ([appleScript executeAndReturnError:&error]) {
NSLog(@"installation success");
} else {
NSLog(@"installation failure");
}
}
}
+ (void)callHelper:(NSArray*) arguments {
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:kShadowsocksHelper];
// this log is very important
NSLog(@"run shadowsocks helper: %@", kShadowsocksHelper);
[task setArguments:arguments];
NSPipe *stdoutpipe;
stdoutpipe = [NSPipe pipe];
[task setStandardOutput:stdoutpipe];
NSPipe *stderrpipe;
stderrpipe = [NSPipe pipe];
[task setStandardError:stderrpipe];
NSFileHandle *file;
file = [stdoutpipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (string.length > 0) {
NSLog(@"%@", string);
}
file = [stderrpipe fileHandleForReading];
data = [file readDataToEndOfFile];
string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (string.length > 0) {
NSLog(@"%@", string);
}
}
+ (void)enablePACProxy {
NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NE/gfwlist.js", NSHomeDirectory()];
NSURL* url = [NSURL fileURLWithPath:urlString];
[self callHelper:@[@"--mode", @"auto", @"--pac-url", [url absoluteString]]];
}
+ (void)enableGlobalProxy {
NSUInteger port = [[NSUserDefaults standardUserDefaults]integerForKey:@"LocalSocks5.ListenPort"];
[self callHelper:@[@"--mode", @"global", @"--port", [NSString stringWithFormat:@"%lu", (unsigned long)port] ]];
}
+ (void)disableProxy {
[self callHelper:@[@"--mode", @"off"]];
}
@end

View File

@ -43,12 +43,15 @@ class ServerProfileManager: NSObject {
}
defaults.setObject(_profiles, forKey: "ServerProfiles")
if getActiveProfile() == nil {
activeProfileId = nil
}
// TODO
if activeProfileId != nil {
defaults.setObject(activeProfileId, forKey: "ActiveServerProfileId")
writeSSLocalConfFile((getActiveProfile()?.toJsonConfig())!)
} else {
defaults.removeObjectForKey("ActiveServerProfileId")
removeSSLocalConfFile()
}
}

View File

@ -0,0 +1,10 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <CommonCrypto/CommonCrypto.h>
#import "LaunchAtLoginController.h"
#import "SWBQRCodeWindowController.h"
#import "Utils.h"
#import "ProxyConfHelper.h"

View File

@ -1,15 +0,0 @@
//
// Sysconf.m
// ShadowsocksX-NG
//
// Created by on 16/6/9.
// Copyright © 2016 qiuyuzhou. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
void ConfProxy() {
}

View File

@ -0,0 +1,14 @@
#!/bin/sh
# install_helper.sh
# shadowsocks
#
# Created by clowwindy on 14-3-15.
cd `dirname "${BASH_SOURCE[0]}"`
sudo mkdir -p "/Library/Application Support/ShadowsocksX-NG/"
sudo cp proxy_conf_helper "/Library/Application Support/ShadowsocksX-NG/"
sudo chown root:admin "/Library/Application Support/ShadowsocksX-NG/proxy_conf_helper"
sudo chmod +s "/Library/Application Support/ShadowsocksX-NG/proxy_conf_helper"
echo done

View File

@ -0,0 +1,14 @@
//
// proxy_conf_helper_version.h
// ShadowsocksX-NG
//
// Created by 邱宇舟 on 16/6/10.
// Copyright © 2016年 qiuyuzhou. All rights reserved.
//
#ifndef proxy_conf_helper_version_h
#define proxy_conf_helper_version_h
#define kProxyConfHelperVersion @"1.0.0"
#endif /* proxy_conf_helper_version_h */

View File

@ -8,4 +8,5 @@
#launchctl kill SIGHUP "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NE.local.plist"
launchctl kickstart -k "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NE.local.plist"
launchctl unload "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NE.local.plist"
launchctl load "$HOME/Library/LaunchAgents/com.qiuyuzhou.shadowsocksX-NE.local.plist"