Refactor ParseSSURL as ServerProfile initializer
This commit is contained in:
@ -121,9 +121,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
|||||||
var isChanged = false
|
var isChanged = false
|
||||||
|
|
||||||
for url in urls {
|
for url in urls {
|
||||||
let profielDict = ParseSSURL(url)
|
if let profile = ServerProfile(url: url) {
|
||||||
if let profielDict = profielDict {
|
|
||||||
let profile = ServerProfile.fromDictionary(profielDict)
|
|
||||||
mgr.profiles.append(profile)
|
mgr.profiles.append(profile)
|
||||||
isChanged = true
|
isChanged = true
|
||||||
|
|
||||||
|
@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ServerProfile: NSObject {
|
class ServerProfile: NSObject {
|
||||||
var uuid: String
|
var uuid: String
|
||||||
|
|
||||||
@ -28,6 +26,56 @@ class ServerProfile: NSObject {
|
|||||||
self.uuid = uuid
|
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 {
|
static func fromDictionary(_ data:[String: Any?]) -> ServerProfile {
|
||||||
let cp = {
|
let cp = {
|
||||||
(profile: ServerProfile) in
|
(profile: ServerProfile) in
|
||||||
|
@ -23,53 +23,3 @@ extension Data {
|
|||||||
return hexBytes.joined(separator: "")
|
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,
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
@ -37,7 +37,7 @@ class ServerProfileTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testServerProfileURL() {
|
func testServerProfileURL() {
|
||||||
let parsed = ParseSSURL(profile.URL())
|
let parsed = ServerProfile(url: profile.URL())?.toDictionary()
|
||||||
|
|
||||||
XCTAssertNotNil(parsed)
|
XCTAssertNotNil(parsed)
|
||||||
|
|
||||||
@ -49,6 +49,17 @@ class ServerProfileTests: XCTestCase {
|
|||||||
XCTAssertEqual(parsed?["OTA"] as? Bool, profile.ota)
|
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() {
|
func testPerformanceExample() {
|
||||||
// This is an example of a performance test case.
|
// This is an example of a performance test case.
|
||||||
self.measure {
|
self.measure {
|
||||||
|
@ -24,7 +24,7 @@ class UtilsTests: XCTestCase {
|
|||||||
func testParseSSURLwithPlainURL() {
|
func testParseSSURLwithPlainURL() {
|
||||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388")
|
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)?.toDictionary()
|
||||||
|
|
||||||
XCTAssertNotNil(profile)
|
XCTAssertNotNil(profile)
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ class UtilsTests: XCTestCase {
|
|||||||
func testParseSSURLwithPlainURLandQuery() {
|
func testParseSSURLwithPlainURLandQuery() {
|
||||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true")
|
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)
|
XCTAssertNotNil(profile)
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ class UtilsTests: XCTestCase {
|
|||||||
func testParseSSURLwithPlainURLandAnotherQuery() {
|
func testParseSSURLwithPlainURLandAnotherQuery() {
|
||||||
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0")
|
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)
|
XCTAssertNotNil(profile)
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ class UtilsTests: XCTestCase {
|
|||||||
// "ss://aes-256-cfb:password@example.com:8388"
|
// "ss://aes-256-cfb:password@example.com:8388"
|
||||||
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")
|
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)?.toDictionary()
|
||||||
|
|
||||||
XCTAssertNotNil(profile)
|
XCTAssertNotNil(profile)
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ class UtilsTests: XCTestCase {
|
|||||||
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
|
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
|
||||||
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")
|
let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)?.toDictionary()
|
||||||
|
|
||||||
XCTAssertNotNil(profile)
|
XCTAssertNotNil(profile)
|
||||||
|
|
||||||
@ -97,7 +97,7 @@ class UtilsTests: XCTestCase {
|
|||||||
func testParseSSURLwithEmptyURL() {
|
func testParseSSURLwithEmptyURL() {
|
||||||
let url = URL(string: "ss://")
|
let url = URL(string: "ss://")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)
|
||||||
|
|
||||||
XCTAssertNil(profile)
|
XCTAssertNil(profile)
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ class UtilsTests: XCTestCase {
|
|||||||
func testParseSSURLwithInvalidURL() {
|
func testParseSSURLwithInvalidURL() {
|
||||||
let url = URL(string: "ss://invalid url")
|
let url = URL(string: "ss://invalid url")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)
|
||||||
|
|
||||||
XCTAssertNil(profile)
|
XCTAssertNil(profile)
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ class UtilsTests: XCTestCase {
|
|||||||
// "ss://invalid url"
|
// "ss://invalid url"
|
||||||
let url = URL(string: "ss://aW52YWxpZCB1cmw")
|
let url = URL(string: "ss://aW52YWxpZCB1cmw")
|
||||||
|
|
||||||
let profile = ParseSSURL(url)
|
let profile = ServerProfile(url: url)
|
||||||
|
|
||||||
XCTAssertNil(profile)
|
XCTAssertNil(profile)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user