alroyso 1 рік тому
батько
коміт
3df0f979b6

+ 3 - 1
README.md

@@ -25,4 +25,6 @@ flutter build macos --dart-define OS_ARCH=arm64
 
 flutter build windows --dart-define OS_ARCH=amd64
 appdmg ./config.json ./MAC加速器-amd64.dmg
-appdmg ./config.json ./MAC加速器-arm64.dmg
+appdmg ./config.json ./MAC加速器-arm64.dmg
+
+appdmg ./config.json ./MAC加速器通用.dmg

+ 75 - 2
lib/app/const/const.dart

@@ -1,8 +1,18 @@
 import 'dart:io';
+import 'package:naiyouwl/app/common/LogHelper.dart';
 import 'package:path/path.dart' as path;
 import 'package:process_run/shell_run.dart';
-
+enum OS { windows, linux, macos }
+enum Architecture { x86_64, arm64 }
 class ClashName {
+  static late final OS os;
+  static late final Architecture architecture;
+  static late final bool isRoot;
+  static void init() {
+    os = determineOS();
+    architecture = determineArchitecture();
+    isRoot = determineIsRoot();
+  }
   static String get platform {
     if (Platform.isWindows) return 'windows';
     if (Platform.isMacOS) return 'darwin';
@@ -10,8 +20,71 @@ class ClashName {
     return 'unknown';
   }
 
+  static OS determineOS() {
+    if(Platform.isWindows) {
+      return OS.windows;
+    } else if (Platform.isLinux) {
+      return OS.linux;
+    } else if (Platform.isMacOS){
+      return OS.macos;
+    } else {
+      LogHelper().e('Unsupported OS');
+      throw Exception('Unsupported OS');
+    }
+  }
+
+  static Architecture determineArchitecture() {
+    if (os == OS.windows) {
+      final arch = Platform.environment['PROCESSOR_ARCHITECTURE'];
+      // https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details
+      if (arch == 'AMD64') {
+        return Architecture.x86_64;
+      } else if (arch == 'ARM64') {
+        return Architecture.arm64;
+      } else {
+        LogHelper().e('Unsupported Architecture');
+        throw Exception('Unsupported Architecture');
+      }
+    } else {
+      final result = Process.runSync('uname', ['-m']);
+      if (result.exitCode == 0) {
+        final arch = result.stdout.toString().trim();
+        if (arch == 'x86_64') {
+          return Architecture.x86_64;
+        } else if (arch == 'aarch64' || arch == 'arm64') {
+          return Architecture.arm64;
+        } else {
+          LogHelper().e('Unsupported Architecture');
+          throw Exception('Unsupported Architecture');
+        }
+      } else {
+        LogHelper().e('Unsupported Architecture');
+        throw Exception('Unsupported Architecture');
+      }
+    }
+  }
+
+  static bool determineIsRoot() {
+    if (os == OS.windows) {
+      final result = Process.runSync('net', ['session']);
+      if (result.exitCode == 0) {
+        return true;
+      } else {
+        return false;
+      }
+    } else {
+      final result = Process.runSync('id', ['-u']);
+      if (result.exitCode == 0) {
+        return result.stdout.toString().trim() == '0';
+      } else {
+        return false;
+      }
+    }
+  }
+
+
   static String get arch {
-    return const String.fromEnvironment('OS_ARCH', defaultValue: 'arm64'); //amd64
+    return architecture.name; //amd64
   }
 
   static String get ext {

+ 2 - 1
lib/app/controller/GlobalController.dart

@@ -79,13 +79,14 @@ class GlobalController extends GetxController {
       try {
         platformVersion =
             await _wlBaseHelpPlugin.getPlatformVersion() ?? 'Unknown platform version';
+        platformVersion = platformVersion + ClashName.architecture.name;
       } on PlatformException {
         platformVersion = 'Failed to get platform version.';
       }
       sysInfo.value = platformVersion;
     }
     else {
-      sysInfo.value = '';
+      sysInfo.value = ClashName.os.name + "-" + ClashName.architecture.name;
     }
 
   }

+ 7 - 7
lib/app/controller/config.dart

@@ -356,13 +356,13 @@ $rules
     }
     //await Future.delayed(const Duration(seconds: 5));  // 等待5秒
 
-    if(!controllers.service.isRunning){
-      isOk = await isPortOccupied(servicePort.value);
-      if(isOk){
-        servicePort.value = await getFreePort();
-        setSerivcePort(servicePort.value);
-      }
-    }
+    // if(!controllers.service.isRunning){
+    //   isOk = await isPortOccupied(servicePort.value);
+    //   if(isOk){
+    //     servicePort.value = await getFreePort();
+    //     setSerivcePort(servicePort.value);
+    //   }
+    // }
 
   }
 

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

@@ -299,13 +299,17 @@ class ServiceController extends GetxController {
 
     Future<void> fetchSetProxy() async {
       try {
+        var dns_ip = "";
+        if(controllers.global.routeModesSelect.value == "tun"){
+          dns_ip = "198.18.0.2";
+        }
         var ut = Uri.http(url,'on');
         final body = json.encode({
           "http_port" : controllers.config.mixedPort.value,
           "https_port" : controllers.config.mixedPort.value,
           "socks_port" :controllers.config.mixedPort.value,
           "bypass": "127.0.0.1, 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, 100.64.0.0/10, 17.0.0.0/8, localhost, *.local, *.crashlytics.com, seed-sequoia.siri.apple.com, sequoia.apple.com,xd.apple.com",
-          "dns_ip": "",
+          "dns_ip": dns_ip,
           "dns_type":"",
           "address":"127.0.0.1"
         });
@@ -318,14 +322,42 @@ class ServiceController extends GetxController {
         print('Exception: $e');
       }
     }
-
+  Future<void> fetchSetDnsProxy() async {
+    try {
+      var dns_ip = "";
+      if(controllers.global.routeModesSelect.value == "tun"){
+        dns_ip = "198.18.0.2";
+      }
+      var ut = Uri.http(url,'on');
+      final body = json.encode({
+        "http_port" : 0,
+        "https_port" : 0,
+        "socks_port" : 0,
+        "bypass": "",
+        "dns_ip": dns_ip,
+        "dns_type":"",
+        "address":"127.0.0.1"
+      });
+      await client.post(ut,body: body,headers:  headers);
+    } on http.ClientException catch (e) {
+      // 处理客户端异常,例如没有网络连接
+      print('Client Exception: ${e.message}');
+    } on Exception catch (e) {
+      // 处理其他类型的异常
+      print('Exception: $e');
+    }
+  }
   Future<void> fetchSetProxyStop() async {
 
     try {
+      var dnsIp = "";
+      if(controllers.global.routeModesSelect.value == "tun"){
+        dnsIp = "223.5.5.5";
+      }
       var ut = Uri.http(url,'off');
       final body = json.encode({
         "bypass": "",
-        "dns_ip":""
+        "dns_ip": dnsIp
       });
       await client.post(ut,body:body,headers:  headers);
     } on http.ClientException catch (e) {
@@ -374,13 +406,8 @@ class ServiceController extends GetxController {
       } catch (e) {
         BotToast.showText(text: e.toString());
       }
-      if(open){
-        await startService();
-        //await startClashCore();
-      }else{
-        serviceMode.value = false;
-      }
-
+      await startService();
+      await fetchStartInit();
     }
   Future<bool> isPortAvailable(int port) async {
     try {

+ 2 - 1
lib/app/controller/tray.dart

@@ -102,7 +102,8 @@ class TrayController extends GetxController with TrayListener {
             MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy),
           ])),
       MenuItem.separator(),
-     // MenuItem(label: 'tray_about'.tr, onClick: handleClickAbout),
+
+      MenuItem(label: '服务端口:${controllers.config.servicePort.value}', disabled: true,),
 
 
       isSHow == true ?  MenuItem(label: "显示控制台", onClick: handleClickConsoleShow,checked: isShowConsole.value)

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

@@ -136,6 +136,8 @@ class HomeController extends GetxController {
               controllers.global.updateMsg("需要开启服务,才能使用...");
               return;
             }
+
+
           }
 
           //await controllers.service.serviceModeSwitch(true);
@@ -186,6 +188,10 @@ class HomeController extends GetxController {
         await controllers.tray.updateTray();
 
         if(routeModes == "tun"){
+          if(Platform.isMacOS){
+            await controllers.service.fetchSetDnsProxy();
+          }
+
           controllers.global.updateMsg("启动tun模式完成...");
         }
         else{
@@ -423,7 +429,7 @@ class HomeController extends GetxController {
       final hello = await controllers.core.fetchHello();
       print(hello);
     } else {
-      await controllers.service.startUserModeService();
+      await controllers.service.startService();
       if (controllers.service.serviceStatus.value != RunningState.running) return;
 
       if(Platform.isMacOS){

+ 21 - 26
lib/app/modules/home/views/home_view.dart

@@ -217,31 +217,26 @@ class HomeView extends GetView<HomeController> {
                     ],
                   ),
                 ),
-               controllers.service.serviceMode.value == true ? Padding(
-                  padding: const EdgeInsets.fromLTRB(60, 10, 50, 0),
-                  child: Row(
-                    mainAxisAlignment: MainAxisAlignment.center,
-                    children: [
-                      ElevatedButton(onPressed: () async {
-                        await controllers.service.serviceModeSwitch(false);
-                      }, child: const Text("卸载服务")),
-                    ],
-
-                  ),
-                ) : Container(),
-              controller.isServiceInstall.value == true ? Padding(
-                  padding: const EdgeInsets.fromLTRB(60, 10, 50, 0),
-                  child: Row(
-                    mainAxisAlignment: MainAxisAlignment.center,
-                    children: [
-                      ElevatedButton(onPressed: () async {
-                        await controllers.service.serviceModeSwitch(true);
-                        controller.isServiceInstall.value = false;
-                      }, child: const Text("安装服务")),
-                    ],
+               // Padding(
+               //    padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
+               //    child: Row(
+               //      mainAxisAlignment: MainAxisAlignment.center,
+               //      children: [
+               //        // ElevatedButton(onPressed: () async {
+               //        //   await controllers.service.serviceModeSwitch(true);
+               //        //   controller.isServiceInstall.value = false;
+               //        // }, child: const Text("安装服务")),
+               //        const SizedBox(width: 10,),
+               //        Text("服务端口${ controller.getSerivce() }"),
+               //        const SizedBox(width: 10,),
+               //        // ElevatedButton(onPressed: () async {
+               //        //   await controllers.service.serviceModeSwitch(false);
+               //        // }, child: const Text("卸载服务")),
+               //      ],
+               //
+               //    ),
+               //  ),
 
-                  ),
-                ) : Container(),
                 // const SizedBox(height: 20,),
                 // Align(
                 //   alignment: Alignment.center,
@@ -249,8 +244,8 @@ class HomeView extends GetView<HomeController> {
                 // )
                 Text("版本号:$kVersion"),
 
-                const SizedBox(height: 10,),
-                Text("服务端口${ controller.getSerivce() }"),
+                const SizedBox(height: 2,),
+
                 controllers.global.sysInfo.value.isEmpty ? Container() : Text("当前:${controllers.global.sysInfo.value}"),
               ],
             );

+ 1 - 1
lib/main.dart

@@ -47,7 +47,7 @@ void main() async {
   }));
 
 
-
+  ClashName.init();
 
   Get.put(TrayController());
   Get.put(WindowController());