新增加白名单模式

新增白名单模式,白名单来自破娃酱
注意事项,替换白名单中proxy的地址时发现原来的IP白名单中 wall_proxy 只有 socks5,而域名白名单中 wall_proxy
有 socks 和 socks5(只有这样才会work)。故此在后面的处理替换中将两个都加上了。后续变化,还需要看破娃酱 white list
具体变化。
This commit is contained in:
qinyuhang
2016-08-14 00:40:14 +08:00
parent be51992ba8
commit eae605910c
12 changed files with 10043 additions and 24 deletions
+50 -4
View File
@@ -30,6 +30,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
@IBOutlet weak var globalModeMenuItem: NSMenuItem!
@IBOutlet weak var manualModeMenuItem: NSMenuItem!
@IBOutlet weak var whiteListModeMenuItem: NSMenuItem!
@IBOutlet weak var whiteListDomainMenuItem: NSMenuItem!
@IBOutlet weak var whiteListIPMenuItem: NSMenuItem!
@IBOutlet weak var serversMenuItem: NSMenuItem!
@IBOutlet var showQRCodeMenuItem: NSMenuItem!
@@ -59,6 +61,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
"LocalSocks5.EnableUDPRelay": NSNumber(bool: false),
"LocalSocks5.EnableVerboseMode": NSNumber(bool: false),
"GFWListURL": "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt",
"WhiteListURL": "https://raw.githubusercontent.com/breakwa11/gfw_whitelist/master/whitelist.pac",
"WhiteListIPURL": "https://raw.githubusercontent.com/breakwa11/gfw_whitelist/master/whiteiplist.pac",
"AutoConfigureNetworkServices": NSNumber(bool: true)
])
@@ -145,6 +149,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
StopSSLocal()
ProxyConfHelper.disableProxy()
}
func applyConfig() {
@@ -155,13 +161,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
if isOn {
StartSSLocal()
if mode == "auto" {
ProxyConfHelper.disableProxy()
ProxyConfHelper.enablePACProxy()
} else if mode == "global" {
ProxyConfHelper.disableProxy()
ProxyConfHelper.enableGlobalProxy()
} else if mode == "manual" {
ProxyConfHelper.disableProxy()
} else if mode == "whiteList"{
//ProxyConfHelper.enableWhiteListProxy()
ProxyConfHelper.disableProxy()
} else if mode == "whiteListDomain" {
ProxyConfHelper.disableProxy()
ProxyConfHelper.enableWhiteDomainListProxy()
} else if mode == "whiteListIP" {
ProxyConfHelper.disableProxy()
ProxyConfHelper.enableWhiteIPListProxy()
}
} else {
StopSSLocal()
@@ -185,6 +198,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
@IBAction func updateWhiteList(sender: NSMenuItem) {
UpdatePACFromWhiteList()
}
@IBAction func editUserRulesForPAC(sender: NSMenuItem) {
@@ -258,9 +272,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
applyConfig()
}
@IBAction func selectWhiteListMode(sender: NSMenuItem) {
@IBAction func selectWhiteDomainListMode(sender: NSMenuItem) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("whiteList", forKey: "ShadowsocksRunningMode")
defaults.setValue("whiteListDomain", forKey: "ShadowsocksRunningMode")
updateRunningModeMenu()
applyConfig()
}
@IBAction func selectWhiteIPListMode(sender: NSMenuItem) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("whiteListIP", forKey: "ShadowsocksRunningMode")
updateRunningModeMenu()
applyConfig()
}
@@ -344,16 +365,41 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
autoModeMenuItem.state = 1
globalModeMenuItem.state = 0
manualModeMenuItem.state = 0
whiteListModeMenuItem.state = 0
whiteListDomainMenuItem.state = 0
whiteListIPMenuItem.state = 0
} else if mode == "global" {
proxyMenuItem.title = "Proxy - Global".localized
autoModeMenuItem.state = 0
globalModeMenuItem.state = 1
manualModeMenuItem.state = 0
whiteListModeMenuItem.state = 0
whiteListDomainMenuItem.state = 0
whiteListIPMenuItem.state = 0
} else if mode == "manual" {
proxyMenuItem.title = "Proxy - Manual".localized
autoModeMenuItem.state = 0
globalModeMenuItem.state = 0
manualModeMenuItem.state = 1
whiteListModeMenuItem.state = 0
whiteListDomainMenuItem.state = 0
whiteListIPMenuItem.state = 0
} else if mode == "whiteListDomain" {
proxyMenuItem.title = "Proxy - WhiteList Domain".localized
autoModeMenuItem.state = 0
globalModeMenuItem.state = 0
manualModeMenuItem.state = 0
whiteListModeMenuItem.state = 1
whiteListDomainMenuItem.state = 1
whiteListIPMenuItem.state = 0
} else if mode == "whiteListIP" {
proxyMenuItem.title = "Proxy - WhiteList IP".localized
autoModeMenuItem.state = 0
globalModeMenuItem.state = 0
manualModeMenuItem.state = 0
whiteListModeMenuItem.state = 1
whiteListDomainMenuItem.state = 0
whiteListIPMenuItem.state = 1
}
}
@@ -47,3 +47,13 @@
"Proxy - Manual" = "Proxy - Manual";
"Proxy - WhiteList" = "Proxy - WhiteList";
"Proxy - WhiteList Domain" = "Proxy - WhiteList Domain";
"Proxy - WhiteList IP" = "Proxy - WhiteList IP";
"White List Mode" = "White List Mode";
"White List Domain" = "White List Domain";
"White List IP" = "White List IP";
+21 -6
View File
@@ -26,7 +26,9 @@
<outlet property="showQRCodeMenuItem" destination="R6A-96-Zcb" id="XHz-pz-nCz"/>
<outlet property="statusMenu" destination="Hob-KD-bx9" id="clA-ZW-0pT"/>
<outlet property="toggleRunningMenuItem" destination="GSu-Tf-StS" id="XHw-pU-nCa"/>
<outlet property="whiteListModeMenuItem" destination="wBP-24-Xo8" id="jpq-gv-HHX"/>
<outlet property="whiteListDomainMenuItem" destination="L27-aH-Wwl" id="h4b-aF-lyt"/>
<outlet property="whiteListIPMenuItem" destination="Xbu-Kq-BSR" id="D9i-Xg-6Gc"/>
<outlet property="whiteListModeMenuItem" destination="RvZ-Zn-29U" id="2v5-Bd-EDj"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
@@ -64,11 +66,24 @@
<action selector="selectManualMode:" target="Voe-Tx-rLC" id="Xxb-28-6fi"/>
</connections>
</menuItem>
<menuItem title="Wihte List Mode" id="wBP-24-Xo8">
<menuItem title="White List Mode" id="RvZ-Zn-29U">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="selectWhiteListMode:" target="Voe-Tx-rLC" id="KnS-FW-UMx"/>
</connections>
<menu key="submenu" title="White List Mode" id="nAe-c2-Q79">
<items>
<menuItem title="White List Domain" id="L27-aH-Wwl">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="selectWhiteDomainListMode:" target="Voe-Tx-rLC" id="Oge-mM-TtL"/>
</connections>
</menuItem>
<menuItem title="White List IP" id="Xbu-Kq-BSR">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="selectWhiteIPListMode:" target="Voe-Tx-rLC" id="Ahq-h9-jJP"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="V92-VH-Agn"/>
<menuItem title="Update PAC from GFW List" id="TFc-Ec-duM">
@@ -77,7 +92,7 @@
<action selector="updateGFWList:" target="Voe-Tx-rLC" id="Ztt-PS-F3T"/>
</connections>
</menuItem>
<menuItem title="Update Wihte List" id="gde-ZE-086">
<menuItem title="Update All Wihte List" id="gde-ZE-086">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="updateWhiteList:" target="Voe-Tx-rLC" id="2Iv-M4-k9C"/>
+1 -1
View File
@@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2.R</string>
<string>1.3.2-R</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
+104
View File
@@ -15,6 +15,8 @@ let PACRulesDirPath = NSHomeDirectory() + "/.ShadowsocksX-NG/"
let PACUserRuleFilePath = PACRulesDirPath + "user-rule.txt"
let PACFilePath = PACRulesDirPath + "gfwlist.js"
let GFWListFilePath = PACRulesDirPath + "gfwlist.txt"
let WhiteListDomainPACFilePath = PACRulesDirPath + "whitelist.pac"
let WhiteListIPPACFilePath = PACRulesDirPath + "whiteiplist.pac"
// Because of LocalSocks5.ListenPort may be changed
@@ -33,6 +35,10 @@ func SyncPac() {
needGenerate = true
}
if !fileMgr.fileExistsAtPath(WhiteListDomainPACFilePath) && !fileMgr.fileExistsAtPath(WhiteListIPPACFilePath) {
needGenerate = true
}
if needGenerate {
GeneratePACFile()
}
@@ -63,6 +69,18 @@ func GeneratePACFile() -> Bool {
try! fileMgr.copyItemAtPath(src!, toPath: PACUserRuleFilePath)
}
// If whitelist.pac is not exsited, copy from bundle
if !fileMgr.fileExistsAtPath(WhiteListDomainPACFilePath) {
let src = NSBundle.mainBundle().pathForResource("whitelist", ofType: "pac")
try! fileMgr.copyItemAtPath(src!, toPath: WhiteListDomainPACFilePath)
}
// If whitelistip.pac is not exsited, copy from bundle
if !fileMgr.fileExistsAtPath(WhiteListIPPACFilePath) {
let src = NSBundle.mainBundle().pathForResource("whiteiplist", ofType: "pac")
try! fileMgr.copyItemAtPath(src!, toPath: WhiteListIPPACFilePath)
}
let socks5Port = NSUserDefaults.standardUserDefaults().integerForKey("LocalSocks5.ListenPort")
do {
@@ -114,6 +132,26 @@ func GeneratePACFile() -> Bool {
try result.dataUsingEncoding(NSUTF8StringEncoding)?
.writeToFile(PACFilePath, options: .DataWritingAtomic)
// Setup Pac for White List
let DomainSrc = NSBundle.mainBundle().pathForResource("whitelist", ofType: "pac")
let IPSrc = NSBundle.mainBundle().pathForResource("whiteiplist", ofType: "pac")
let DomainPacFile = NSData(contentsOfFile: DomainSrc!)
let IPPACFile = NSData(contentsOfFile: IPSrc!)
var DomainPACStr = String(data: DomainPacFile!,encoding: NSUTF8StringEncoding)!
var IPPACStr = String(data: IPPACFile!,encoding: NSUTF8StringEncoding)!
if(DomainPACStr.rangeOfString("SOCKS ") == nil) {
DomainPACStr = DomainPACStr.stringByReplacingOccurrencesOfString("SOCKS5 127.0.0.1:1080;", withString: "SOCKS5 127.0.0.1:\(socks5Port);SOCKS 127.0.0.1:\(socks5Port);")
}
DomainPACStr = DomainPACStr.stringByReplacingOccurrencesOfString("SOCKS 127.0.0.1:1080;", withString: "SOCKS 127.0.0.1:\(socks5Port);")
if(IPPACStr.rangeOfString("SOCKS ") == nil) {
IPPACStr = IPPACStr.stringByReplacingOccurrencesOfString("SOCKS5 127.0.0.1:1080;", withString: "SOCKS5 127.0.0.1:\(socks5Port);SOCKS 127.0.0.1:\(socks5Port);")
}
IPPACStr = IPPACStr.stringByReplacingOccurrencesOfString("SOCKS 127.0.0.1:1080;", withString: "SOCKS 127.0.0.1:\(socks5Port);")
try
DomainPACStr.dataUsingEncoding(NSUTF8StringEncoding)?.writeToFile(WhiteListDomainPACFilePath, options: .DataWritingAtomic)
try
IPPACStr.dataUsingEncoding(NSUTF8StringEncoding)?.writeToFile(WhiteListIPPACFilePath, options: .DataWritingAtomic)
return true
} catch {
@@ -164,3 +202,69 @@ func UpdatePACFromGFWList() {
}
}
}
func UpdatePACFromWhiteList(){
if !NSFileManager.defaultManager().fileExistsAtPath(PACRulesDirPath) {
do {
try NSFileManager.defaultManager().createDirectoryAtPath(PACRulesDirPath
, withIntermediateDirectories: true, attributes: nil)
} catch {
}
}
let url = NSUserDefaults.standardUserDefaults().stringForKey("WhiteListURL")
Alamofire.request(.GET, url!)
.responseString {
response in
if response.result.isSuccess {
if let v = response.result.value {
do {
try v.writeToFile(WhiteListDomainPACFilePath, atomically: true, encoding: NSUTF8StringEncoding)
if GeneratePACFile() {
// Popup a user notification
let notification = NSUserNotification()
notification.title = "Wihte List update succeed.".localized
NSUserNotificationCenter.defaultUserNotificationCenter()
.deliverNotification(notification)
}
} catch {
}
}
} else {
// Popup a user notification
let notification = NSUserNotification()
notification.title = "Failed to download latest Wihte List update succeed.".localized
NSUserNotificationCenter.defaultUserNotificationCenter()
.deliverNotification(notification)
}
}
let IPURL = NSUserDefaults.standardUserDefaults().stringForKey("WhiteListIPURL")
Alamofire.request(.GET, IPURL!)
.responseString {
response in
if response.result.isSuccess {
if let v = response.result.value {
do {
try v.writeToFile(WhiteListIPPACFilePath, atomically: true, encoding: NSUTF8StringEncoding)
if GeneratePACFile() {
// Popup a user notification
let notification = NSUserNotification()
notification.title = "Wihte List update succeed.".localized
NSUserNotificationCenter.defaultUserNotificationCenter()
.deliverNotification(notification)
}
} catch {
}
}
} else {
// Popup a user notification
let notification = NSUserNotification()
notification.title = "Failed to download latest Wihte List update succeed.".localized
NSUserNotificationCenter.defaultUserNotificationCenter()
.deliverNotification(notification)
}
}
}
+3 -1
View File
@@ -18,6 +18,8 @@
+ (void)disableProxy;
+ (void)enableWhiteListProxy;
+ (void)enableWhiteDomainListProxy;
+ (void)enableWhiteIPListProxy;
@end
+29 -12
View File
@@ -135,22 +135,39 @@
[self callHelper:args];
}
+ (void)enableWhiteDomainListProxy {
NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NG/whitelist.pac", NSHomeDirectory()];
NSURL* url = [NSURL fileURLWithPath:urlString];
NSMutableArray* args = [@[@"--mode", @"auto", @"--pac-url", [url absoluteString]]mutableCopy];
[self addArguments4ManualSpecifyNetworkServices:args];
[self callHelper:args];
}
+ (void)enableWhiteIPListProxy {
NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NG/whiteiplist.pac", NSHomeDirectory()];
NSURL* url = [NSURL fileURLWithPath:urlString];
NSMutableArray* args = [@[@"--mode", @"auto", @"--pac-url", [url absoluteString]]mutableCopy];
[self addArguments4ManualSpecifyNetworkServices:args];
[self callHelper:args];
}
+ (void)disableProxy {
// 带上所有参数是为了判断是否原有代理设置是否由ssx-ng设置的。如果是用户手工设置的其他配置,则不进行清空。
NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NG/gfwlist.js", NSHomeDirectory()];
NSURL* url = [NSURL fileURLWithPath:urlString];
NSUInteger port = [[NSUserDefaults standardUserDefaults]integerForKey:@"LocalSocks5.ListenPort"];
NSMutableArray* args = [@[@"--mode", @"off"
, @"--port", [NSString stringWithFormat:@"%lu", (unsigned long)port]
, @"--pac-url", [url absoluteString]
]mutableCopy];
// NSString* urlString = [NSString stringWithFormat:@"%@/.ShadowsocksX-NG/gfwlist.js", NSHomeDirectory()];
// NSURL* url = [NSURL fileURLWithPath:urlString];
// NSUInteger port = [[NSUserDefaults standardUserDefaults]integerForKey:@"LocalSocks5.ListenPort"];
//
// NSMutableArray* args = [@[@"--mode", @"off"
// , @"--port", [NSString stringWithFormat:@"%lu", (unsigned long)port]
// , @"--pac-url", [url absoluteString]
// ]mutableCopy];
NSMutableArray* args = [@[@"--mode", @"off"]mutableCopy];
[self addArguments4ManualSpecifyNetworkServices:args];
[self callHelper:args];
}
+ (void)enableWhiteListProxy {
}
@end
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
@@ -52,3 +52,12 @@
"Proxy - WhiteList" = "代理 - 白名单";
"Proxy - WhiteList Domain" = "代理 - 域名白名单";
"Proxy - WhiteList IP" = "代理 - IP白名单";
"White List Mode" = "白名单模式";
"White List Domain" = "域名白名单";
"White List IP" = "IP白名单";
@@ -67,3 +67,18 @@
/* Class = "NSMenuItem"; title = "退出"; ObjectID = "vJS-JW-byz"; */
"vJS-JW-byz.title" = "退出";
/* Class = "NSMenuItem"; title = "域名白名单"; ObjectID = "L27-aH-Wwl"; */
"L27-aH-Wwl.title" = "域名白名单";
/* Class = "NSMenuItem"; title = "IP白名单"; ObjectID = "Xbu-Kq-BSR"; */
"Xbu-Kq-BSR.title" = "IP白名单";
/* Class = "NSMenuItem"; title = "白名单模式"; ObjectID = "nAe-c2-Q79"; */
"nAe-c2-Q79.title" = "白名单模式";
/* Class = "NSMenuItem"; title = "白名单模式"; ObjectID = "RvZ-Zn-29U"; */
"RvZ-Zn-29U.title" = "白名单模式";
/* Class = "NSMenuItem"; title = "更新全部白名单"; ObjectID = "gde-ZE-086"; */
"gde-ZE-086.title" = "更新全部白名单";