connection_service.dart 8.2 KB

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