const.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import 'dart:io';
  2. import 'package:naiyouwl/app/common/LogHelper.dart';
  3. import 'package:path/path.dart' as path;
  4. import 'package:process_run/shell_run.dart';
  5. enum OS { windows, linux, macos }
  6. enum Architecture { amd64, arm64 }
  7. class ClashName {
  8. static late final OS os;
  9. static late final Architecture architecture;
  10. static late final bool isRoot;
  11. static void init() {
  12. os = determineOS();
  13. architecture = determineArchitecture();
  14. isRoot = determineIsRoot();
  15. }
  16. static String get platform {
  17. if (Platform.isWindows) return 'windows';
  18. if (Platform.isMacOS) return 'darwin';
  19. if (Platform.isLinux) return 'linux';
  20. return 'unknown';
  21. }
  22. static OS determineOS() {
  23. if(Platform.isWindows) {
  24. return OS.windows;
  25. } else if (Platform.isLinux) {
  26. return OS.linux;
  27. } else if (Platform.isMacOS){
  28. return OS.macos;
  29. } else {
  30. LogHelper().e('Unsupported OS');
  31. throw Exception('Unsupported OS');
  32. }
  33. }
  34. static Architecture determineArchitecture() {
  35. if (os == OS.windows) {
  36. final arch = Platform.environment['PROCESSOR_ARCHITECTURE'];
  37. // https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details
  38. if (arch == 'AMD64') {
  39. return Architecture.amd64;
  40. } else if (arch == 'ARM64') {
  41. return Architecture.arm64;
  42. } else {
  43. LogHelper().e('Unsupported Architecture');
  44. throw Exception('Unsupported Architecture');
  45. }
  46. } else {
  47. final result = Process.runSync('uname', ['-m']);
  48. if (result.exitCode == 0) {
  49. final arch = result.stdout.toString().trim();
  50. if (arch == 'x86_64') {
  51. return Architecture.amd64;
  52. } else if (arch == 'aarch64' || arch == 'arm64') {
  53. return Architecture.arm64;
  54. } else {
  55. LogHelper().e('Unsupported Architecture');
  56. throw Exception('Unsupported Architecture');
  57. }
  58. } else {
  59. LogHelper().e('Unsupported Architecture');
  60. throw Exception('Unsupported Architecture');
  61. }
  62. }
  63. }
  64. static bool determineIsRoot() {
  65. if (os == OS.windows) {
  66. final result = Process.runSync('net', ['session']);
  67. if (result.exitCode == 0) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. } else {
  73. final result = Process.runSync('id', ['-u']);
  74. if (result.exitCode == 0) {
  75. return result.stdout.toString().trim() == '0';
  76. } else {
  77. return false;
  78. }
  79. }
  80. }
  81. static String get arch {
  82. return architecture.name; //amd64
  83. }
  84. static String get ext {
  85. if (Platform.isWindows) return '.exe';
  86. return '';
  87. }
  88. static String get name {
  89. return 'ccore-$platform-$arch$ext';
  90. }
  91. }
  92. class Paths {
  93. static Directory get assets {
  94. File mainFile = File(Platform.resolvedExecutable);
  95. String assetsPath = '../data/flutter_assets/assets';
  96. if (Platform.isMacOS) assetsPath = '../../Frameworks/App.framework/Resources/flutter_assets/assets';
  97. return Directory(path.normalize(path.join(mainFile.path, assetsPath)));
  98. }
  99. static Directory get assetsBin {
  100. return Directory(path.join(assets.path, 'bin'));
  101. }
  102. static Directory get assetsDep {
  103. return Directory(path.join(assets.path, 'dep'));
  104. }
  105. static Directory get config {
  106. return Directory(path.join(userHomePath, '.config', 'ccore'));
  107. }
  108. }
  109. class Files {
  110. static File get assetsClashCore {
  111. return File(path.join(Paths.assetsBin.path, ClashName.name));
  112. }
  113. static File get assetsClashService {
  114. if (Platform.isMacOS){
  115. return File(path.join(Paths.assetsBin.path, 'service-${ClashName.platform}'));
  116. }
  117. return File(path.join(Paths.assetsBin.path, 'service-${ClashName.platform}-${ClashName.arch}'));
  118. }
  119. static File get assetsCCore {
  120. if (Platform.isMacOS){
  121. return File(path.join(Paths.assetsBin.path, 'core-${ClashName.platform}'));
  122. }
  123. return File(path.join(Paths.assetsBin.path, 'core-${ClashName.platform}-${ClashName.arch}'));
  124. }
  125. static File get assetsSysProxyWin {
  126. return File(path.join(Paths.assetsBin.path, 'sysproxy64.exe'));
  127. }
  128. static File get assetsCountryMmdb {
  129. return File(path.join(Paths.assetsDep.path, 'Country.mmdb'));
  130. }
  131. static File get assetsGeosite {
  132. return File(path.join(Paths.assetsDep.path, 'geosite.dat'));
  133. }
  134. static File get assetsGeoIP {
  135. return File(path.join(Paths.assetsDep.path, 'geoip.dat'));
  136. }
  137. static File get assetsWintun {
  138. return File(path.join(Paths.assetsDep.path, 'wintun.dll'));
  139. }
  140. // static File get assetsExample {
  141. // return File(path.join(Paths.assetsDep.path, 'example.yaml'));
  142. // }
  143. static File get configConfig {
  144. return File(path.join(Paths.config.path, '.config.json'));
  145. }
  146. static File get makeProxyConfig {
  147. return File(path.join(Paths.config.path, 'proxy.yaml'));
  148. }
  149. static File get makeInitProxyConfig {
  150. return File(path.join(Paths.config.path, 'init_proxy.yaml'));
  151. }
  152. static File get versionConfig {
  153. return File(path.join(Paths.config.path, '.versions.json'));
  154. }
  155. static File get configCountryMmdb {
  156. return File(path.join(Paths.config.path, 'Country.mmdb'));
  157. }
  158. static File get configGeoIP {
  159. return File(path.join(Paths.config.path, 'geoip.dat'));
  160. }
  161. static File get configGeosite {
  162. return File(path.join(Paths.config.path, 'geosite.dat'));
  163. }
  164. static File get configWintun {
  165. return File(path.join(Paths.config.path, 'wintun.dll'));
  166. }
  167. static File get configExample {
  168. return File(path.join(Paths.config.path, 'example.yaml'));
  169. }
  170. }