Merge branch 'pr/198' into develop
This commit is contained in:
@ -59,6 +59,7 @@
|
||||
C6E28E951DA79705004F8330 /* HTTPPreferencesWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C6E28E971DA79705004F8330 /* HTTPPreferencesWindowController.xib */; };
|
||||
C8E42A6C1D4F270A0074C7EA /* UserRulesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */; };
|
||||
C8E42A6E1D4F2CAF0074C7EA /* UserRulesController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8E42A701D4F2CAF0074C7EA /* UserRulesController.xib */; };
|
||||
D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E3630A1E2072980027449B /* ServerProfileTests.swift */; };
|
||||
E0E57CCA7EB34B90F9D340F2 /* Pods_ShadowsocksX_NGTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 388120F062D7EB7DD0D8DDCA /* Pods_ShadowsocksX_NGTests.framework */; };
|
||||
F0809FF1595BE2966343D3C7 /* libPods-proxy_conf_helper.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E7783AEDB4A3BDDC9FF16AC /* libPods-proxy_conf_helper.a */; };
|
||||
/* End PBXBuildFile section */
|
||||
@ -176,6 +177,7 @@
|
||||
C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserRulesController.swift; sourceTree = "<group>"; };
|
||||
C8E42A6F1D4F2CAF0074C7EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserRulesController.xib; sourceTree = "<group>"; };
|
||||
C8E42A721D4F2CB10074C7EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/UserRulesController.strings"; sourceTree = "<group>"; };
|
||||
D8E3630A1E2072980027449B /* ServerProfileTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerProfileTests.swift; sourceTree = "<group>"; };
|
||||
E9E9FB3855DA55D0710EE7BD /* Pods-ShadowsocksX-NG.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.release.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.release.xcconfig"; sourceTree = "<group>"; };
|
||||
FE3237E9FB24D9B924A0E630 /* Pods-ShadowsocksX-NG.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.debug.xcconfig"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
@ -309,6 +311,7 @@
|
||||
9B0BFFF71D0460A70040E62B /* ShadowsocksX-NGTests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D8E3630A1E2072980027449B /* ServerProfileTests.swift */,
|
||||
9B0BFFF81D0460A70040E62B /* ShadowsocksX_NGTests.swift */,
|
||||
9B0BFFFA1D0460A70040E62B /* Info.plist */,
|
||||
);
|
||||
@ -666,6 +669,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
9B0BFFF91D0460A70040E62B /* ShadowsocksX_NGTests.swift in Sources */,
|
||||
D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@ -905,6 +909,7 @@
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.qiuyuzhou.ShadowsocksX-NGTests";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "ShadowsocksX-NG/ShadowsocksX-NG-Bridging-Header.h";
|
||||
SWIFT_VERSION = 3.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ShadowsocksX-NG.app/Contents/MacOS/ShadowsocksX-NG";
|
||||
};
|
||||
@ -920,6 +925,7 @@
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.qiuyuzhou.ShadowsocksX-NGTests";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "ShadowsocksX-NG/ShadowsocksX-NG-Bridging-Header.h";
|
||||
SWIFT_VERSION = 3.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ShadowsocksX-NG.app/Contents/MacOS/ShadowsocksX-NG";
|
||||
};
|
||||
|
@ -121,9 +121,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
||||
var isChanged = false
|
||||
|
||||
for url in urls {
|
||||
let profielDict = ParseSSURL(url)
|
||||
if let profielDict = profielDict {
|
||||
let profile = ServerProfile.fromDictionary(profielDict as [String : AnyObject])
|
||||
if let profile = ServerProfile(url: url) {
|
||||
mgr.profiles.append(profile)
|
||||
isChanged = true
|
||||
|
||||
|
@ -8,8 +8,6 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
|
||||
|
||||
class ServerProfile: NSObject {
|
||||
var uuid: String
|
||||
|
||||
@ -28,7 +26,57 @@ class ServerProfile: NSObject {
|
||||
self.uuid = uuid
|
||||
}
|
||||
|
||||
static func fromDictionary(_ data:[String:AnyObject]) -> ServerProfile {
|
||||
convenience init?(url: URL?) {
|
||||
self.init()
|
||||
|
||||
func padBase64(string: String) -> String {
|
||||
var length = string.characters.count
|
||||
if length % 4 == 0 {
|
||||
return string
|
||||
} else {
|
||||
length = 4 - length % 4 + length
|
||||
return string.padding(toLength: length, withPad: "=", startingAt: 0)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeUrl(url: URL?) -> String? {
|
||||
guard let encodedStr = url?.host else {
|
||||
return nil
|
||||
}
|
||||
guard let data = Data(base64Encoded: padBase64(string: encodedStr)) else {
|
||||
return url?.absoluteString
|
||||
}
|
||||
guard let decoded = String(data: data, encoding: String.Encoding.utf8) else {
|
||||
return nil
|
||||
}
|
||||
return "ss://\(decoded)"
|
||||
}
|
||||
|
||||
guard let decodedUrl = decodeUrl(url: url) else {
|
||||
return nil
|
||||
}
|
||||
guard var parsedUrl = URLComponents(string: decodedUrl) else {
|
||||
return nil
|
||||
}
|
||||
guard let host = parsedUrl.host, let port = parsedUrl.port,
|
||||
let method = parsedUrl.user, let password = parsedUrl.password else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.serverHost = host
|
||||
self.serverPort = UInt16(port)
|
||||
self.method = method
|
||||
self.password = password
|
||||
|
||||
remark = parsedUrl.queryItems?
|
||||
.filter({ $0.name == "Remark" }).first?.value ?? ""
|
||||
if let otaStr = parsedUrl.queryItems?
|
||||
.filter({ $0.name == "OTA" }).first?.value {
|
||||
ota = NSString(string: otaStr).boolValue
|
||||
}
|
||||
}
|
||||
|
||||
static func fromDictionary(_ data:[String: Any?]) -> ServerProfile {
|
||||
let cp = {
|
||||
(profile: ServerProfile) in
|
||||
profile.serverHost = data["ServerHost"] as! String
|
||||
@ -121,9 +169,22 @@ class ServerProfile: NSObject {
|
||||
}
|
||||
|
||||
func URL() -> Foundation.URL? {
|
||||
let parts = "\(method):\(password)@\(serverHost):\(serverPort)"
|
||||
let base64String = parts.data(using: String.Encoding.utf8)?
|
||||
.base64EncodedString(options: NSData.Base64EncodingOptions())
|
||||
var url = URLComponents()
|
||||
|
||||
url.host = serverHost
|
||||
url.user = method
|
||||
url.password = password
|
||||
url.port = Int(serverPort)
|
||||
|
||||
url.queryItems = [URLQueryItem(name: "Remark", value: remark),
|
||||
URLQueryItem(name: "OTA", value: ota.description)]
|
||||
|
||||
let parts = url.string?.replacingOccurrences(
|
||||
of: "//", with: "",
|
||||
options: String.CompareOptions.anchored, range: nil)
|
||||
|
||||
let base64String = parts?.data(using: String.Encoding.utf8)?
|
||||
.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
if var s = base64String {
|
||||
s = s.trimmingCharacters(in: CharacterSet(charactersIn: "="))
|
||||
return Foundation.URL(string: "ss://\(s)")
|
||||
|
@ -21,7 +21,7 @@ class ServerProfileManager: NSObject {
|
||||
let defaults = UserDefaults.standard
|
||||
if let _profiles = defaults.array(forKey: "ServerProfiles") {
|
||||
for _profile in _profiles {
|
||||
let profile = ServerProfile.fromDictionary(_profile as! [String : AnyObject])
|
||||
let profile = ServerProfile.fromDictionary(_profile as! [String: Any])
|
||||
profiles.append(profile)
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,4 @@
|
||||
|
||||
void ScanQRCodeOnScreen();
|
||||
|
||||
NSDictionary<NSString *, id>* ParseSSURL(NSURL* url);
|
||||
|
||||
#endif /* QRCodeUtils_h */
|
||||
|
@ -71,63 +71,3 @@ void ScanQRCodeOnScreen() {
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
// 解析SS URL,如果成功则返回一个与ServerProfile类兼容的dict
|
||||
NSDictionary<NSString *, id>* ParseSSURL(NSURL* url) {
|
||||
if (!url.host) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *urlString = [url absoluteString];
|
||||
int i = 0;
|
||||
NSString *errorReason = nil;
|
||||
while(i < 2) {
|
||||
if (i == 1) {
|
||||
NSString* host = url.host;
|
||||
if ([host length]%4!=0) {
|
||||
int n = 4 - [host length]%4;
|
||||
if (1==n) {
|
||||
host = [host stringByAppendingString:@"="];
|
||||
} else if (2==n) {
|
||||
host = [host stringByAppendingString:@"=="];
|
||||
}
|
||||
}
|
||||
NSData *data = [[NSData alloc] initWithBase64EncodedString:host options:0];
|
||||
NSString *decodedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
urlString = decodedString;
|
||||
}
|
||||
i++;
|
||||
urlString = [urlString stringByReplacingOccurrencesOfString:@"ss://" withString:@"" options:NSAnchoredSearch range:NSMakeRange(0, urlString.length)];
|
||||
NSRange firstColonRange = [urlString rangeOfString:@":"];
|
||||
NSRange lastColonRange = [urlString rangeOfString:@":" options:NSBackwardsSearch];
|
||||
NSRange lastAtRange = [urlString rangeOfString:@"@" options:NSBackwardsSearch];
|
||||
if (firstColonRange.length == 0) {
|
||||
errorReason = @"colon not found";
|
||||
continue;
|
||||
}
|
||||
if (firstColonRange.location == lastColonRange.location) {
|
||||
errorReason = @"only one colon";
|
||||
continue;
|
||||
}
|
||||
if (lastAtRange.length == 0) {
|
||||
errorReason = @"at not found";
|
||||
continue;
|
||||
}
|
||||
if (!((firstColonRange.location < lastAtRange.location) && (lastAtRange.location < lastColonRange.location))) {
|
||||
errorReason = @"wrong position";
|
||||
continue;
|
||||
}
|
||||
NSString *method = [urlString substringWithRange:NSMakeRange(0, firstColonRange.location)];
|
||||
NSString *password = [urlString substringWithRange:NSMakeRange(firstColonRange.location + 1, lastAtRange.location - firstColonRange.location - 1)];
|
||||
NSString *IP = [urlString substringWithRange:NSMakeRange(lastAtRange.location + 1, lastColonRange.location - lastAtRange.location - 1)];
|
||||
NSString *port = [urlString substringWithRange:NSMakeRange(lastColonRange.location + 1, urlString.length - lastColonRange.location - 1)];
|
||||
|
||||
|
||||
return @{@"ServerHost": IP,
|
||||
@"ServerPort": @([port integerValue]),
|
||||
@"Method": method,
|
||||
@"Password": password,
|
||||
};
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
@ -8,14 +8,12 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
extension String {
|
||||
var localized: String {
|
||||
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Data {
|
||||
func sha1() -> String {
|
||||
let data = self
|
||||
|
153
ShadowsocksX-NGTests/ServerProfileTests.swift
Normal file
153
ShadowsocksX-NGTests/ServerProfileTests.swift
Normal file
@ -0,0 +1,153 @@
|
||||
//
|
||||
// ServerProfileTests.swift
|
||||
// ShadowsocksX-NG
|
||||
//
|
||||
// Created by Rainux Luo on 07/01/2017.
|
||||
// Copyright © 2017 qiuyuzhou. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import ShadowsocksX_NG
|
||||
|
||||
class ServerProfileTests: XCTestCase {
|
||||
|
||||
var profile: ServerProfile!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
profile = ServerProfile.fromDictionary(["ServerHost": "example.com",
|
||||
"ServerPort": 8388,
|
||||
"Method": "aes-256-cfb",
|
||||
"Password": "password",
|
||||
"Remark": "Protoss Prism",
|
||||
"OTA": true])
|
||||
XCTAssertNotNil(profile)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testInitWithSelfGeneratedURL() {
|
||||
let newProfile = ServerProfile.init(url: profile.URL())
|
||||
|
||||
XCTAssertEqual(newProfile?.serverHost, profile.serverHost)
|
||||
XCTAssertEqual(newProfile?.serverPort, profile.serverPort)
|
||||
XCTAssertEqual(newProfile?.method, profile.method)
|
||||
XCTAssertEqual(newProfile?.password, profile.password)
|
||||
XCTAssertEqual(newProfile?.remark, profile.remark)
|
||||
XCTAssertEqual(newProfile?.ota, profile.ota)
|
||||
}
|
||||
|
||||
func testInitWithPlainURL() {
|
||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNotNil(profile)
|
||||
|
||||
XCTAssertEqual(profile?.serverHost, "example.com")
|
||||
XCTAssertEqual(profile?.serverPort, 8388)
|
||||
XCTAssertEqual(profile?.method, "aes-256-cfb")
|
||||
XCTAssertEqual(profile?.password, "password")
|
||||
XCTAssertEqual(profile?.remark, "")
|
||||
XCTAssertEqual(profile?.ota, false)
|
||||
}
|
||||
|
||||
func testInitWithPlainURLandQuery() {
|
||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNotNil(profile)
|
||||
|
||||
XCTAssertEqual(profile?.serverHost, "example.com")
|
||||
XCTAssertEqual(profile?.serverPort, 8388)
|
||||
XCTAssertEqual(profile?.method, "aes-256-cfb")
|
||||
XCTAssertEqual(profile?.password, "password")
|
||||
XCTAssertEqual(profile?.remark, "Prism")
|
||||
XCTAssertEqual(profile?.ota, true)
|
||||
}
|
||||
|
||||
func testInitWithPlainURLandAnotherQuery() {
|
||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNotNil(profile)
|
||||
|
||||
XCTAssertEqual(profile?.serverHost, "example.com")
|
||||
XCTAssertEqual(profile?.serverPort, 8388)
|
||||
XCTAssertEqual(profile?.method, "aes-256-cfb")
|
||||
XCTAssertEqual(profile?.password, "password")
|
||||
XCTAssertEqual(profile?.remark, "Prism")
|
||||
XCTAssertEqual(profile?.ota, false)
|
||||
}
|
||||
|
||||
func testInitWithBase64EncodedURL() {
|
||||
// "ss://aes-256-cfb:password@example.com:8388"
|
||||
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNotNil(profile)
|
||||
|
||||
XCTAssertEqual(profile?.serverHost, "example.com")
|
||||
XCTAssertEqual(profile?.serverPort, 8388)
|
||||
XCTAssertEqual(profile?.method, "aes-256-cfb")
|
||||
XCTAssertEqual(profile?.password, "password")
|
||||
XCTAssertEqual(profile?.remark, "")
|
||||
XCTAssertEqual(profile?.ota, false)
|
||||
}
|
||||
|
||||
func testInitWithBase64EncodedURLandQuery() {
|
||||
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
|
||||
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNotNil(profile)
|
||||
|
||||
XCTAssertEqual(profile?.serverHost, "example.com")
|
||||
XCTAssertEqual(profile?.serverPort, 8388)
|
||||
XCTAssertEqual(profile?.method, "aes-256-cfb")
|
||||
XCTAssertEqual(profile?.password, "password")
|
||||
XCTAssertEqual(profile?.remark, "Prism")
|
||||
XCTAssertEqual(profile?.ota, true)
|
||||
}
|
||||
|
||||
func testInitWithEmptyURL() {
|
||||
let url = URL(string: "ss://")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNil(profile)
|
||||
}
|
||||
|
||||
func testInitWithInvalidURL() {
|
||||
let url = URL(string: "ss://invalid url")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNil(profile)
|
||||
}
|
||||
|
||||
func testInitWithBase64EncodedInvalidURL() {
|
||||
// "ss://invalid url"
|
||||
let url = URL(string: "ss://aW52YWxpZCB1cmw")
|
||||
|
||||
let profile = ServerProfile(url: url)
|
||||
|
||||
XCTAssertNil(profile)
|
||||
}
|
||||
|
||||
func testPerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
self.measure {
|
||||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user