config.dart 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import 'dart:io';
  2. import 'dart:convert';
  3. import 'package:dio/dio.dart';
  4. import 'package:get/get.dart';
  5. import 'package:naiyouwl/app/bean/config.dart';
  6. import 'package:naiyouwl/app/const/const.dart';
  7. import 'package:yaml/yaml.dart';
  8. import 'package:path/path.dart' as path;
  9. import 'package:flutter_emoji/flutter_emoji.dart';
  10. import '../data/model/NodeMode.dart';
  11. final Map<String, dynamic> _defaultConfig = {
  12. 'selected': 'example.yaml',
  13. 'updateInterval': 86400,
  14. 'updateSubsAtStart': false,
  15. 'setSystemProxy': false,
  16. 'startAtLogin': false,
  17. 'breakConnections': false,
  18. 'language': 'zh_CN',
  19. 'port': 9899,
  20. 'subs': [],
  21. };
  22. class ConfigController extends GetxController {
  23. late final dio;
  24. var config = Config.fromJson(_defaultConfig).obs;
  25. var clashCoreApiAddress = '127.0.0.1:9090'.obs;
  26. var clashCoreApiSecret = ''.obs;
  27. var clashCoreDns = ''.obs;
  28. var clashCoreTunEnable = false.obs;
  29. var servicePort = 0.obs;
  30. Future<void> initConfig() async {
  31. // var port = await getFreePort();
  32. //dio.addSentry();
  33. dio = Dio(BaseOptions(baseUrl: clashCoreApiAddress.value));
  34. if (!await Paths.config.exists()) await Paths.config.create(recursive: true);
  35. if (!await Files.configCountryMmdb.exists()) await Files.assetsCountryMmdb.copy(Files.configCountryMmdb.path);
  36. if (Platform.isWindows && !await Files.configWintun.exists()) await Files.assetsWintun.copy(Files.configWintun.path);
  37. final locale = Get.deviceLocale!;
  38. _defaultConfig['language'] = '${locale.languageCode}_${locale.countryCode}';
  39. if (await Files.configConfig.exists()) {
  40. final local = json.decode(await Files.configConfig.readAsString());
  41. config.value = Config.fromJson({..._defaultConfig, ...local});
  42. } else {
  43. config.value = Config.fromJson(_defaultConfig);
  44. }
  45. //config.value.port = port;
  46. if (config.value.subs.isEmpty) {
  47. if (!await Files.configExample.exists()) {
  48. await Files.assetsExample.copy(Files.configExample.path);
  49. }
  50. config.value.subs.add(ConfigSub(name: 'example.yaml', url: '', updateTime: 0));
  51. config.value.selected = 'example.yaml';
  52. }
  53. await save();
  54. await readClashCoreApi();
  55. }
  56. String nodeToYaml(NodeMode node) {
  57. switch (node.type) {
  58. case 'trojan':
  59. return ''' - { name: ${node.name}, type: ${node.type}, server: ${node.host}, port: ${node.port}, password: ${node.passwd}, udp: 1 }''';
  60. case 'shadowsocks':
  61. return ''' - { name: ${node.name}, type: ss, server: ${node.host}, port: ${node.port}, password: ${node.passwd}, cipher: ${node.method}, udp: 1 }''';
  62. case 'v2ray':
  63. final type = (node.vless == 1) ? 'vless' : 'vmess';
  64. if (type == 'vless') {
  65. return ''' - { name: ${node.name}, type: $type, server: ${node.host}, port: ${node.port}, uuid: ${node.uuid}, alterId: ${node.v2AlterId}, udp: 1, flow: xtls-rprx-vision, servername: www.amazon.com, tls: true, reality-opts: { public-key: ${node.vlessPulkey} } }''';
  66. } else {
  67. return ''' - { name: ${node.name}, type: $type, server: ${node.host}, port: ${node.port}, uuid: ${node.uuid}, alterId: ${node.v2AlterId}, cipher: ${node.method}, udp: 1 }''';
  68. }
  69. default:
  70. return '';
  71. }
  72. }
  73. Future<void> makeClashConfig(List<NodeMode> nodes) async{
  74. Files.makeProxyConfig.deleteSync(recursive: true);
  75. var stack = "system";
  76. if( Platform.isWindows){
  77. stack = "gvisor";
  78. }
  79. var dnsPort = 1553;
  80. if(clashCoreTunEnable.value == true)
  81. {
  82. dnsPort = 53;
  83. }
  84. var mixedport = 9888;
  85. bool bg = await isPortOccupied(mixedport);
  86. if(bg) {
  87. mixedport = await getFreePort();
  88. }
  89. var extePort = 9777;
  90. final ac = await isPortOccupied(extePort);
  91. if(ac) {
  92. extePort = await getFreePort();
  93. }
  94. var proxies = nodes.map(nodeToYaml).toList();
  95. var proxyGroups = '''
  96. proxy-groups:
  97. - name: proxy
  98. type: select
  99. proxies:
  100. - ${nodes.map((node) => node.name).join('\n - ')}
  101. ''';
  102. var rules = '''
  103. rules:
  104. - GEOIP,CN,DIRECT
  105. - MATCH,proxy
  106. ''';
  107. var initconfig = '''
  108. mixed-port: $mixedport
  109. allow-lan: true
  110. bind-address: '*'
  111. mode: Rule
  112. log-level: info
  113. external-controller: '127.0.0.1:$extePort'
  114. unified-delay: false
  115. geodata-mode: true
  116. tcp-concurrent: false
  117. find-process-mode: strict
  118. global-client-fingerprint: chrome
  119. dns:
  120. nameserver:
  121. - 114.114.114.114
  122. - 119.29.29.29
  123. - https://doh.pub/dns-query
  124. - https://dns.alidns.com/dns-query
  125. fallback:
  126. - https://dns.cloudflare.com/dns-query
  127. - "[2001:da8::666]:53"
  128. - https://public.dns.iij.jp/dns-query
  129. - https://jp.tiar.app/dns-query
  130. - https://jp.tiarap.org/dns-query
  131. - tls://dot.tiar.app
  132. enable: true
  133. ipv6: false
  134. # enhanced-mode: redir-host
  135. enhanced-mode: fake-ip
  136. fake-ip-range: 198.18.0.1/16
  137. listen: 0.0.0.0:$dnsPort
  138. fake-ip-filter:
  139. - "*.lan"
  140. default-nameserver:
  141. - 114.114.114.114
  142. - 119.29.29.29
  143. - "[2001:da8::666]:53"
  144. tun:
  145. enable: ${clashCoreTunEnable.value}
  146. stack: $stack
  147. # stack: gvisor
  148. dns-hijack:
  149. - 198.18.0.2:53 # when `fake-ip-range` is 198.18.0.1/16, should hijack 198.18.0.2:53
  150. auto-route: true # auto set global route for Windows
  151. # It is recommended to use `interface-name`
  152. auto-detect-interface: true # auto detect interface, conflict with `interface-name`
  153. proxies:
  154. ${proxies.join('\n')}
  155. $proxyGroups
  156. $rules
  157. ''';
  158. await Files.makeProxyConfig.writeAsString(initconfig);
  159. config.value.selected = 'proxy.yaml';
  160. }
  161. Future<void> save() async {
  162. await Files.configConfig.writeAsString(json.encode(config.toJson()));
  163. }
  164. Future<void> readClashCoreApi() async {
  165. final configStr = await File(path.join(Paths.config.path, config.value.selected)).readAsString();
  166. // final emoji = EmojiParser();
  167. // final b = emoji.unemojify(_config);
  168. final configJson = loadYaml(configStr.replaceAll(EmojiParser.REGEX_EMOJI, 'emoji'));
  169. // print(_json["external-controller"]);
  170. // https://github.com/dart-lang/yaml/issues/53
  171. // final _extControl = RegExp(r'''(?<!#\s*)external-controller:\s+['"]?([^'"]+?)['"]?\s''').firstMatch(_config)?.group(1);
  172. // final _secret = RegExp(r'''(?<!#\s*)secret:\s+['"]?([^'"]+?)['"]?\s''').firstMatch(_config)?.group(1);
  173. clashCoreApiAddress.value = (configJson["external-controller"] ?? '127.0.0.1:9090').replaceAll('0.0.0.0', '127.0.0.1');
  174. clashCoreApiSecret.value = (configJson["secret"] ?? '');
  175. clashCoreTunEnable.value = configJson["tun"]?["enable"] == true;
  176. clashCoreDns.value = '';
  177. if (configJson["dns"]?["enable"] == true && (configJson["dns"]["listen"] ?? '').isNotEmpty) {
  178. final dns = (configJson["dns"]["listen"] as String).split(":");
  179. final ip = dns[0];
  180. final port = dns[1];
  181. if (port == '53') {
  182. clashCoreDns.value = ip == '0.0.0.0' ? '127.0.0.1' : ip;
  183. }
  184. }
  185. }
  186. Future<void> setLanguage(String language) async {
  187. config.value.language = language;
  188. await save();
  189. config.refresh();
  190. }
  191. Future<void> setSystemProxy(bool open) async {
  192. config.value.setSystemProxy = open;
  193. await save();
  194. config.refresh();
  195. }
  196. Future<void> setUpdateInterval(int value) async {
  197. config.value.updateInterval = value;
  198. await save();
  199. config.refresh();
  200. }
  201. Future<void> setUpdateSubsAtStart(bool value) async {
  202. config.value.updateSubsAtStart = value;
  203. await save();
  204. config.refresh();
  205. }
  206. Future<void> setSelectd(String selected) async {
  207. config.value.selected = selected;
  208. await save();
  209. config.refresh();
  210. }
  211. Future<bool> updateSub(ConfigSub sub) async {
  212. if ((sub.url ?? '').isEmpty) return false;
  213. final res = await dio.get(sub.url!);
  214. final subInfo = res.headers['subscription-userinfo'];
  215. final file = File(path.join(Paths.config.path, sub.name));
  216. final oldConfig = await file.exists() ? await file.readAsString() : '';
  217. final changed = oldConfig != res.data;
  218. if (changed) await file.writeAsString(res.data);
  219. sub.updateTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  220. sub.info = null;
  221. if (subInfo != null) {
  222. // final info = Map.fromEntries(
  223. // subInfo.first.split(RegExp(r';\s*')).where((s) => s.isNotEmpty).map((e) => e.split('=')).map((e) => MapEntry(e[0], int.parse(e[1]))));
  224. final entries = subInfo.first.split(RegExp(r';\s*'))
  225. .where((s) => s.isNotEmpty)
  226. .map((e) => e.split('='))
  227. .toList();
  228. final info = <String, dynamic>{};
  229. for (var entry in entries) {
  230. info[entry[0]] = int.parse(entry[1]);
  231. }
  232. sub.info = ConfigSubInfo.fromJson(info);
  233. }
  234. await setSub(sub.name, sub);
  235. return changed;
  236. }
  237. Future<void> setSub(String subName, ConfigSub sub) async {
  238. final idx = config.value.subs.indexWhere((it) => it.name == subName);
  239. config.value.subs[idx] = sub;
  240. if (subName != sub.name) {
  241. final file = File(path.join(Paths.config.path, subName));
  242. if (await file.exists()) await file.rename(path.join(Paths.config.path, sub.name));
  243. }
  244. await save();
  245. config.refresh();
  246. }
  247. Future<void> addSub(ConfigSub sub) async {
  248. config.value.subs.add(sub);
  249. final file = File(path.join(Paths.config.path, sub.name));
  250. if (!await file.exists()) await file.create();
  251. await save();
  252. config.refresh();
  253. }
  254. Future<void> deleteSub(String subName) async {
  255. final file = File(path.join(Paths.config.path, subName));
  256. if (await file.exists()) await file.delete();
  257. config.value.subs.removeWhere((it) => it.name == subName);
  258. await save();
  259. config.refresh();
  260. }
  261. Future<void> setBreakConnections(bool value) async {
  262. config.value.breakConnections = value;
  263. await save();
  264. config.refresh();
  265. }
  266. Future<int> getFreePort() async {
  267. var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
  268. int port = server.port;
  269. await server.close();
  270. return port;
  271. }
  272. Future<bool> isPortOccupied(int port) async {
  273. bool isOccupied = false;
  274. ServerSocket? server;
  275. try {
  276. server = await ServerSocket.bind(InternetAddress.loopbackIPv4, port);
  277. } catch (e) {
  278. isOccupied = true;
  279. } finally {
  280. await server?.close();
  281. }
  282. return isOccupied;
  283. }
  284. }