|
@@ -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 {
|