alroyso 1 year ago
parent
commit
cc365a5571

+ 7 - 3
lib/app/controller/core.dart

@@ -80,7 +80,8 @@ class CoreController extends GetxController {
 
   Future<dynamic> fetchHello() async {
     try {
-      final res = await  client.get(Uri.http(url,'info'),headers: headers);
+      var ut = Uri.parse('http://$url/info');
+      final res = await  client.get(ut,headers: headers);
       if(res.statusCode == 200) {
         var jsonResponse =
         jsonDecode(res.body) as Map<String, dynamic>;
@@ -97,13 +98,15 @@ class CoreController extends GetxController {
     }
   }
 
-  Future<void> updateVersion() async {
+  Future<ClashCoreVersion?> updateVersion() async {
     try {
-      final res = await  client.get(Uri.http(url,'version'),headers: headers);
+      var ut = Uri.parse('http://$url/version');
+      final res = await  client.get(ut,headers: headers);
       if(res.statusCode == 200) {
         var jsonResponse =
         jsonDecode(res.body) as Map<String, dynamic>;
         version.value = ClashCoreVersion.fromJson(jsonResponse);
+        return version.value;
       }
 
     } on http.ClientException catch (e) {
@@ -115,6 +118,7 @@ class CoreController extends GetxController {
       print('Exception: $e');
       return null;
     }
+    return null;
   }
 
   Future<void> updateConfig() async {

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

@@ -108,6 +108,7 @@ class ServiceController extends GetxController {
       }
       serviceMode.value = data.mode == 'service-mode';
       controllers.global.updateMsg("服务模式");
+      await fetchStop();
     } catch (e) {
       await startUserModeService();
       controllers.global.updateMsg("开启用户服务");
@@ -134,6 +135,7 @@ class ServiceController extends GetxController {
       final isRun = await controllers.global.onIsProcessRunning(Files.assetsClashService.path);
       if(isRun == true){
         await isService();
+        await fetchStart();
         serviceStatus.value = RunningState.running;
         return;
       }
@@ -264,9 +266,11 @@ class ServiceController extends GetxController {
     } on http.ClientException catch (e) {
       // 处理客户端异常,例如没有网络连接
       print('Client Exception: ${e.message}');
+      coreStatus.value = RunningState.error;
     } on Exception catch (e) {
       // 处理其他类型的异常
       print('Exception: $e');
+      coreStatus.value = RunningState.error;
     }
 
   }
@@ -372,6 +376,7 @@ class ServiceController extends GetxController {
       try {
         var ut = Uri.http(url,'stop');
         await client.post(ut,headers:headers);
+        coreStatus.value = RunningState.stoped;
       } on http.ClientException catch (e) {
         // 处理客户端异常,例如没有网络连接
         print('Client Exception: ${e.message}');

+ 22 - 1
lib/app/modules/home/controllers/home_controller.dart

@@ -411,7 +411,7 @@ class HomeController extends GetxController {
     controllers.global.updateMsg("正在初始化内核");
     isInit.value = true;
     //controllers.protocol.initProtocol();
-    await controllers.config.portDetection();
+    // await controllers.config.portDetection();
     // init clash config
     // init config
     await controllers.config.initConfig();
@@ -425,6 +425,8 @@ class HomeController extends GetxController {
     if(Platform.isWindows){
       await controllers.service.initClashCoreConfig();
       if (controllers.service.coreStatus.value != RunningState.running) return;
+      await controllers.config.readClashCoreApi();
+      controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
       await controllers.core.updateVersion();
       final hello = await controllers.core.fetchHello();
       print(hello);
@@ -432,6 +434,25 @@ class HomeController extends GetxController {
       await controllers.service.startService();
       if (controllers.service.serviceStatus.value != RunningState.running) return;
 
+      //判断clash 是否启动在如果是启动,则不管了
+      final ret =  await controllers.service.fetchInfo();
+      if(ret != null){
+        if(ret.status != 'running'){
+          await controllers.service.fetchStartInit();
+          controllers.global.updateMsg("启动内核..");
+        } else {
+          await controllers.config.readClashCoreApi();
+          controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
+          final coreRet = await controllers.core.updateVersion();
+          if(coreRet != null){
+            print(coreRet);
+            controllers.service.coreStatus.value = RunningState.running;
+            controllers.global.updateMsg("内核已经启动..");
+          }
+        }
+      } else {
+        controllers.global.updateMsg("服务可能没有启动,..");
+      }
       if(Platform.isMacOS){
         if(!await controllers.global.onIsRunAdmin()){
           isServiceInstall.value = true;