Skip to content

Commit

Permalink
feat: add update external resources action
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Sep 5, 2023
1 parent 6cf0b5e commit 1d2abc3
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 10 deletions.
16 changes: 16 additions & 0 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
495A44D320D267D000888A0A /* LaunchAtLogin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44D220D267D000888A0A /* LaunchAtLogin.swift */; };
495BFB8821919B9800C8779D /* RemoteConfigManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495BFB8721919B9800C8779D /* RemoteConfigManager.swift */; };
4960A6DB2136529200B940C9 /* JSBridgeHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4960A6DA2136529200B940C9 /* JSBridgeHandler.swift */; };
496322222AA5D89E00854231 /* UpdateExternalResourceAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496322212AA5D89E00854231 /* UpdateExternalResourceAction.swift */; };
496322242AA5D91200854231 /* BaseAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496322232AA5D91200854231 /* BaseAction.swift */; };
4966E9E32118153A00A391FB /* NSUserNotificationCenter+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */; };
4966E9E6211824F300A391FB /* NSImage+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4966E9E5211824F300A391FB /* NSImage+extension.swift */; };
4969E73D2A5E3CB20012E005 /* ConnectionDetailInfoGeneralView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4969E73F2A5E3CB20012E005 /* ConnectionDetailInfoGeneralView.xib */; };
Expand Down Expand Up @@ -190,6 +192,8 @@
495A44D220D267D000888A0A /* LaunchAtLogin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchAtLogin.swift; sourceTree = "<group>"; };
495BFB8721919B9800C8779D /* RemoteConfigManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteConfigManager.swift; sourceTree = "<group>"; };
4960A6DA2136529200B940C9 /* JSBridgeHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSBridgeHandler.swift; sourceTree = "<group>"; };
496322212AA5D89E00854231 /* UpdateExternalResourceAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateExternalResourceAction.swift; sourceTree = "<group>"; };
496322232AA5D91200854231 /* BaseAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseAction.swift; sourceTree = "<group>"; };
4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSUserNotificationCenter+Extension.swift"; sourceTree = "<group>"; };
4966E9E5211824F300A391FB /* NSImage+extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+extension.swift"; sourceTree = "<group>"; };
4969E73E2A5E3CB20012E005 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ConnectionDetailInfoGeneralView.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -426,6 +430,15 @@
path = Views;
sourceTree = "<group>";
};
496322202AA5D88100854231 /* Actions */ = {
isa = PBXGroup;
children = (
496322212AA5D89E00854231 /* UpdateExternalResourceAction.swift */,
496322232AA5D91200854231 /* BaseAction.swift */,
);
path = Actions;
sourceTree = "<group>";
};
49722FDD211ED2A900650A41 /* Vendor */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -593,6 +606,7 @@
491C24FF21BD558B00AB5D44 /* Extensions */,
492C4866210EE69B004554A0 /* General */,
4931969C21631F2E00A8E6E7 /* Views */,
496322202AA5D88100854231 /* Actions */,
4989F98520D0AA300001E564 /* ViewControllers */,
49761DA521C9490400AE13EF /* Resources */,
49CF3B3A20CD783A001EBF94 /* Support Files */,
Expand Down Expand Up @@ -882,6 +896,7 @@
499A485522ED707300F6C675 /* RemoteConfigViewController.swift in Sources */,
49D6A45229AEEC15006487EF /* StatusItemTool.swift in Sources */,
49CF3B5C20CE8068001EBF94 /* ClashResourceManager.swift in Sources */,
496322242AA5D91200854231 /* BaseAction.swift in Sources */,
4952C3D02117027C004A4FA8 /* ConfigFileManager.swift in Sources */,
49BC061C212931F4005A0FE7 /* AboutViewController.swift in Sources */,
4949D154213242F600EF85E6 /* Paths.swift in Sources */,
Expand Down Expand Up @@ -966,6 +981,7 @@
49769FB427E9B3E400E3D664 /* LoginKitWrapper.m in Sources */,
4960A6DB2136529200B940C9 /* JSBridgeHandler.swift in Sources */,
493AEAE5221AE7230016FE98 /* ProxyMenuItem.swift in Sources */,
496322222AA5D89E00854231 /* UpdateExternalResourceAction.swift in Sources */,
499A485E22ED9B7C00F6C675 /* NSTableView+Reload.swift in Sources */,
F939724C23A4B33500FE5A3F /* ClashProvider.swift in Sources */,
0AEF6EAF2A5F961B00EFEE23 /* SectionedTableView.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions ClashX/Actions/BaseAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// BaseAction.swift
// ClashX
//
// Created by yicheng on 2023/9/4.
// Copyright © 2023 west2online. All rights reserved.
//

protocol BaseStaticAction {
static func run()
}
55 changes: 55 additions & 0 deletions ClashX/Actions/UpdateExternalResourceAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// UpdateExternalResourceAction.swift
// ClashX
//
// Created by yicheng on 2023/9/4.
// Copyright © 2023 west2online. All rights reserved.
//

import Foundation
enum UpdateExternalResourceAction:BaseStaticAction {
static func run() {
ApiRequest.requestExternalProviderNames { provider in
let group = DispatchGroup()
var successCount = 0
let totalCount = provider.proxies.count + provider.rules.count
if totalCount == 0 {
self.onFinished(success: 0, total: 0, fails: [])
return
}
var fails = [String]()
for name in provider.proxies {
group.enter()
ApiRequest.updateProvider(name: name, type: .proxy) { success in
if success { successCount += 1 } else {
fails.append(name)
}
group.leave()
}
}

for name in provider.rules {
group.enter()
ApiRequest.updateProvider(name: name, type: .rule) { success in
if success { successCount += 1 } else {
fails.append(name)
}
group.leave()
}
}

group.notify(queue: .main) {
self.onFinished(success: successCount, total: totalCount, fails: fails)
}

}
}

private static func onFinished(success:Int, total: Int, fails: [String]) {
var info = String(format: NSLocalizedString("total: %d, success: %d", comment: ""), total, success)
if !fails.isEmpty {
info.append(String(format: NSLocalizedString("fails: %@", comment: ""), fails.joined(separator: " ")))
}
NSUserNotificationCenter.default.post(title: NSLocalizedString("Update external resource complete", comment: ""), info: info)
}
}
4 changes: 4 additions & 0 deletions ClashX/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,10 @@ extension AppDelegate {
}
}

@IBAction func actionUpdateExternalResource(_ sender: Any) {
UpdateExternalResourceAction.run()
}

@IBAction func actionQuit(_ sender: Any) {
NSApplication.shared.terminate(self)
}
Expand Down
10 changes: 8 additions & 2 deletions ClashX/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@
<action selector="actionUpdateConfig:" target="Voe-Tx-rLC" id="oW5-Ll-SY8"/>
</connections>
</menuItem>
<menuItem title="Update external resources" keyEquivalent="U" id="9g1-lW-mA8">
<modifierMask key="keyEquivalentModifierMask" shift="YES" command="YES"/>
<connections>
<action selector="actionUpdateExternalResource:" target="Voe-Tx-rLC" id="wYg-KO-78x"/>
</connections>
</menuItem>
<menuItem title="Remote config" id="h1C-R6-Y9w">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Remote config" id="az2-wz-yyy">
Expand Down Expand Up @@ -828,9 +834,9 @@
</items>
</menu>
</menuItem>
<menuItem title="Remote Controller" id="BRR-WK-aeP">
<menuItem title="Remote controller" id="BRR-WK-aeP">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Remote Controller" id="1He-Eq-fSy">
<menu key="submenu" title="Remote controller" id="1He-Eq-fSy">
<items>
<menuItem isSeparatorItem="YES" id="tXn-AQ-OT3"/>
<menuItem title=" Manage" id="hlb-KQ-Fdr">
Expand Down
72 changes: 72 additions & 0 deletions ClashX/General/ApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,78 @@ extension ApiRequest {
req("/connections", method: .delete).response { _ in }
}
}

// MARK: - Providers

struct AllProviders {
var proxies = [String]()
var rules = [String]()
}

static func requestExternalProviderNames(completeHandler: @escaping (AllProviders) -> Void) {
var providers = AllProviders()
let group = DispatchGroup()
group.enter()
ApiRequest.req("/providers/proxies").responseData { resp in
switch resp.result {
case let .success(res):
let json = JSON(res)
let provoders = json["providers"].dictionaryValue
.filter({ $0.value["vehicleType"] == "HTTP" }).map { $0.key }
providers.proxies = provoders
case let .failure(err):
Logger.log(err.localizedDescription, level: .warning)
}
group.leave()
}

#if PRO_VERSION
group.enter()
ApiRequest.req("/providers/rules").responseData { resp in
switch resp.result {
case let .success(res):
let json = JSON(res)
let provoders = json["providers"].dictionaryValue
.filter({ $0.value["vehicleType"] == "HTTP" }).map { $0.key }
providers.rules = provoders
case let .failure(err):
Logger.log(err.localizedDescription, level: .warning)
}
group.leave()
}
#endif
group.notify(queue: .main) {
completeHandler(providers)
}
}

enum ProviderType {
case proxy
case rule
}

static func updateProvider(name: String, type: ProviderType, completeHandler: @escaping (Bool) -> Void) {
let url: String
switch type {
case .proxy:
url = "/providers/proxies/\(name.encoded)"
case .rule:
url = "/providers/rules/\(name.encoded)"
}
ApiRequest.req(url, method: .put).response { resp in
if resp.response?.statusCode == 204 {
completeHandler(true)
} else {
completeHandler(false)
}
}
}

static func resetFakeIpCache() {
ApiRequest.req("/cache/fakeip/flush", method: .post).response { resp in
Logger.log("flush fake ip: \(resp.response?.statusCode ?? -1)")
}
}
}

// MARK: - Stream Apis
Expand Down
9 changes: 9 additions & 0 deletions ClashX/Support Files/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
/* No comment provided by engineer. */
"Fail:" = "Fail:";

/* No comment provided by engineer. */
"fails: %@" = "fails: %@";

/* No comment provided by engineer. */
"Global" = "Global";

Expand Down Expand Up @@ -259,12 +262,18 @@
/* No comment provided by engineer. */
"This version of ClashX contains a break change due to clash core 1.0 released. Check if your config is not working properly." = "This version of ClashX contains a break change due to clash core 1.0 released. Check if your config is not working properly.";

/* No comment provided by engineer. */
"total: %d, success: %d" = "total: %d, success: %d";

/* No comment provided by engineer. */
"Type" = "Type";

/* No comment provided by engineer. */
"Unknown" = "Unknown";

/* No comment provided by engineer. */
"Update external resource complete" = "Update external resource complete";

/* No comment provided by engineer. */
"Update GEOIP Database" = "Update GEOIP Database";

Expand Down
9 changes: 9 additions & 0 deletions ClashX/Support Files/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
/* No comment provided by engineer. */
"Fail:" = "失败:";

/* No comment provided by engineer. */
"fails: %@" = "失败: %@";

/* No comment provided by engineer. */
"Global" = "全局";

Expand Down Expand Up @@ -259,12 +262,18 @@
/* No comment provided by engineer. */
"This version of ClashX contains a break change due to clash core 1.0 released. Check if your config is not working properly." = "由于Clash Core发布1.0版本,使用此版本的 ClashX 可能需要更新配置内容\n前往 https://github.com/Dreamacro/clash/wiki/breaking-changes-in-1.0.0 查看详情";

/* No comment provided by engineer. */
"total: %d, success: %d" = "共计: %d, 成功: %d";

/* No comment provided by engineer. */
"Type" = "类型";

/* No comment provided by engineer. */
"Unknown" = "未知";

/* No comment provided by engineer. */
"Update external resource complete" = "更新外部资源完成";

/* No comment provided by engineer. */
"Update GEOIP Database" = "更新IP数据库";

Expand Down
9 changes: 9 additions & 0 deletions ClashX/Support Files/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
/* No comment provided by engineer. */
"Fail:" = "失敗:";

/* No comment provided by engineer. */
"fails: %@" = "失敗: %@";

/* No comment provided by engineer. */
"Global" = "全局";

Expand Down Expand Up @@ -259,12 +262,18 @@
/* No comment provided by engineer. */
"This version of ClashX contains a break change due to clash core 1.0 released. Check if your config is not working properly." = "此版本的 ClashX 包含因 clash core 1.0 發布而導致的中斷更改。請檢查您的配置是否無法正常工作";

/* No comment provided by engineer. */
"total: %d, success: %d" = "共計: %d, 成功: %d";

/* No comment provided by engineer. */
"Type" = "類型";

/* No comment provided by engineer. */
"Unknown" = "未知";

/* No comment provided by engineer. */
"Update external resource complete" = "更新外部資源完成";

/* No comment provided by engineer. */
"Update GEOIP Database" = "更新 GEOIP 數據庫";

Expand Down
11 changes: 7 additions & 4 deletions ClashX/zh-Hans.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
/* Class = "NSMenuItem"; title = "Copy shell command"; ObjectID = "Jmb-PK-rMW"; */
"Jmb-PK-rMW.title" = "复制终端代理命令";

/* Class = "NSMenuItem"; title = "Config"; ObjectID = "JMV-Dy-CI0"; */
/* Class = "NSMenuItem"; title = "Configs"; ObjectID = "JMV-Dy-CI0"; */
"JMV-Dy-CI0.title" = "配置";

/* Class = "NSMenuItem"; title = "WARNING"; ObjectID = "ko2-Ir-DxA"; */
Expand Down Expand Up @@ -103,7 +103,7 @@
/* Class = "NSTextFieldCell"; title = "Text Cell"; ObjectID = "RCv-zz-HKW"; */
"RCv-zz-HKW.title" = "Text Cell";

/* Class = "NSMenu"; title = "Config"; ObjectID = "tck-zU-JKQ"; */
/* Class = "NSMenu"; title = "Configs"; ObjectID = "tck-zU-JKQ"; */
"tck-zU-JKQ.title" = "配置";

/* Class = "NSTextFieldCell"; title = "Configs"; ObjectID = "tL1-bl-LXd"; */
Expand Down Expand Up @@ -163,10 +163,10 @@
/* Class = "NSTableColumn"; headerCell.title = "Api Url"; ObjectID = "yO6-uZ-IRv"; */
"yO6-uZ-IRv.headerCell.title" = "Api Url";

/* Class = "NSMenu"; title = "Remote Controller"; ObjectID = "1He-Eq-fSy"; */
/* Class = "NSMenu"; title = "Remote controller"; ObjectID = "1He-Eq-fSy"; */
"1He-Eq-fSy.title" = "远程控制器";

/* Class = "NSMenuItem"; title = "Remote Controller"; ObjectID = "BRR-WK-aeP"; */
/* Class = "NSMenuItem"; title = "Remote controller"; ObjectID = "BRR-WK-aeP"; */
"BRR-WK-aeP.title" = "远程控制器";

/* Class = "NSMenuItem"; title = " Manage"; ObjectID = "hlb-KQ-Fdr"; */
Expand Down Expand Up @@ -309,3 +309,6 @@

/* Class = "NSButtonCell"; title = "Update"; ObjectID = "pHl-C4-fNt"; */
"pHl-C4-fNt.title" = "更新";

/* Class = "NSMenuItem"; title = "Update external resources"; ObjectID = "9g1-lW-mA8"; */
"9g1-lW-mA8.title" = "更新外部资源";
Loading

0 comments on commit 1d2abc3

Please sign in to comment.