Support generate SS URL with Remark and OTA
This commit is contained in:
@ -121,9 +121,22 @@ class ServerProfile: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func URL() -> Foundation.URL? {
|
func URL() -> Foundation.URL? {
|
||||||
let parts = "\(method):\(password)@\(serverHost):\(serverPort)"
|
var url = URLComponents()
|
||||||
let base64String = parts.data(using: String.Encoding.utf8)?
|
|
||||||
.base64EncodedString(options: NSData.Base64EncodingOptions())
|
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 {
|
if var s = base64String {
|
||||||
s = s.trimmingCharacters(in: CharacterSet(charactersIn: "="))
|
s = s.trimmingCharacters(in: CharacterSet(charactersIn: "="))
|
||||||
return Foundation.URL(string: "ss://\(s)")
|
return Foundation.URL(string: "ss://\(s)")
|
||||||
|
@ -28,9 +28,13 @@ func ParseSSURL(_ url: URL?) -> [String: Any?]? {
|
|||||||
|
|
||||||
func padBase64(string: String) -> String {
|
func padBase64(string: String) -> String {
|
||||||
var length = string.characters.count
|
var length = string.characters.count
|
||||||
|
if length % 4 == 0 {
|
||||||
|
return string
|
||||||
|
} else {
|
||||||
length = 4 - length % 4 + length
|
length = 4 - length % 4 + length
|
||||||
return string.padding(toLength: length, withPad: "=", startingAt: 0)
|
return string.padding(toLength: length, withPad: "=", startingAt: 0)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if url?.host == nil {
|
if url?.host == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -26,7 +26,7 @@ class ServerProfileTests: XCTestCase {
|
|||||||
"ServerPort": 8388,
|
"ServerPort": 8388,
|
||||||
"Method": "aes-256-cfb",
|
"Method": "aes-256-cfb",
|
||||||
"Password": "password",
|
"Password": "password",
|
||||||
"Remark": "Prism",
|
"Remark": "Protoss Prism",
|
||||||
"OTA": true])
|
"OTA": true])
|
||||||
XCTAssertNotNil(profile)
|
XCTAssertNotNil(profile)
|
||||||
}
|
}
|
||||||
@ -45,6 +45,8 @@ class ServerProfileTests: XCTestCase {
|
|||||||
XCTAssertEqual(parsed?["ServerPort"] as? UInt16, profile.serverPort)
|
XCTAssertEqual(parsed?["ServerPort"] as? UInt16, profile.serverPort)
|
||||||
XCTAssertEqual(parsed?["Method"] as? String, profile.method)
|
XCTAssertEqual(parsed?["Method"] as? String, profile.method)
|
||||||
XCTAssertEqual(parsed?["Password"] as? String, profile.password)
|
XCTAssertEqual(parsed?["Password"] as? String, profile.password)
|
||||||
|
XCTAssertEqual(parsed?["Remark"] as? String, profile.remark)
|
||||||
|
XCTAssertEqual(parsed?["OTA"] as? Bool, profile.ota)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPerformanceExample() {
|
func testPerformanceExample() {
|
||||||
|
Reference in New Issue
Block a user