|
@@ -70,13 +70,60 @@ class GlobalController extends GetxController {
|
|
|
|
|
|
Future<void> fetchNodes() async {
|
|
|
nodeModes.value = await ApiService().getNode("/api/client/v4/nodes?vless=1");
|
|
|
+ await makeProxy();
|
|
|
+ if(controllers.service.coreStatus.value == RunningState.stoped){
|
|
|
+ await controllers.service.reloadClashCore();
|
|
|
+ }
|
|
|
+ if (controllers.service.coreStatus.value != RunningState.running) return;
|
|
|
+ await controllers.core.updateVersion();
|
|
|
+ await updateDate();
|
|
|
+
|
|
|
+ NodeMode? targetNode;
|
|
|
+ if (selectedNode.value == null) {
|
|
|
+ targetNode = await findNodeWithMinUsers(nodeModes);
|
|
|
+ } else {
|
|
|
+ targetNode = selectedNode.value;
|
|
|
+ }
|
|
|
+ if (targetNode != null){
|
|
|
+ selectNode(targetNode);
|
|
|
+ ProxieProxiesItem? targetProxie = await findProxieByName(targetNode.name);
|
|
|
+ if (targetProxie != null) {
|
|
|
+ handleSetProxieGroup(targetProxie, targetNode.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ // Future<ProxieProxiesItem> findProxieByName(String name) async {
|
|
|
+ // return proxieGroups.firstWhere((proxie) => proxie['name'] == name, orElse: () => null);
|
|
|
+ // }
|
|
|
+
|
|
|
+ Future<ProxieProxiesItem?> findProxieByName(String name) async {
|
|
|
+ ProxieProxiesItem? result;
|
|
|
+ try {
|
|
|
+ result = proxieGroups.firstWhere((proxie) => proxie.all!.contains(name));
|
|
|
+ } catch (e) {
|
|
|
+ // 在这里可以处理异常,或者只是简单地返回 null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
Future<void> makeProxy() async {
|
|
|
await controllers.config.makeClashConfig(nodeModes);
|
|
|
- await controllers.service.reloadClashCore();
|
|
|
+ // await controllers.service.reloadClashCore();
|
|
|
|
|
|
}
|
|
|
+ Future<NodeMode> findNodeWithMinUsers(List<NodeMode> nodes) async {
|
|
|
+ return nodes
|
|
|
+ .where((node) => node.countryCode == "hk")
|
|
|
+ .reduce((a, b) => a.onlineUsers < b.onlineUsers ? a : b);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
Future<SystemProxyConfig> getSysProxy() async{
|
|
|
return await SystemProxy.instance.get();
|
|
@@ -152,6 +199,16 @@ class GlobalController extends GetxController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ Future<void> handleSetSelectProxieGroup(NodeMode proxie, String value) async {
|
|
|
+ if (proxie.name == value) return;
|
|
|
+ await controllers.core.fetchSetProxieGroup(proxie.name, value);
|
|
|
+ await updateDate();
|
|
|
+ final conn = await controllers.core.fetchConnection();
|
|
|
+ for (final it in conn.connections) {
|
|
|
+ if (it.chains.contains(proxie.name)) controllers.core.fetchCloseConnections(it.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void watchExit() {
|
|
|
// watch process kill
|
|
|
// ref https://github.com/dart-lang/sdk/issues/12170
|
|
@@ -169,6 +226,18 @@ class GlobalController extends GetxController {
|
|
|
handleExit();
|
|
|
});
|
|
|
}
|
|
|
+ void selectNode(NodeMode node) {
|
|
|
+ controllers.global.selectedNode.value = node;
|
|
|
+ _storeSelectedNode(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ Future<void> _storeSelectedNode(NodeMode node) async {
|
|
|
+ final prefs = await SharedPreferences.getInstance();
|
|
|
+ // 为简化起见,我们只存储node的ID,但您可以根据需要存储更多信息
|
|
|
+ prefs.setInt('selectedNodeId', node.id);
|
|
|
+ await loadSelectedNode();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
Future<void> loadSelectedNode() async {
|
|
|
final prefs = await SharedPreferences.getInstance();
|