Support parse SS URL with query string
So we can encode Remark and OTA to query string
This commit is contained in:
@ -36,23 +36,36 @@ func ParseSSURL(_ url: URL?) -> [String: Any?]? {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var plainUrl: URL! = url
|
var plainUrl: URLComponents! = URLComponents(url: url!,
|
||||||
|
resolvingAgainstBaseURL: false)
|
||||||
|
|
||||||
let data = Data(base64Encoded: padBase64(string: url!.host!),
|
let data = Data(base64Encoded: padBase64(string: url!.host!),
|
||||||
options: Data.Base64DecodingOptions())
|
options: Data.Base64DecodingOptions())
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
let decoded = String(data: data!, encoding: String.Encoding.utf8)
|
let decoded = String(data: data!, encoding: String.Encoding.utf8)
|
||||||
plainUrl = URL(string: "ss://\(decoded!)")
|
plainUrl = URLComponents(string: "ss://\(decoded!)")
|
||||||
|
|
||||||
if plainUrl == nil {
|
if plainUrl == nil {
|
||||||
return 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,
|
return ["ServerHost": plainUrl.host,
|
||||||
"ServerPort": UInt16(plainUrl.port!),
|
"ServerPort": UInt16(plainUrl.port!),
|
||||||
"Method": plainUrl.user,
|
"Method": plainUrl.user,
|
||||||
"Password": plainUrl.password,
|
"Password": plainUrl.password,
|
||||||
|
"Remark": remark,
|
||||||
|
"OTA": ota,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,23 @@ class UtilsTests: XCTestCase {
|
|||||||
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
|
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
|
||||||
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
|
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
|
||||||
XCTAssertEqual(profile?["Password"] as? String, "password")
|
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() {
|
func testParseSSURLwithBase64EncodedURL() {
|
||||||
@ -73,6 +90,8 @@ class UtilsTests: XCTestCase {
|
|||||||
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
|
XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388)
|
||||||
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
|
XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb")
|
||||||
XCTAssertEqual(profile?["Password"] as? String, "password")
|
XCTAssertEqual(profile?["Password"] as? String, "password")
|
||||||
|
XCTAssertEqual(profile?["Remark"] as? String, "Prism")
|
||||||
|
XCTAssertEqual(profile?["OTA"] as? Bool, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testParseSSURLwithEmptyURL() {
|
func testParseSSURLwithEmptyURL() {
|
||||||
|
Reference in New Issue
Block a user