alroyso 1 month ago
parent
commit
f796cd3bb7

+ 15 - 42
lib/app/clash/service/clash_service.dart

@@ -2,10 +2,8 @@ import 'dart:convert';
 import 'dart:io';
 import 'dart:async';
 import 'package:get/get.dart';
-import 'package:yaml/yaml.dart';
 import 'package:yaml_edit/yaml_edit.dart';
 import 'package:path/path.dart' as path;
-import '../../common/LogHelper.dart';
 import '../../common/constants.dart';
 import '../../const/const.dart';
 import '../../controller/controllers.dart';
@@ -82,16 +80,10 @@ class ClashService extends GetxController {
           dnsHijack: ['any:53'],
         ),
         proxies: [],
-        proxyGroups: [
-          ProxyGroup(
-            name: 'proxy',
-            type: 'select',
-            proxies: ['DIRECT'],
-          ),
-        ],
+
         rules: [
           'GEOIP,CN,DIRECT',
-          'MATCH,proxy'
+          'MATCH,DIRECT'
         ]
     );
     try {
@@ -198,46 +190,27 @@ class ClashService extends GetxController {
 
   Future<void> reloadClashCore() async {
     try {
+      // if(coreStatus.value == RunningState.stoped){
+      //   await updatePorts();
+      // }
+
       controllers.config.config.value.selected = Files.makeProxyConfig.path;
       if (coreStatus.value == RunningState.running) {
         controllers.global.updateMsg("切换配置...");
         await controllers.config.readClashCoreApi();
         controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
-        
-        // 在切换配置前确保配置文件有效
-        final configFile = File(path.join(Paths.config.path, controllers.config.config.value.selected));
-        if (!await configFile.exists()) {
-          throw Exception("配置文件不存在");
-        }
-        
-        // 确保配置文件包含必要的代理组
-        final configStr = await configFile.readAsString();
-        final yamlEditor = YamlEditor(configStr);
-        final configMap = yamlEditor.parseAt([]) as YamlMap;
-        
-        if (configMap['proxy-groups'] == null || 
-            (configMap['proxy-groups'] as YamlList).isEmpty) {
-          throw Exception("配置文件缺少代理组");
-        }
-        
-        await controllers.core.changeConfig(configFile.path);
+        await controllers.core.changeConfig(path.join(Paths.config.path, controllers.config.config.value.selected));
         controllers.global.updateMsg("fetchReloadConfig${controllers.config.clashCoreApiAddress.value}...");
       }
     } catch (e) {
-      LogHelper().e("切换配置失败: $e");
-      
-      // 如果切换失败,尝试回退到初始配置
-      try {
-        controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
-        await controllers.config.readClashCoreApi();
-        controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
-        await controllers.core.changeConfig(
-          path.join(Paths.config.path, controllers.config.config.value.selected)
-        );
-      } catch (fallbackError) {
-        LogHelper().e("回退到初始配置失败: $fallbackError");
-        throw Exception("配置切换失败");
-      }
+      // if(coreStatus.value == RunningState.stoped){
+      //   await updatePorts();
+      // }
+
+      controllers.global.updateMsg("重新配置...");
+      await controllers.config.readClashCoreApi();
+      controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
+      await controllers.core.changeConfig(path.join(Paths.config.path, controllers.config.config.value.selected));
     }
   }
 

+ 6 - 0
lib/app/controller/GlobalController.dart

@@ -236,6 +236,9 @@ class GlobalController extends GetxController {
       }
       
       nodeModes.value = await ApiService().getNode("/api/client/v4/nodes?vless=1");
+
+      await makeProxy();
+      await controllers.cc_service.reloadClashCore();
     } catch (e) {
       handleApiError(e);
     }
@@ -304,6 +307,9 @@ class GlobalController extends GetxController {
     routeModesSelect.value = open ? "tun": "sys";
     LogHelper().d("TunProxySwitch ----- 当前ROUTE模式${routeModesSelect.value}");
     tunProxySwitchIng.value = false;
+    // await TunProxySwitch(true);
+    await makeProxy();
+    await controllers.cc_service.reloadClashCore();
   }
 
   Future<void> systemProxySwitch(bool open) async {

+ 18 - 0
lib/app/controller/core.dart

@@ -200,6 +200,24 @@ class CoreController extends GetxController {
     }
   }
 
+
+  Future<void> restartConfig(String configPath) async{
+    try {
+      final body = json.encode({
+        "path": configPath
+      });
+      var ut = Uri.parse('http://$url/restart');
+      final res = await  client.put(ut,body:body,headers: headers);
+      print("changeConfig ---- ${res.statusCode}");
+      if(res.statusCode == 204)
+      {
+        await updateConfig();
+      }
+    } on http.ClientException catch (e) {
+    } on Exception catch (e) {
+    }
+  }
+
   // type updateConfigRequest struct {
   // 	Path    string `json:"path"`
   // 	Payload string `json:"payload"`

+ 0 - 4
lib/app/controller/service.dart

@@ -259,10 +259,6 @@ class ServiceController extends GetxController {
         controllers.global.updateMsg("fetchReloadConfig${controllers.config.clashCoreApiAddress.value}...");
       }
     } catch (e) {
-
-
-
-
       controllers.global.updateMsg("重新配置...");
       await controllers.config.readClashCoreApi();
       controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);

+ 13 - 11
lib/app/service/connection_service.dart

@@ -60,16 +60,16 @@ class ConnectionService {
           await serviceController.reloadClashCore();
           // await serviceController.fetchSetProxy();
         } else {
-          if (controllers.service.clashServiceIsRuning) {
-            await globalController.makeProxy();
-            await reloadClashCore();
-          }
+          // if (controllers.service.clashServiceIsRuning) {
+          //   await globalController.makeProxy();
+          //   await reloadClashCore();
+          // }
         }
       } else {
-        if (controllers.service.clashServiceIsRuning) {
-          await globalController.makeProxy();
-          await reloadClashCore();
-        }
+        // if (controllers.service.clashServiceIsRuning) {
+        //   await globalController.makeProxy();
+        //   await reloadClashCore();
+        // }
       }
 
       await globalController.updateNode();
@@ -122,6 +122,8 @@ class ConnectionService {
          await proxyManager.startProxy(port);
         }
 
+      } else {
+        controllers.global.systemProxySwitch(true);
       }
       updateStatus(ConnectionStatus.connected);
       globalController.updateMsg("连接成功");
@@ -133,9 +135,9 @@ class ConnectionService {
         if(!await proxyManager.isProxyEnabled()){
           globalController.updateMsg("当前没有设置系统代理,20秒后切换成网卡模式");
           await Future.delayed(Duration(seconds: 20)); // 等待核心状态更新
-          await globalController.TunProxySwitch(true);
-          await globalController.makeProxy();
-          await reloadClashCore();
+          // await globalController.TunProxySwitch(true);
+          // await globalController.makeProxy();
+          // await reloadClashCore();
           LogHelper().d("重置网卡模式");
         }