clash_service.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:async';
  4. import 'package:get/get.dart';
  5. import 'package:yaml/yaml.dart';
  6. import 'package:yaml_edit/yaml_edit.dart';
  7. import 'package:path/path.dart' as path;
  8. import '../../common/LogHelper.dart';
  9. import '../../common/constants.dart';
  10. import '../../const/const.dart';
  11. import '../../controller/controllers.dart';
  12. import '../../data/model/NodeMode.dart';
  13. import '../../utils/shell.dart';
  14. import '../../utils/utils.dart';
  15. import '../mode/clash_config.dart';
  16. class ClashService extends GetxController {
  17. Process? clashCoreProcess;
  18. final coreStatus = RunningState.stoped.obs;
  19. final serviceMode = false.obs;
  20. bool get clashServiceIsRuning => coreStatus.value == RunningState.running;
  21. Future<void> updatePorts() async {
  22. // 检查端口占用
  23. int newPort = await findAvailablePort(controllers.config.mixedPort.value+1);
  24. controllers.config.mixedPort.value = newPort;
  25. controllers.global.updateMsg("混合端口已更新为: $newPort");
  26. // 检查API端口占用
  27. int newApiPort = await findAvailablePort(controllers.config.apiAddressPort.value);
  28. controllers.config.apiAddressPort.value = newApiPort;
  29. controllers.global.updateMsg("API端口已更新为: $newApiPort");
  30. int newDnsPort = await findAvailablePort(int.parse(controllers.config.dnsPort.value.split(':').last));
  31. controllers.config.dnsPort.value = '${controllers.config.dnsPort.value.split(':').first}:$newDnsPort';
  32. await controllers.config.saveConfig();
  33. //await controllers.config.readClashCoreApi();
  34. }
  35. Future<void> makeInitConfig() async {
  36. //await updatePorts();
  37. //await controllers.config.readClashCoreApi();
  38. var mode = controllers.global.modesSelect.value;
  39. var clashConfig = ClashConfig(
  40. mixedPort: controllers.config.mixedPort.value,
  41. allowLan: true,
  42. bindAddress: '*',
  43. mode: mode,
  44. logLevel: 'debug',
  45. externalController: '127.0.0.1:${controllers.config.apiAddressPort.value}',
  46. unifiedDelay: false,
  47. geodataMode: true,
  48. tcpConcurrent: false,
  49. findProcessMode: 'strict',
  50. globalClientFingerprint: 'chrome',
  51. dns: DNS(
  52. enable: false,
  53. listen: controllers.config.dnsPort.value,
  54. ipv6: false,
  55. enhancedMode: kRedirHostMode,
  56. fakeIpFilter: null,
  57. nameserver: kDomesticDNS,
  58. proxyServerNameserver: kDomesticDNS,
  59. nameserverPolicy: {
  60. 'geosite:cn,private': kDomesticDNS,
  61. 'geosite:geolocation-!cn': kForeignDNS,
  62. },
  63. ),
  64. tun: Tun(
  65. enable: false,
  66. stack: 'system',
  67. autoRoute: true,
  68. autoRedirect: true,
  69. autoDetectInterface: true,
  70. dnsHijack: ['any:53'],
  71. ),
  72. proxies: [],
  73. proxyGroups: [
  74. ProxyGroup(
  75. name: 'proxy',
  76. type: 'select',
  77. proxies: ['DIRECT'],
  78. ),
  79. ],
  80. rules: [
  81. 'GEOIP,CN,DIRECT',
  82. 'MATCH,proxy'
  83. ]
  84. );
  85. try {
  86. final file = File(path.join(Paths.config.path, Files.makeInitProxyConfig.path ));
  87. await file.writeAsString(clashConfig.toYaml());
  88. print('配置文件已成功保存到: ${file.path}');
  89. } catch (e) {
  90. print('保存配置文件时发生错误: $e');
  91. throw Exception('无法保存配置文件');
  92. }
  93. }
  94. Future<bool> startClashCore() async {
  95. final timeout = const Duration(seconds: 30);
  96. final checkInterval = const Duration(milliseconds: 200);
  97. var startTime = DateTime.now();
  98. await controllers.config.readClashCoreApi();
  99. try {
  100. controllers.global.updateMsg("启动内核---${controllers.config.config.value.selected}");
  101. if (controllers.config.config.value.selected == 'init_proxy.yaml') {
  102. controllers.global.updateMsg("启动内核初始化");
  103. } else {
  104. controllers.global.updateMsg("启动内核");
  105. }
  106. coreStatus.value = RunningState.starting;
  107. int? exitCode;
  108. clashCoreProcess = await Process.start(
  109. Files.assetsCCore.path,
  110. ['-d', Paths.config.path, '-f', path.join(Paths.config.path, controllers.config.config.value.selected)],
  111. mode: ProcessStartMode.inheritStdio
  112. );
  113. clashCoreProcess!.exitCode.then((code) => exitCode = code);
  114. if (exitCode != null && exitCode != 0) {
  115. controllers.global.updateMsg("启动内核错误,请重启点电脑测试");
  116. return false;
  117. }
  118. controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
  119. while (DateTime.now().difference(startTime) < timeout) {
  120. try {
  121. controllers.global.updateMsg("等待内核启动..");
  122. await controllers.core.fetchHello();
  123. break;
  124. } catch (_) {
  125. await Future.delayed(checkInterval);
  126. }
  127. }
  128. if (DateTime.now().difference(startTime) >= timeout) {
  129. coreStatus.value = RunningState.error;
  130. controllers.global.updateMsg("内核启动超时,重新点击加速后尝试。");
  131. return false;
  132. }
  133. await controllers.core.updateConfig();
  134. coreStatus.value = RunningState.running;
  135. controllers.global.updateMsg("重新加载内核成功,点击连接 ");
  136. return true;
  137. } catch (e) {
  138. controllers.global.updateMsg("启动内核错误");
  139. coreStatus.value = RunningState.error;
  140. return false;
  141. }
  142. }
  143. Future<void> stopClashCore() async {
  144. coreStatus.value = RunningState.stopping;
  145. await controllers.global.closeProxy();
  146. clashCoreProcess?.kill();
  147. killProcess(ClashName.name);
  148. coreStatus.value = RunningState.stoped;
  149. }
  150. Future<bool> initClashCoreConfig() async {
  151. controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
  152. await makeInitConfig();
  153. await startClashCore();
  154. if (coreStatus.value == RunningState.error) {
  155. controllers.global.updateMsg("启动内核失败...");
  156. return false;
  157. } else {
  158. await controllers.core.updateVersion();
  159. controllers.global.updateMsg("启动内核成功...");
  160. return true;
  161. }
  162. }
  163. Future<void> stopClash() async {
  164. controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
  165. if (coreStatus.value == RunningState.running) {
  166. await controllers.config.readClashCoreApi();
  167. controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
  168. await controllers.core.changeConfig(path.join(Paths.config.path, controllers.config.config.value.selected));
  169. }
  170. }
  171. Future<void> reloadClashCore() async {
  172. try {
  173. controllers.config.config.value.selected = Files.makeProxyConfig.path;
  174. if (coreStatus.value == RunningState.running) {
  175. controllers.global.updateMsg("切换配置...");
  176. await controllers.config.readClashCoreApi();
  177. controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
  178. // 在切换配置前确保配置文件有效
  179. final configFile = File(path.join(Paths.config.path, controllers.config.config.value.selected));
  180. if (!await configFile.exists()) {
  181. throw Exception("配置文件不存在");
  182. }
  183. // 确保配置文件包含必要的代理组
  184. final configStr = await configFile.readAsString();
  185. final yamlEditor = YamlEditor(configStr);
  186. final configMap = yamlEditor.parseAt([]) as YamlMap;
  187. if (configMap['proxy-groups'] == null ||
  188. (configMap['proxy-groups'] as YamlList).isEmpty) {
  189. throw Exception("配置文件缺少代理组");
  190. }
  191. await controllers.core.changeConfig(configFile.path);
  192. controllers.global.updateMsg("fetchReloadConfig${controllers.config.clashCoreApiAddress.value}...");
  193. }
  194. } catch (e) {
  195. LogHelper().e("切换配置失败: $e");
  196. // 如果切换失败,尝试回退到初始配置
  197. try {
  198. controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
  199. await controllers.config.readClashCoreApi();
  200. controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
  201. await controllers.core.changeConfig(
  202. path.join(Paths.config.path, controllers.config.config.value.selected)
  203. );
  204. } catch (fallbackError) {
  205. LogHelper().e("回退到初始配置失败: $fallbackError");
  206. throw Exception("配置切换失败");
  207. }
  208. }
  209. }
  210. Future<String> generateYamlConfig(List<NodeMode> nodes) async {
  211. // await updatePorts();
  212. final config = ClashConfig(
  213. mixedPort: controllers.config.mixedPort.value,
  214. allowLan: true,
  215. bindAddress: '*',
  216. mode: controllers.global.modesSelect.value == "global" ? "global" : "rule",
  217. logLevel: 'info',
  218. externalController: '127.0.0.1:${controllers.config.apiAddressPort.value}',
  219. unifiedDelay: false,
  220. geodataMode: true,
  221. tcpConcurrent: false,
  222. findProcessMode: 'strict',
  223. globalClientFingerprint: 'chrome',
  224. profile: {
  225. 'store-selected': true,
  226. 'store-fake-ip': true,
  227. },
  228. sniffer: Sniffer(
  229. enable: true,
  230. sniff: {
  231. 'HTTP': {
  232. 'ports': [80, '8080-8880'],
  233. 'override-destination': true,
  234. },
  235. 'TLS': {
  236. 'ports': [443, 8443],
  237. },
  238. // 'QUIC': {
  239. // 'ports': [443, 8443],
  240. // },
  241. },
  242. skipDomain: ['www.baidu.com'],
  243. ),
  244. dns: DNS(
  245. enable: true,
  246. listen: kDnsListenPort,
  247. ipv6: false,
  248. enhancedMode: kRedirHostMode,
  249. fakeIpFilter: null,
  250. nameserver: kDomesticDNS,
  251. proxyServerNameserver: kDomesticDNS,
  252. nameserverPolicy: {
  253. 'geosite:cn,private': kDomesticDNS,
  254. 'geosite:geolocation-!cn': kForeignDNS,
  255. },
  256. ),
  257. tun: Tun(
  258. enable: controllers.global.routeModesSelect.value == "tun" ? true:false,
  259. stack: 'gvisor',
  260. autoRoute: true,
  261. autoRedirect: false,
  262. autoDetectInterface: true,
  263. dnsHijack: ['any:53'],
  264. ),
  265. proxies: [],
  266. proxyGroups: [
  267. ProxyGroup(
  268. name: 'proxy',
  269. type: 'select',
  270. proxies: [],
  271. ),
  272. ],
  273. rules: [
  274. 'GEOIP,CN,DIRECT',
  275. 'MATCH,proxy',
  276. ],
  277. );
  278. for (final node in nodes) {
  279. BaseProxy proxy;
  280. switch (node.type) {
  281. case 'trojan':
  282. proxy = TrojanProxy(
  283. name: node.name ?? '',
  284. server: node.host ?? '',
  285. port: node.port ?? 0,
  286. password: node.passwd ?? '',
  287. udp: node.udp == 1,
  288. sni: node.sni,
  289. );
  290. break;
  291. case 'shadowsocks':
  292. proxy = SSProxy(
  293. name: node.name ?? '',
  294. type: 'ss',
  295. server: node.host ?? '',
  296. port: node.port ?? 0,
  297. password: node.passwd ?? '',
  298. cipher: node.method ?? '',
  299. udp: node.udp == 1,
  300. );
  301. break;
  302. case 'v2ray':
  303. final type = (node.vless == 1) ? 'vless' : 'vmess';
  304. if (type == 'vless') {
  305. proxy = VlessProxy(
  306. name: node.name ?? '',
  307. uuid: node.uuid ?? '',
  308. server: node.host ?? '',
  309. port: node.port ?? 0,
  310. udp: node.udp == 1,
  311. flow: '',
  312. tls: true,
  313. servername: node.v2Sni,
  314. realityOpts: {'public-key': node.vlessPulkey},
  315. network: 'tcp',
  316. );
  317. } else {
  318. proxy = VmessProxy(
  319. name: node.name ?? '',
  320. uuid: node.uuid ?? '',
  321. server: node.host ?? '',
  322. port: node.port ?? 0,
  323. alterId: node.v2AlterId,
  324. cipher: node.method,
  325. udp: node.udp == 1,
  326. );
  327. }
  328. break;
  329. default:
  330. continue;
  331. }
  332. config.proxies?.add(proxy.toJson());
  333. config.proxyGroups?[0].proxies.add(node.name ?? '');
  334. }
  335. return config.toYaml();
  336. }
  337. Future<void> saveConfigToFile(String filePath, List<NodeMode> nodes) async {
  338. try {
  339. final configYaml = await generateYamlConfig(nodes);
  340. final file = File(filePath);
  341. await file.writeAsString(configYaml);
  342. print('配置文件已成功保存到: $filePath');
  343. } catch (e) {
  344. print('保存配置文件时发生错误: $e');
  345. throw Exception('无法保存配置文件');
  346. }
  347. }
  348. }
  349. extension ConfigToYaml on ClashConfig {
  350. String toYaml() {
  351. final yamlMap = toJson();
  352. final yamlEditor = YamlEditor('');
  353. yamlEditor.update([], yamlMap);
  354. return yamlEditor.toString();
  355. }
  356. }