connection_service.dart 9.1 KB

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