Refactor ParseSSURL as ServerProfile initializer

This commit is contained in:
Rainux Luo
2017-01-09 21:49:09 +08:00
parent c50e18d832
commit dbc6ee92d5
6 changed files with 91 additions and 84 deletions

View File

@ -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)
if let profile = ServerProfile(url: url) {
mgr.profiles.append(profile)
isChanged = true

View File

@ -8,8 +8,6 @@
import Cocoa
class ServerProfile: NSObject {
var uuid: String
@ -28,6 +26,56 @@ class ServerProfile: NSObject {
self.uuid = uuid
}
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

View File

@ -23,53 +23,3 @@ extension Data {
return hexBytes.joined(separator: "")
}
}
func ParseSSURL(_ url: URL?) -> [String: Any?]? {
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)
}
}
if url?.host == nil {
return nil
}
var plainUrl: URLComponents! = URLComponents(url: url!,
resolvingAgainstBaseURL: false)
let data = Data(base64Encoded: padBase64(string: url!.host!),
options: Data.Base64DecodingOptions())
if data != nil {
let decoded = String(data: data!, encoding: String.Encoding.utf8)
plainUrl = URLComponents(string: "ss://\(decoded!)")
if plainUrl == nil {
return nil
}
}
let remark = plainUrl.queryItems?
.filter({ $0.name == "Remark" }).first?.value
let otaStr = plainUrl.queryItems?
.filter({ $0.name == "OTA" }).first?.value
var ota: Bool? = nil
if otaStr != nil {
ota = NSString(string: otaStr!).boolValue
}
return ["ServerHost": plainUrl.host,
"ServerPort": UInt16(plainUrl.port!),
"Method": plainUrl.user,
"Password": plainUrl.password,
"Remark": remark,
"OTA": ota,
]
}

View File

@ -37,7 +37,7 @@ class ServerProfileTests: XCTestCase {
}
func testServerProfileURL() {
let parsed = ParseSSURL(profile.URL())
let parsed = ServerProfile(url: profile.URL())?.toDictionary()
XCTAssertNotNil(parsed)
@ -49,6 +49,17 @@ class ServerProfileTests: XCTestCase {
XCTAssertEqual(parsed?["OTA"] as? Bool, profile.ota)
}
func testServerProfileInitFromURL() {
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 testPerformanceExample() {
// This is an example of a performance test case.
self.measure {

View File

@ -24,7 +24,7 @@ class UtilsTests: XCTestCase {
func testParseSSURLwithPlainURL() {
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)?.toDictionary()
XCTAssertNotNil(profile)
@ -37,7 +37,7 @@ class UtilsTests: XCTestCase {
func testParseSSURLwithPlainURLandQuery() {
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)?.toDictionary()
XCTAssertNotNil(profile)
@ -52,7 +52,7 @@ class UtilsTests: XCTestCase {
func testParseSSURLwithPlainURLandAnotherQuery() {
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)?.toDictionary()
XCTAssertNotNil(profile)
@ -68,7 +68,7 @@ class UtilsTests: XCTestCase {
// "ss://aes-256-cfb:password@example.com:8388"
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)?.toDictionary()
XCTAssertNotNil(profile)
@ -82,7 +82,7 @@ class UtilsTests: XCTestCase {
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)?.toDictionary()
XCTAssertNotNil(profile)
@ -97,7 +97,7 @@ class UtilsTests: XCTestCase {
func testParseSSURLwithEmptyURL() {
let url = URL(string: "ss://")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)
XCTAssertNil(profile)
}
@ -105,7 +105,7 @@ class UtilsTests: XCTestCase {
func testParseSSURLwithInvalidURL() {
let url = URL(string: "ss://invalid url")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)
XCTAssertNil(profile)
}
@ -114,7 +114,7 @@ class UtilsTests: XCTestCase {
// "ss://invalid url"
let url = URL(string: "ss://aW52YWxpZCB1cmw")
let profile = ParseSSURL(url)
let profile = ServerProfile(url: url)
XCTAssertNil(profile)
}