Skip to content

Commit

Permalink
subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Sep 17, 2023
1 parent df12e65 commit a2c6919
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 93 deletions.
8 changes: 4 additions & 4 deletions V2rayU/Import.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ImportUri {
var error: String = ""
var uri: String = ""

static func importUri(uri: String, id: 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 @@ -25,7 +25,7 @@ class ImportUri {

if uri.hasPrefix("vmess://") {
let importUri = ImportUri()
importUri.importVmessUri(uri: uri, id: id)
importUri.importVmessUri(uri: uri)
return importUri
}
if uri.hasPrefix("trojan://") {
Expand Down Expand Up @@ -167,7 +167,7 @@ class ImportUri {
}
}

func importVmessUri(uri: String, id: String = "") {
func importVmessUri(uri: String) {
var url = URL(string: uri)
if url == nil {
// 标准url不支持非url-encoded
Expand Down Expand Up @@ -265,7 +265,7 @@ class ImportUri {
}
}

func importVlessUri(uri: String, id: String = "") {
func importVlessUri(uri: String) {
var url = URL(string: uri)
if url == nil {
// 标准url不支持非url-encoded
Expand Down
190 changes: 101 additions & 89 deletions V2rayU/V2raySubscribe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,101 +292,54 @@ class V2raySubSync: NSObject {
// remove old v2ray servers by subscribe
V2rayServer.remove(subscribe: subscribe)

if self.parseYaml(strTmp: strTmp, subscribe: subscribe) {
if self.importByYaml(strTmp: strTmp, subscribe: subscribe) {
return
}

let id: String = String(url.suffix(32));

let list = strTmp.trimmingCharacters(in: .newlines).components(separatedBy: CharacterSet.newlines)
var count = 0
for item in list {
count += 1
if count > 50 {
break // limit 50
}
// import every server
if (item.count == 0) {
continue;
} else {
self.importUri(uri: item.trimmingCharacters(in: .whitespacesAndNewlines), subscribe: subscribe, id: id)
}
}

// refresh server
menuController.showServers()

// reload server
if menuController.configWindow != nil {
// fix: must be used from main thread only
DispatchQueue.main.async {
menuController.configWindow.serversTableView.reloadData()
self.importByNormal(strTmp: strTmp, subscribe: subscribe)
}

func getOld(subscribe: String) -> [String] {
// reload all
V2rayServer.loadConfig()
// get old
var oldList: [String] = []
for (_, item) in V2rayServer.list().enumerated() {
if item.subscribe == subscribe {
oldList.append(item.name)
}
}
return oldList
}

func parseYaml(strTmp: String, subscribe: String) -> Bool {
func importByYaml(strTmp: String, subscribe: String) -> Bool {
// parse clash yaml
do {
var oldList = getOld(subscribe: subscribe)
var exists: Dictionary = [String: Bool]()

let decoder = YAMLDecoder()
let decoded = try decoder.decode(Clash.self, from: strTmp)
// reload all
V2rayServer.loadConfig()
// get old
var oldList: [String] = []
for (_, item) in V2rayServer.list().enumerated() {
if item.subscribe == subscribe {
oldList.append(item.name)
}
}
// new exits
var exists: Dictionary = [String: Bool]()
for item in decoded.proxies {
if let importUri = importByClash(clash: item) {
if importUri.isValid {
// old share uri
let v2ray = V2rayItem(name: "tmp", remark: item.name, isValid: importUri.isValid, json: importUri.json, url: "", subscribe: subscribe)
let share = ShareUri()
share.qrcode(item: v2ray)
if let v2rayOld = V2rayServer.exist(url: share.uri) {
v2rayOld.json = importUri.json
v2rayOld.isValid = importUri.isValid
v2rayOld.name = item.name
v2rayOld.store()
logTip(title: "success update: ", informativeText: importUri.remark)
exists[v2rayOld.name] = true
} else {
// add server
V2rayServer.add(remark: item.name, json: importUri.json, isValid: true, url: share.uri, subscribe: subscribe)
logTip(title: "success add: ", informativeText: importUri.remark)
}
} else {
logTip(title: "fail: ", informativeText: importUri.error)
importUri.remark = item.name
if let v2rayOld = self.saveImport(importUri: importUri, subscribe: subscribe) {
exists[v2rayOld.name] = true
}
}
}

logTip(title: "need remove?: ", informativeText: "\(oldList.count) - \(exists.count)")

// remove not exist
for name in oldList {
if exists[name] ?? false {
// delete from v2ray UserDefaults
V2rayItem.remove(name: name)
logTip(title: "remove: ", informativeText: name)
}
logTip(title: "need remove?: ", informativeText: "old=\(oldList.count) - new=\(exists.count)")

// remove not exist
for name in oldList {
if exists[name] ?? false {
// delete from v2ray UserDefaults
V2rayItem.remove(name: name)
logTip(title: "remove: ", informativeText: name)
}
}

// refresh server
menuController.showServers()

// reload server
if menuController.configWindow != nil {
// fix: must be used from main thread only
DispatchQueue.main.async {
menuController.configWindow.serversTableView.reloadData()
}
}
self.refreshMenu()
return true
} catch {
NSLog("parseYaml \(error)")
Expand All @@ -395,23 +348,82 @@ class V2raySubSync: NSObject {
return false
}

func importUri(uri: String, subscribe: String, id: String) {
if uri.count == 0 {
logTip(title: "fail: ", uri: uri, informativeText: "uri not found")
return
func importByNormal(strTmp: String, subscribe: String) {
var oldList = getOld(subscribe: subscribe)
var exists: Dictionary = [String: Bool]()

let list = strTmp.trimmingCharacters(in: .newlines).components(separatedBy: CharacterSet.newlines)
var count = 0
for uri in list {
count += 1
if count > 50 {
break // limit 50
}
// import every server
if (uri.count > 0) {
let filterUri = uri.trimmingCharacters(in: .whitespacesAndNewlines)
if let importUri = ImportUri.importUri(uri: filterUri,checkExist: false) {
if let v2rayOld = self.saveImport(importUri: importUri, subscribe: subscribe) {
exists[v2rayOld.name] = true
}
}
}
}

logTip(title: "need remove?: ", informativeText: "old=\(oldList.count) - new=\(exists.count)")

// remove not exist
for name in oldList {
if exists[name] ?? false {
// delete from v2ray UserDefaults
V2rayItem.remove(name: name)
logTip(title: "remove: ", informativeText: name)
}
}

self.refreshMenu()
}

func refreshMenu() {
// refresh server
menuController.showServers()

if let importUri = ImportUri.importUri(uri: uri, id: id) {
if importUri.isValid {
// add server
V2rayServer.add(remark: importUri.remark, json: importUri.json, isValid: true, url: importUri.uri, subscribe: subscribe)
logTip(title: "success: ", uri: uri, informativeText: importUri.remark)
// reload server
if menuController.configWindow != nil {
// fix: must be used from main thread only
DispatchQueue.main.async {
menuController.configWindow.serversTableView.reloadData()
}
}
}

func saveImport(importUri: ImportUri, subscribe: String) -> V2rayItem? {
if importUri.isValid {
var newUri = importUri.uri
// clash has no uri
if newUri.count == 0 {
// old share uri
let v2ray = V2rayItem(name: "tmp", remark: importUri.remark, isValid: importUri.isValid, json: importUri.json, url: "", subscribe: subscribe)
let share = ShareUri()
share.qrcode(item: v2ray)
newUri = share.uri
}
if let v2rayOld = V2rayServer.exist(url: newUri) {
v2rayOld.json = importUri.json
v2rayOld.isValid = importUri.isValid
v2rayOld.name = importUri.remark
v2rayOld.store()
logTip(title: "success update: ", informativeText: importUri.remark)
return v2rayOld
} else {
logTip(title: "fail: ", uri: uri, informativeText: importUri.error)
// add server
V2rayServer.add(remark: importUri.remark, json: importUri.json, isValid: true, url: newUri, subscribe: subscribe)
logTip(title: "success add: ", informativeText: importUri.remark)
}

return
} else {
logTip(title: "fail: ", informativeText: importUri.error)
}
return nil
}

func logTip(title: String = "", uri: String = "", informativeText: String = "") {
Expand Down

0 comments on commit a2c6919

Please sign in to comment.