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