alroyso 1 month ago
parent
commit
3fb4193855

+ 3 - 2
lib/app/clash/service/clash_service.dart

@@ -166,8 +166,9 @@ class ClashService extends GetxController {
   }
 
   Future<bool> initClashCoreConfig() async {
-    controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
-    await makeInitConfig();
+    controllers.config.config.value.selected = Files.makeProxyConfig.path;
+    //await makeInitConfig();
+
     await startClashCore();
     if (coreStatus.value == RunningState.error) {
       controllers.global.updateMsg("启动内核失败...");

+ 25 - 48
lib/app/modules/home/controllers/home_controller.dart

@@ -155,40 +155,17 @@ class HomeController extends GetxController {
 
       updateStatus(ConnectionStatus.connecting);
 
-      // if(Platform.isMacOS){
-      //   // 检查服务是否运行
-      //   if (!controllers.cc_service.serviceIsRuning) {
-      //     // 尝试获取 hello 的返回结果并处理
-      //     if(controllers.service.coreStatus.value != RunningState.running)
-      //     {
-      //       var hellow = await controllers.core.fetchHello();
-      //
-      //       // 如果获取不到 hellow,则重试启动内核
-      //       if (hellow == null) {
-      //         controllers.global.updateMsg("尝试重新启动内核");
-      //         bool success = await restartClashCoreWithRetry(maxRetries: 3);  // 重试 3 次
-      //         if (!success) {
-      //           controllers.global.updateMsg("内核重启失败,已达到最大重试次数");
-      //           updateStatus(ConnectionStatus.stopped);
-      //           return;  // 如果重启失败,直接返回,阻止继续进行
-      //         }
-      //       }
-      //     }
-      //   }
-      // }
-
-
      // await Future.delayed(Duration(seconds: 3)); // 等待核心状态更新
       if(globalController.selectedNode.value != null)
       {
-        await connectionService.startConnection();
+        await connectionService.startConnectionWin();
       } else {
         // 继续后面的逻辑
         var error = await networkService.fetchAuthUser();
 
         // 根据 error 的结果继续处理
         if (error == "") {
-          await connectionService.startConnection();
+          await connectionService.startConnectionWin();
         } else {
           connectionService.updateStatus(ConnectionStatus.disconnected);
           globalController.updateMsg("验证失败,{$error}");
@@ -199,28 +176,28 @@ class HomeController extends GetxController {
     }
   }
 
-  // 重试机制,确保内核重启
-  Future<bool> restartClashCoreWithRetry({int maxRetries = 3}) async {
-    int attempts = 0;
-    bool success = false;
-
-    while (attempts < maxRetries && !success) {
-      attempts++;
-      controllers.global.updateMsg("正在尝试第 $attempts 次重启内核...");
-
-      await controllers.service.stopClashCore();
-      success = await controllers.service.initClashCoreConfig();
-
-      if (success) {
-        controllers.global.updateMsg("第 $attempts 次重启内核成功!");
-      } else {
-        controllers.global.updateMsg("第 $attempts 次重启内核失败...");
-        await Future.delayed(Duration(seconds: 2));  // 等待2秒再重试
-      }
-    }
-
-    return success;
-  }
+  // // 重试机制,确保内核重启
+  // Future<bool> restartClashCoreWithRetry({int maxRetries = 3}) async {
+  //   int attempts = 0;
+  //   bool success = false;
+  //
+  //   while (attempts < maxRetries && !success) {
+  //     attempts++;
+  //     controllers.global.updateMsg("正在尝试第 $attempts 次重启内核...");
+  //
+  //     await controllers.service.stopClashCore();
+  //     success = await controllers.service.initClashCoreConfig();
+  //
+  //     if (success) {
+  //       controllers.global.updateMsg("第 $attempts 次重启内核成功!");
+  //     } else {
+  //       controllers.global.updateMsg("第 $attempts 次重启内核失败...");
+  //       await Future.delayed(Duration(seconds: 2));  // 等待2秒再重试
+  //     }
+  //   }
+  //
+  //   return success;
+  // }
 
   void init(){
     Future.delayed(Duration.zero, () async {
@@ -245,7 +222,7 @@ class HomeController extends GetxController {
       nodeModes =  controllers.global.nodeModes;
       isLoading.value = false;
       await Future.delayed(Duration(seconds: 2)); // 等待核心状态更新
-      await connectionService.coreInit();
+      //await connectionService.coreInit();
     });
   }
   @override

+ 86 - 82
lib/app/service/connection_service.dart

@@ -23,8 +23,38 @@ class ConnectionService {
   ConnectionService(this.globalController, this.updateStatus);
 
 
-  Future<void> startConnection() async {
+  Future<void> startConnectionWin() async {
     try {
+      updateStatus(ConnectionStatus.connecting);
+      globalController.updateMsg("正在启动连接...");
+      await globalController.updateNode();
+      // 等待连接建立
+      await Future.delayed(Duration(seconds: 2));
+      updateStatus(ConnectionStatus.connected);
+      globalController.connectStatus.value = true;
+      globalController.updateMsg("连接成功");
+
+      final proxyManager = WlBaseHelp();
+
+      if(Platform.isWindows){
+        int? port = controllers.config.mixedPort.value;
+        if (port == 0) {
+          port = null;
+        }
+        int? socksPort = controllers.config.mixedPort.value;
+        if (socksPort == 0) {
+          socksPort = null;
+        }
+        int? mixedPort = controllers.config.mixedPort.value;
+        if (mixedPort == 0) {
+          mixedPort = null;
+        }
+
+        if(port !=null){
+          await proxyManager.startProxy(port);
+        }
+
+      }
 
       // // 检查所有内核是否已停止
       // bool allCoresStopped = await globalController.checkAllCoresStopped();
@@ -51,98 +81,72 @@ class ConnectionService {
       //     globalController.updateMsg("端口已更新,正在重新启动...");
       //   }
       // }
-      updateStatus(ConnectionStatus.connecting);
-      globalController.updateMsg("正在启动连接...");
-
-      if(Platform.isMacOS){
-        if (serviceController.serviceIsRuning) {
-          await globalController.makeProxy();
-          await serviceController.reloadClashCore();
-          // await serviceController.fetchSetProxy();
-        } else {
-          // if (controllers.service.clashServiceIsRuning) {
-          //   await globalController.makeProxy();
-          //   await reloadClashCore();
-          // }
-        }
-      } else {
-        // if (controllers.service.clashServiceIsRuning) {
-        //   await globalController.makeProxy();
-        //   await reloadClashCore();
-        // }
-      }
-
-      await globalController.updateNode();
 
 
-      // 等待连接建立
-      await Future.delayed(Duration(seconds: 2));
-
-      if(Platform.isMacOS){
-        if (serviceController.serviceIsRuning) {
-          //await serviceController.fetchStartInit();
-          await serviceController.fetchSetProxy();
-          await globalController.openProxy();
-        } else {
-          if (controllers.service.clashServiceIsRuning) {
-            globalController.connectStatus.value = true;
-
-          } else {
-            throw Exception("内核启动失败");
-          }
-        }
-      } else {
-        if (controllers.service.clashServiceIsRuning) {
-          updateStatus(ConnectionStatus.connected);
-          globalController.connectStatus.value = true;
-          globalController.updateMsg("连接成功");
+      // if(Platform.isMacOS){
+      //   if (serviceController.serviceIsRuning) {
+      //     await globalController.makeProxy();
+      //     await serviceController.reloadClashCore();
+      //     // await serviceController.fetchSetProxy();
+      //   } else {
+      //     // if (controllers.service.clashServiceIsRuning) {
+      //     //   await globalController.makeProxy();
+      //     //   await reloadClashCore();
+      //     // }
+      //   }
+      // } else {
+      //   // if (controllers.service.clashServiceIsRuning) {
+      //   //   await globalController.makeProxy();
+      //   //   await reloadClashCore();
+      //   // }
+      // }
 
-        } else {
-          throw Exception("内核启动失败");
-        }
-      }
 
-      final proxyManager = WlBaseHelp();
 
-      if(Platform.isWindows){
-        int? port = controllers.config.mixedPort.value;
-        if (port == 0) {
-          port = null;
-        }
-        int? socksPort = controllers.config.mixedPort.value;
-        if (socksPort == 0) {
-          socksPort = null;
-        }
-        int? mixedPort = controllers.config.mixedPort.value;
-        if (mixedPort == 0) {
-          mixedPort = null;
-        }
 
-        if(port !=null){
-         await proxyManager.startProxy(port);
-        }
 
-      } else {
-        controllers.global.systemProxySwitch(true);
-      }
-      updateStatus(ConnectionStatus.connected);
-      globalController.updateMsg("连接成功");
-      controllers.global.connectStatus.value = true;
 
-      //检测系统代理并切换成网卡模式
-      if(Platform.isWindows){
+      // if(Platform.isMacOS){
+      //   if (serviceController.serviceIsRuning) {
+      //     //await serviceController.fetchStartInit();
+      //     await serviceController.fetchSetProxy();
+      //     await globalController.openProxy();
+      //   } else {
+      //     if (controllers.service.clashServiceIsRuning) {
+      //       globalController.connectStatus.value = true;
+      //
+      //     } else {
+      //       throw Exception("内核启动失败");
+      //     }
+      //   }
+      // } else {
+      //   if (controllers.service.clashServiceIsRuning) {
 
-        if(!await proxyManager.isProxyEnabled()){
-          globalController.updateMsg("当前没有设置系统代理,20秒后切换成网卡模式");
-          await Future.delayed(Duration(seconds: 20)); // 等待核心状态更新
-          // await globalController.TunProxySwitch(true);
-          // await globalController.makeProxy();
-          // await reloadClashCore();
-          LogHelper().d("重置网卡模式");
-        }
+      //
+      //   } else {
+      //     throw Exception("内核启动失败");
+      //   }
+      // }
 
 
-      }
+      // updateStatus(ConnectionStatus.connected);
+      // globalController.updateMsg("连接成功");
+      // controllers.global.connectStatus.value = true;
+      //
+      // //检测系统代理并切换成网卡模式
+      // if(Platform.isWindows){
+      //
+      //   if(!await proxyManager.isProxyEnabled()){
+      //     globalController.updateMsg("当前没有设置系统代理,20秒后切换成网卡模式");
+      //     await Future.delayed(Duration(seconds: 20)); // 等待核心状态更新
+      //     // await globalController.TunProxySwitch(true);
+      //     // await globalController.makeProxy();
+      //     // await reloadClashCore();
+      //     LogHelper().d("重置网卡模式");
+      //   }
+      //
+      //
+      // }
 
 
     } catch (e) {