connection_service.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:get/get.dart';
  4. import 'package:naiyouwl/app/common/LogHelper.dart';
  5. import 'package:naiyouwl/app/controller/controllers.dart';
  6. import 'package:naiyouwl/app/component/connection_status.dart';
  7. import 'package:naiyouwl/app/clash/service/clash_service.dart';
  8. import 'package:naiyouwl/app/controller/service.dart';
  9. import 'package:naiyouwl/app/utils/shell.dart';
  10. import 'package:wl_base_help/wl_base_help.dart';
  11. import 'package:path/path.dart' as path;
  12. import '../const/const.dart';
  13. import '../controller/GlobalController.dart';
  14. import '../modules/home/controllers/home_controller.dart';
  15. import '../utils/WindowsProxyManager.dart';
  16. import '../utils/utils.dart';
  17. import 'dart:convert';
  18. import 'dart:async';
  19. class ConnectionService {
  20. final GlobalController globalController;
  21. final Function(ConnectionStatus) updateStatus;
  22. final ClashService clashService = Get.find<ClashService>();
  23. final ServiceController serviceController = Get.find<ServiceController>();
  24. ConnectionService(this.globalController, this.updateStatus);
  25. Future<void> startConnectionMac() async {
  26. try {
  27. updateStatus(ConnectionStatus.connecting);
  28. globalController.updateMsg("正在启动连接...");
  29. // 更新节点
  30. await globalController.updateNode();
  31. bool success;
  32. // 检查是否是TUN模式
  33. if (globalController.routeModesSelect.value == "tun") {
  34. final password = await getKeychainPassword();
  35. if (password == null) {
  36. throw Exception("未保存管理员密码,请先保存密码");
  37. }
  38. await globalController.makeProxy();
  39. success = await clashService.startTunCore(password);
  40. if (!success) {
  41. throw Exception("内核启动失败");
  42. }
  43. } else {
  44. await controllers.cc_service.reloadClashCore();
  45. }
  46. await globalController.swift(globalController.selectedNode.value?.name ?? "");
  47. await Future.delayed(const Duration(seconds: 2));
  48. updateStatus(ConnectionStatus.connected);
  49. globalController.connectStatus.value = true;
  50. globalController.updateMsg("连接成功");
  51. await globalController.openProxy();
  52. } catch (e) {
  53. updateStatus(ConnectionStatus.disconnected);
  54. globalController.handleApiError("连接失败: $e");
  55. throw e;
  56. }
  57. }
  58. Future<void> startConnectionWin() async {
  59. try {
  60. updateStatus(ConnectionStatus.connecting);
  61. globalController.updateMsg("正在启动连接...");
  62. await globalController.updateNode();
  63. await controllers.cc_service.reloadClashCore();
  64. await globalController.swift(globalController.selectedNode.value?.name ?? "");
  65. // 等待连接建立
  66. await Future.delayed(Duration(seconds: 2));
  67. updateStatus(ConnectionStatus.connected);
  68. globalController.connectStatus.value = true;
  69. globalController.updateMsg("连接成功");
  70. final proxyManager = WlBaseHelp();
  71. if(Platform.isWindows){
  72. int? port = controllers.config.mixedPort.value;
  73. if (port == 0) {
  74. port = null;
  75. }
  76. int? socksPort = controllers.config.mixedPort.value;
  77. if (socksPort == 0) {
  78. socksPort = null;
  79. }
  80. int? mixedPort = controllers.config.mixedPort.value;
  81. if (mixedPort == 0) {
  82. mixedPort = null;
  83. }
  84. if(port !=null){
  85. await proxyManager.startProxy(port);
  86. }
  87. }
  88. // // 检查所有内核是否已停止
  89. // bool allCoresStopped = await globalController.checkAllCoresStopped();
  90. //
  91. // if (!allCoresStopped) {
  92. // // 检查并停止占用端口
  93. // await globalController.checkAndStopIfPortsOccupied();
  94. //
  95. // // 如果端口被占用,尝试更换端口
  96. // if (globalController.msgStatus.value == '端口被占用,已停止所有内核') {
  97. // // 尝试为每个端口找到新的可用端口
  98. // int newMixedPort = await findAvailablePort(controllers.config.mixedPort.value);
  99. // int newApiPort = await findAvailablePort(controllers.config.apiAddressPort.value);
  100. // int newServicePort = await findAvailablePort(controllers.config.servicePort.value);
  101. //
  102. // // 更新配置
  103. // controllers.config.mixedPort.value = newMixedPort;
  104. // controllers.config.apiAddressPort.value = newApiPort;
  105. // controllers.config.servicePort.value = newServicePort;
  106. //
  107. // // 保存新的配置
  108. // await controllers.config.saveConfig();
  109. //
  110. // globalController.updateMsg("端口已更新,正在重新启动...");
  111. // }
  112. // }
  113. // if(Platform.isMacOS){
  114. // if (serviceController.serviceIsRuning) {
  115. // await globalController.makeProxy();
  116. // await serviceController.reloadClashCore();
  117. // // await serviceController.fetchSetProxy();
  118. // } else {
  119. // // if (controllers.service.clashServiceIsRuning) {
  120. // // await globalController.makeProxy();
  121. // // await reloadClashCore();
  122. // // }
  123. // }
  124. // } else {
  125. // // if (controllers.service.clashServiceIsRuning) {
  126. // // await globalController.makeProxy();
  127. // // await reloadClashCore();
  128. // // }
  129. // }
  130. // if(Platform.isMacOS){
  131. // if (serviceController.serviceIsRuning) {
  132. // //await serviceController.fetchStartInit();
  133. // await serviceController.fetchSetProxy();
  134. // await globalController.openProxy();
  135. // } else {
  136. // if (controllers.service.clashServiceIsRuning) {
  137. // globalController.connectStatus.value = true;
  138. //
  139. // } else {
  140. // throw Exception("内核启动失败");
  141. // }
  142. // }
  143. // } else {
  144. // if (controllers.service.clashServiceIsRuning) {
  145. //
  146. // } else {
  147. // throw Exception("内核启动失败");
  148. // }
  149. // }
  150. // updateStatus(ConnectionStatus.connected);
  151. // globalController.updateMsg("连接成功");
  152. // controllers.global.connectStatus.value = true;
  153. //
  154. // //检测系统代理并切换成网卡模式
  155. // if(Platform.isWindows){
  156. //
  157. // if(!await proxyManager.isProxyEnabled()){
  158. // globalController.updateMsg("当前没有设置系统代理,20秒后切换成网卡模式");
  159. // await Future.delayed(Duration(seconds: 20)); // 等待核心状态更新
  160. // // await globalController.TunProxySwitch(true);
  161. // // await globalController.makeProxy();
  162. // // await reloadClashCore();
  163. // LogHelper().d("重置网卡模式");
  164. // }
  165. //
  166. //
  167. // }
  168. } catch (e) {
  169. updateStatus(ConnectionStatus.disconnected);
  170. globalController.handleApiError("连接失败: $e");
  171. }
  172. }
  173. Future<void> stopConnection() async {
  174. try {
  175. updateStatus(ConnectionStatus.disconnected);
  176. globalController.updateMsg("正在断开连接...");
  177. if(Platform.isMacOS){
  178. if (serviceController.serviceIsRuning) {
  179. await serviceController.fetchSetProxyStop();
  180. } else {
  181. await controllers.cc_service.stopClash();
  182. await globalController.closeProxy();
  183. }
  184. }
  185. globalController.connectStatus.value = false;
  186. updateStatus(ConnectionStatus.disconnected);
  187. globalController.updateMsg("已断开连接");
  188. } catch (e) {
  189. globalController.handleApiError("断开连接失败: $e");
  190. }
  191. }
  192. Future<void> reloadClashCore() async {
  193. try {
  194. globalController.updateMsg("正在重新加载内核...");
  195. if (serviceController.serviceIsRuning) {
  196. await serviceController.reloadClashCore();
  197. globalController.updateMsg("内核已重新加载");
  198. globalController.swift(globalController.selectedNode.value?.name ?? "");
  199. return;
  200. }
  201. if (controllers.service.clashServiceIsRuning){
  202. await controllers.service.reloadClashCore();
  203. globalController.updateMsg("内核已重新加载");
  204. globalController.swift(globalController.selectedNode.value?.name ?? "");
  205. } else {
  206. globalController.updateMsg("内核启动失败");
  207. }
  208. } catch (e) {
  209. globalController.handleApiError("重新加载内核失败: $e");
  210. }
  211. }
  212. Future<void> coreInit() async {
  213. try {
  214. globalController.updateMsg("正在初始化核心...");
  215. if (serviceController.installStatus.value) {
  216. //每次启动多要初始化
  217. await controllers.cc_service.fetchStartInit();
  218. } else {
  219. await controllers.service.initClashCoreConfig();
  220. }
  221. globalController.updateMsg("核心初始化完成");
  222. } catch (e) {
  223. globalController.updateMsg("失败...");
  224. globalController.handleApiError("核心初始化失败: $e");
  225. }
  226. }
  227. Future<void> installService() async{
  228. if(serviceController.serviceStatus.value == RunningState.stoped){
  229. controllers.service.stopClashCore();
  230. globalController.updateMsg("正在安装服务");
  231. await serviceController.serviceModeSwitch(true);
  232. }
  233. }
  234. Future<void> UninstallService() async{
  235. if(serviceController.serviceStatus.value == RunningState.running){
  236. controllers.global.updateMsg("正在卸载服务");
  237. await serviceController.serviceModeSwitch(false);
  238. }
  239. }
  240. }