config.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import 'dart:io';
  2. import 'dart:convert';
  3. import 'package:get/get.dart';
  4. import 'package:naiyouwl/app/clash/mode/config.dart';
  5. import 'package:naiyouwl/app/const/const.dart';
  6. import 'package:path/path.dart' as path;
  7. import 'package:yaml/yaml.dart';
  8. import 'package:yaml_edit/yaml_edit.dart';
  9. import '../common/constants.dart';
  10. final Map<String, dynamic> _defaultConfig = {
  11. 'selected': 'example.yaml',
  12. 'updateInterval': 86400,
  13. 'updateSubsAtStart': false,
  14. 'setSystemProxy': false,
  15. 'startAtLogin': false,
  16. 'breakConnections': false,
  17. 'language': 'zh_CN',
  18. 'servicePort': 9899,
  19. 'dnsPort': ":1053",
  20. };
  21. final Map<String, dynamic> _defaultVersionConfig = {
  22. 'serviceVersion': '1.0.0',
  23. 'coreVersion': '1.18.8',
  24. 'appVersion': kDnsListenPort,
  25. };
  26. class ConfigController extends GetxController {
  27. var config = GuiConfig.fromJson(_defaultConfig).obs;
  28. var versionConfig = VersionConfig.fromJson(_defaultVersionConfig).obs;
  29. var clashCoreApiAddress = '127.0.0.1:9799'.obs;
  30. var clashCoreApiSecret = ''.obs;
  31. var clashCoreDns = ''.obs;
  32. var clashCoreTunEnable = false.obs;
  33. var mixedPort = 9788.obs;
  34. var apiAddressPort = 9799.obs;
  35. var servicePort = 9899.obs;
  36. var dnsPort = kDnsListenPort.obs;
  37. Future<void> initConfig() async {
  38. if (await Paths.config.exists()) {
  39. if (await Files.versionConfig.exists()) {
  40. final localVersionConfig = json.decode(await Files.versionConfig.readAsString());
  41. final localAppVersion = localVersionConfig['appVersion'];
  42. if (compareVersions(localAppVersion, kVersion) < 0) {
  43. await Paths.config.delete(recursive: true);
  44. await Paths.config.create(recursive: true);
  45. }
  46. } else {
  47. await Paths.config.delete(recursive: true);
  48. await Paths.config.create(recursive: true);
  49. }
  50. }
  51. // 创建用户配置目录
  52. if (!await Paths.config.exists()) {
  53. await Paths.config.create(recursive: true);
  54. }
  55. // 复制资源文件到配置目录
  56. await _copyAssetToConfig(Files.assetsCountryMmdb, Files.configCountryMmdb);
  57. await _copyAssetToConfig(Files.assetsGeoIP, Files.configGeoIP);
  58. await _copyAssetToConfig(Files.assetsGeosite, Files.configGeosite);
  59. if (Platform.isWindows) {
  60. await _copyAssetToConfig(Files.assetsWintun, Files.configWintun);
  61. }
  62. // 设置默认语言
  63. final locale = Get.deviceLocale!;
  64. _defaultConfig['language'] = '${locale.languageCode}_${locale.countryCode}';
  65. // 读取或创建配置文件
  66. if (await Files.configConfig.exists()) {
  67. final local = json.decode(await Files.configConfig.readAsString());
  68. config.value = GuiConfig.fromJson({..._defaultConfig, ...local});
  69. } else {
  70. config.value = GuiConfig.fromJson(_defaultConfig);
  71. }
  72. // 读取或创建版本配置文件
  73. if (await Files.versionConfig.exists()) {
  74. final local = json.decode(await Files.versionConfig.readAsString());
  75. versionConfig.value = VersionConfig.fromJson({..._defaultVersionConfig, ...local});
  76. } else {
  77. versionConfig.value = VersionConfig.fromJson(_defaultVersionConfig);
  78. }
  79. print(Files.configConfig.path);
  80. // 保存 GuiConfig 到 gui_config.json
  81. await saveGuiConfig();
  82. // 保存 VersionConfig 到 version_config.json
  83. await saveVersionConfig();
  84. // await makeInitConfig();
  85. }
  86. Future<void> saveConfig() async {
  87. // 保存 GuiConfig 到 gui_config.json
  88. await saveGuiConfig();
  89. // 保存 VersionConfig 到 version_config.json
  90. await saveVersionConfig();
  91. // await makeInitConfig();
  92. }
  93. // 比较两个版本号
  94. int compareVersions(String v1, String v2) {
  95. var parts1 = v1.split('.');
  96. var parts2 = v2.split('.');
  97. for (int i = 0; i < 3; i++) {
  98. int p1 = int.parse(parts1[i]);
  99. int p2 = int.parse(parts2[i]);
  100. if (p1 != p2) {
  101. return p1 - p2;
  102. }
  103. }
  104. return 0;
  105. }
  106. // 返回一个可修改的 GUI 配置对象
  107. GuiConfig getEditableGuiConfig() {
  108. return config.value;
  109. }
  110. // 更新 GUI 配置
  111. Future<void> updateGuiConfig(GuiConfig updatedConfig) async {
  112. config.value = updatedConfig;
  113. await saveGuiConfig();
  114. }
  115. // 返回一个可修改的版本配置对象
  116. VersionConfig getEditableVersionConfig() {
  117. return versionConfig.value;
  118. }
  119. // 更新版本配置
  120. Future<void> updateVersionConfig(VersionConfig updatedVersionConfig) async {
  121. versionConfig.value = updatedVersionConfig;
  122. await saveVersionConfig();
  123. }
  124. // 示例使用方法:
  125. // var editableConfig = getEditableGuiConfig();
  126. // editableConfig.language = 'en_US';
  127. // editableConfig.startAtLogin = true;
  128. // await updateGuiConfig(editableConfig);
  129. Future<void> saveGuiConfig() async {
  130. await Files.configConfig.writeAsString(json.encode(config.value.toJson()));
  131. }
  132. Future<void> saveVersionConfig() async {
  133. await Files.versionConfig.writeAsString(json.encode(versionConfig.value.toJson()));
  134. }
  135. Future<void> _copyAssetToConfig(File source, File destination) async {
  136. if (!await destination.exists()) {
  137. await source.copy(destination.path);
  138. }
  139. }
  140. Future<void> readClashCoreApi() async {
  141. final configStr = await File(path.join(Paths.config.path, config.value.selected)).readAsString();
  142. final yamlEditor = YamlEditor(configStr);
  143. final configMap = yamlEditor.parseAt([]) as YamlMap;
  144. clashCoreApiAddress.value = configMap['external-controller'] as String? ?? '127.0.0.1:9090';
  145. print("clash api address ${clashCoreApiAddress.value}");
  146. clashCoreApiSecret.value = (configMap['secret'] as String? ?? '');
  147. mixedPort.value = (configMap['mixed-port'] as int? ?? 9788);
  148. var tun = (configMap['tun'] as YamlMap?)?['enable'];
  149. clashCoreTunEnable.value = tun == null ? false : tun;
  150. clashCoreDns.value = '';
  151. var dnsEnable = (configMap['dns'] as YamlMap?)?['enable'];
  152. if(dnsEnable != null) {
  153. final dns = ((configMap['dns'] as YamlMap)['listen'] as String? ?? '').split(':');
  154. if (dns.length == 2) {
  155. final ip = dns[0];
  156. final port = dns[1];
  157. if (port == '53') {
  158. clashCoreDns.value = ip == '0.0.0.0' ? '127.0.0.1' : ip;
  159. }
  160. }
  161. }
  162. }
  163. // ... 其他方法保持不变
  164. }