alroyso 1 tahun lalu
induk
melakukan
06eec9a513

+ 4 - 4
lib/app/bean/config.dart

@@ -8,7 +8,7 @@ class Config {
     required this.breakConnections,
     required this.subs,
     required this.language,
-    required this.port
+    required this.servicePort
   });
   late String selected;
   late int updateInterval;
@@ -17,7 +17,7 @@ class Config {
   late bool startAtLogin;
   late bool breakConnections;
   late String language;
-  late int port;
+  late int servicePort;
   late List<ConfigSub> subs;
 
   Config.fromJson(Map<String, dynamic> json) {
@@ -28,7 +28,7 @@ class Config {
     startAtLogin = json['startAtLogin'];
     breakConnections = json['breakConnections'];
     language = json['language'];
-    port = json['port'];
+    servicePort = json['port'];
     subs = List.from(json['subs']).map((e) => ConfigSub.fromJson(e)).toList();
   }
 
@@ -41,7 +41,7 @@ class Config {
     data['startAtLogin'] = startAtLogin;
     data['breakConnections'] = breakConnections;
     data['language'] = language;
-    data['port'] = port;
+    data['port'] = servicePort;
     data['subs'] = subs.map((e) => e.toJson()).toList();
     return data;
   }

+ 13 - 1
lib/app/const/const.dart

@@ -57,7 +57,12 @@ class Files {
   static File get assetsCountryMmdb {
     return File(path.join(Paths.assetsDep.path, 'Country.mmdb'));
   }
-
+  static File get assetsGeosite {
+    return File(path.join(Paths.assetsDep.path, 'geosite.dat'));
+  }
+  static File get assetsGeoIP {
+    return File(path.join(Paths.assetsDep.path, 'geoip.dat'));
+  }
   static File get assetsWintun {
     return File(path.join(Paths.assetsDep.path, 'wintun.dll'));
   }
@@ -78,6 +83,13 @@ class Files {
     return File(path.join(Paths.config.path, 'Country.mmdb'));
   }
 
+  static File get configGeoIP {
+    return File(path.join(Paths.config.path, 'geoip.dat'));
+  }
+  static File get configGeosite {
+    return File(path.join(Paths.config.path, 'geosite.dat'));
+  }
+
   static File get configWintun {
     return File(path.join(Paths.config.path, 'wintun.dll'));
   }

+ 46 - 32
lib/app/controller/GlobalController.dart

@@ -53,25 +53,39 @@ class GlobalController extends GetxController {
 
     // init config
     await controllers.config.initConfig();
+    await controllers.service.initConfig();
+    await initService();
     // final language = controllers.config.config.value.language.split('_');
     //
     // await applyLanguage(Locale(language[0], language[1]));
 
-    await controllers.service.initConfig();
+   //  await controllers.service.initConfig();
+   //  // init service
+   //  await controllers.service.startService();
+   //  if (controllers.service.serviceStatus.value != RunningState.running) return;
+   // // await controllers.service.serviceModeSwitch(true);
+   //  // init clash core
+   //
+   //  await controllers.service.startClashCore();
+   //  if (controllers.service.coreStatus.value != RunningState.running) return;
+   //  await controllers.core.updateVersion();
+    // await controllers.global.updateDate();
+    //await controllers.service.install();
+    initRegularlyUpdate();
+  }
+
+  Future<void> initService() async {
+
     // init service
-    await controllers.service.install();
+    await controllers.service.startService();
     if (controllers.service.serviceStatus.value != RunningState.running) return;
-   // await controllers.service.serviceModeSwitch(true);
+    // await controllers.service.serviceModeSwitch(true);
     // init clash core
 
-    // await controllers.service.startClashCore();
-    // if (controllers.service.coreStatus.value != RunningState.running) return;
-    // await controllers.core.updateVersion();
-    // await controllers.pageProxie.updateDate();
-
-    initRegularlyUpdate();
+    await controllers.service.startClashCore();
+    if (controllers.service.coreStatus.value != RunningState.running) return;
+    await controllers.core.updateVersion();
   }
-
   Future<void> updateRoute(String route) async {
     if(allowStatusUpdate){
 
@@ -86,27 +100,27 @@ 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();
+    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);
-    //   }
-    // }
+    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);
+      }
+    }
 
   }
 
@@ -126,7 +140,7 @@ class GlobalController extends GetxController {
         handleSetProxieGroup(targetProxie, targetNode.name);
       }
     }
-    await updateDate();
+    //await updateDate();
   }
   Future<void> startTunMode() async {
     await makeProxy();
@@ -143,7 +157,7 @@ class GlobalController extends GetxController {
         handleSetProxieGroup(targetProxie, targetNode.name);
       }
     }
-    await updateDate();
+
   }
 
   Future<void> swift(String name) async {

+ 12 - 4
lib/app/controller/config.dart

@@ -37,7 +37,10 @@ class ConfigController extends GetxController {
     //dio.addSentry();
     dio = Dio(BaseOptions(baseUrl: clashCoreApiAddress.value));
     if (!await Paths.config.exists()) await Paths.config.create(recursive: true);
+
     if (!await Files.configCountryMmdb.exists()) await Files.assetsCountryMmdb.copy(Files.configCountryMmdb.path);
+    if (!await Files.configGeoIP.exists()) await Files.assetsGeoIP.copy(Files.configGeoIP.path);
+    if (!await Files.configGeosite.exists()) await Files.assetsGeosite.copy(Files.configGeosite.path);
     if (Platform.isWindows && !await Files.configWintun.exists()) await Files.assetsWintun.copy(Files.configWintun.path);
     final locale = Get.deviceLocale!;
     _defaultConfig['language'] = '${locale.languageCode}_${locale.countryCode}';
@@ -48,8 +51,10 @@ class ConfigController extends GetxController {
     } else {
       config.value = Config.fromJson(_defaultConfig);
     }
-    //config.value.port = port;
-
+    bool bg = await isPortOccupied(config.value.servicePort);
+    if(bg) {
+      config.value.servicePort = await getFreePort();
+    }
     if (config.value.subs.isEmpty) {
       if (!await Files.configExample.exists()) {
         await Files.assetsExample.copy(Files.configExample.path);
@@ -79,7 +84,10 @@ class ConfigController extends GetxController {
     }
   }
   Future<void> makeClashConfig(List<NodeMode> nodes) async{
-    Files.makeProxyConfig.deleteSync(recursive: true);
+    if( Files.makeProxyConfig.existsSync()){
+      Files.makeProxyConfig.deleteSync(recursive: true);
+    }
+
     var stack = "system";
     if( Platform.isWindows){
       stack = "gvisor";
@@ -171,7 +179,7 @@ $rules
 
     await Files.makeProxyConfig.writeAsString(initconfig);
     config.value.selected = 'proxy.yaml';
-
+   // await readClashCoreApi();
   }
 
   Future<void> save() async {

+ 8 - 10
lib/app/controller/service.dart

@@ -24,9 +24,8 @@ import 'package:web_socket_channel/io.dart';
 final headers = {"User-Agent": "ccore-for-flutter/0.0.1"};
 
 class ServiceController extends GetxController {
-  late final _dio ;
+  late final _dio = Dio(BaseOptions(baseUrl: 'http://127.0.0.1:9899', headers: headers));
   var serviceMode = false.obs;
-  var servicePort = 0.obs;
   var coreStatus = RunningState.stoped.obs;
   var serviceStatus = RunningState.stoped.obs;
 
@@ -41,14 +40,10 @@ class ServiceController extends GetxController {
 
   ServiceController(
 
-  ) {
-
-  }
+  );
 
   Future<void> initConfig() async{
-    servicePort.value = controllers.config.config.value.port;
-    print('http://127.0.0.1:${servicePort.value}');
-    _dio = Dio(BaseOptions(baseUrl: 'http://127.0.0.1:${servicePort.value}', headers: headers));
+    _dio.options.baseUrl = 'http://127.0.0.1:${controllers.config.config.value.servicePort}';
   }
 
   Future<void> startService() async {
@@ -148,7 +143,7 @@ class ServiceController extends GetxController {
     }
 
     IOWebSocketChannel fetchLogWs() {
-      return IOWebSocketChannel.connect(Uri.parse('ws://127.0.0.1:${servicePort.value}/logs'), headers: headers);
+      return IOWebSocketChannel.connect(Uri.parse('ws://127.0.0.1:${controllers.config.config.value.servicePort}/logs'), headers: headers);
     }
 
     Future<void> fetchStart(String name) async {
@@ -169,7 +164,7 @@ class ServiceController extends GetxController {
 
     Future<void> install() async {
       final res = await runAsAdmin(Files.assetsClashService.path, ["stop", "uninstall", "install", "start"]);
-
+      await initConfig();
       log.debug('install', res.stdout, res.stderr);
       if (res.exitCode != 0) throw res.stderr;
       await waitServiceStart();
@@ -185,7 +180,10 @@ class ServiceController extends GetxController {
     Future<void> serviceModeSwitch(bool open) async {
       if (serviceStatus.value == RunningState.running) await stopService();
       try {
+
         open ? await install() : await uninstall();
+        var data = _dio.post("/info");
+        print(data);
       } catch (e) {
         BotToast.showText(text: e.toString());
       }

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

@@ -88,24 +88,38 @@ class HomeController extends GetxController {
       controllers.global.allowStatusUpdate = false;
       // // 停止服务
       // await controllers.service.stopClashCore();
-      controllers.config.clashCoreTunEnable.value = false;
-      controllers.service.reloadClashCore();
+      var routeModes = controllers.global.routeModesSelect.value;
+      if(routeModes != "tun"){
+        controllers.config.clashCoreTunEnable.value = false;
+        controllers.service.reloadClashCore();
+        await controllers.global.systemProxySwitch(false);
+        //await controllers.global.updateDate();
+      }
 
-      await controllers.global.systemProxySwitch(false);
-      await controllers.global.updateDate();
       return;
     } else {
       var routeModes = controllers.global.routeModesSelect.value;
       if(routeModes == "tun"){
-        controllers.global.startTunMode();
+        // final coreStatus = controllers.service.coreStatus.value;
+        // if(coreStatus != RunningState.running){
+        //   controllers.service.serviceModeSwitch(true);
+        // }
         controllers.config.clashCoreTunEnable.value = true;
-        controllers.service.serviceModeSwitch(true);
+        controllers.global.startTunMode();
+        controllers.service.stopClashCore();
+
         //controllers.service.reloadClashCore();
 
       } else {
-        controllers.global.startSysMode();
         controllers.config.clashCoreTunEnable.value = false;
-        controllers.service.reloadClashCore();
+        controllers.global.startSysMode();
+        final coreStatus = controllers.service.coreStatus.value;
+        if (coreStatus == RunningState.running){
+          controllers.service.reloadClashCore();
+        }
+        else{
+          controllers.service.startClashCore();
+        }
       }
       controllers.global.allowStatusUpdate = true;
       updateStatus(ConnectionStatus.connecting);
@@ -139,6 +153,8 @@ class HomeController extends GetxController {
       isLoading.value = true;
       userMode.value = await ApiService().userinfo("/api/client/v4/userinfo");
       await globalController.fetchNodes();
+      //await controllers.global.makeProxy();
+      //await controllers.global.initService();
     // //生成配置
     //   await controllers.global.makeProxy();
     //   // 开始重启服务
@@ -187,7 +203,7 @@ class HomeController extends GetxController {
     fetchSysConfig();
     fetchLocalUser();
     fetchUserinfo();
-
+    //controllers.global.initService();
   }
 
   @override

+ 2 - 1
lib/app/utils/shell.dart

@@ -17,7 +17,7 @@ Future<void> killProcess(String name) async {
 
 Future<ProcessResult> runAsAdmin(String executable, List<String> arguments) async {
   String executablePath = shellArgument(executable).replaceAll(' ', r'\\ ');
-  executablePath = executablePath.substring(1, executablePath.length);
+  //executablePath = executablePath.substring(1, executablePath.length - 1);
   if (Platform.isMacOS) {
     return await Process.run(
       'osascript',
@@ -27,6 +27,7 @@ Future<ProcessResult> runAsAdmin(String executable, List<String> arguments) asyn
       ],
     );
   } else if (Platform.isWindows) {
+
     return await Process.run(
       path.join(Paths.assetsBin.path, "run-as-admin.bat"),
       [executable, ...arguments],