Support parse SS URL with query string

So we can encode Remark and OTA to query string
This commit is contained in:
Rainux Luo
2017-01-07 08:51:57 +08:00
parent faf029c7f7
commit 5e06b4b4f0
2 changed files with 34 additions and 2 deletions

View File

@ -36,23 +36,36 @@ func ParseSSURL(_ url: URL?) -> [String: Any?]? {
return nil
}
var plainUrl: URL! = url
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 = URL(string: "ss://\(decoded!)")
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

@ -45,6 +45,23 @@ class UtilsTests: XCTestCase {
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
XCTAssertEqual(profile?["Password"] as? String, "password")
XCTAssertEqual(profile?["Remark"] as? String, "Prism")
XCTAssertEqual(profile?["OTA"] as? Bool, true)
}
func testParseSSURLwithPlainURLandAnotherQuery() {
let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0")
let profile = ParseSSURL(url)
XCTAssertNotNil(profile)
XCTAssertEqual(profile?["ServerHost"] as? String, "example.com")
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
XCTAssertEqual(profile?["Password"] as? String, "password")
XCTAssertEqual(profile?["Remark"] as? String, "Prism")
XCTAssertEqual(profile?["OTA"] as? Bool, false)
}
func testParseSSURLwithBase64EncodedURL() {
@ -73,6 +90,8 @@ class UtilsTests: XCTestCase {
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
XCTAssertEqual(profile?["Password"] as? String, "password")
XCTAssertEqual(profile?["Remark"] as? String, "Prism")
XCTAssertEqual(profile?["OTA"] as? Bool, true)
}
func testParseSSURLwithEmptyURL() {