Skip to content

Commit

Permalink
fix decode ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed May 26, 2019
1 parent 1672c92 commit 5c98d44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
4 changes: 2 additions & 2 deletions V2rayU/Preference/PreferenceSubscribe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ final class PreferenceSubscribeViewController: NSViewController, PreferencePane
public func sync() {
let list = V2raySubscribe.list()

if list.count==0{
if list.count == 0 {
logTip(title: "fail: ", uri: "", informativeText: " please add Subscription Url ")
}

Expand Down Expand Up @@ -161,7 +161,7 @@ final class PreferenceSubscribeViewController: NSViewController, PreferencePane
let list = strTmp!.components(separatedBy: CharacterSet.newlines)
for item in list {
// import every server
self.importUri(uri: item)
self.importUri(uri: item.trimmingCharacters(in: .whitespacesAndNewlines))
}
}

Expand Down
31 changes: 10 additions & 21 deletions V2rayU/Scanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class ImportUri {
var remark: String = ""
var error: String = ""
var uri: String = ""
static func importUri(uri: String,checkExist:Bool = true) -> ImportUri? {

static func importUri(uri: String, checkExist: Bool = true) -> ImportUri? {
if checkExist && V2rayServer.exist(url: uri) {
let importUri = ImportUri()
importUri.isValid = false
Expand All @@ -149,7 +149,7 @@ class ImportUri {
}
return nil
}

static func supportProtocol(uri: String) -> Bool {
if uri.hasPrefix("ss://") || uri.hasPrefix("ssr://") || uri.hasPrefix("vmess://") {
return true
Expand Down Expand Up @@ -575,17 +575,12 @@ class ShadowsockUri {
let base64End = urlStr.firstIndex(of: "#")
let encodedStr = String(urlStr[base64Begin..<(base64End ?? urlStr.endIndex)])

guard let data = Data(base64Encoded: self.padBase64(string: encodedStr)) else {
guard let decoded = encodedStr.base64Decoded() else {
self.error = "decode ss error"
return (url.absoluteString, nil)
}

guard let decoded = String(data: data, encoding: String.Encoding.utf8) else {
self.error = "decode ss error"
return (nil, nil)
}

let s = decoded.trimmingCharacters(in: CharacterSet(charactersIn: "\n"))
let s = decoded.trimmingCharacters(in: .whitespacesAndNewlines)

if let index = base64End {
let i = urlStr.index(index, offsetBy: 1)
Expand All @@ -607,7 +602,7 @@ class ShadowsockUri {
}

// link: https://coderschool.cn/2498.html
class ShadowsockRUri : ShadowsockUri {
class ShadowsockRUri: ShadowsockUri {

override func Init(url: URL) {
let (_decodedUrl, _tag) = self.decodeUrl(url: url)
Expand Down Expand Up @@ -649,27 +644,21 @@ class ShadowsockRUri : ShadowsockUri {
// remove left ssr://
let base64Begin = urlStr.index(urlStr.startIndex, offsetBy: 6)
let encodedStr = String(urlStr[base64Begin...])

guard let data = Data(base64Encoded: self.padBase64(string: encodedStr)) else {
self.error = "decode ssr error"
return (url.absoluteString, nil)
}

guard let decoded = String(data: data, encoding: String.Encoding.utf8) else {
guard let decoded = encodedStr.base64Decoded() else {
self.error = "decode ssr error"
return (nil, nil)
return (url.absoluteString, nil)
}

let raw = decoded.trimmingCharacters(in: CharacterSet(charactersIn: "\n"))
let raw = decoded.trimmingCharacters(in: .whitespacesAndNewlines)

let sep = raw.range(of: "/?")
let s = String(raw[..<(sep?.lowerBound ?? raw.endIndex)])
if let iBeg = raw.range(of: "remarks=")?.upperBound {
let fragment = String(raw[iBeg...])
let iEnd = fragment.firstIndex(of: "&")
let aRemarks = String(fragment[..<(iEnd ?? raw.endIndex)])
guard let data = Data(base64Encoded: self.padBase64(string: aRemarks)), let tag = String(data: data, encoding: .utf8) else {
// self.error = "decode ssr error remarks is not base64"
guard let tag = aRemarks.base64Decoded() else {
return (s, aRemarks)
}
return (s, tag)
Expand Down

0 comments on commit 5c98d44

Please sign in to comment.