connection_service.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:get/get.dart';
  4. import 'package:naiyouwl/app/controller/controllers.dart';
  5. import 'package:naiyouwl/app/component/connection_status.dart';
  6. import 'package:naiyouwl/app/clash/service/clash_service.dart';
  7. import 'package:naiyouwl/app/controller/service.dart';
  8. import '../controller/GlobalController.dart';
  9. import '../modules/home/controllers/home_controller.dart';
  10. import '../utils/utils.dart';
  11. class ConnectionService {
  12. final GlobalController globalController;
  13. final Function(ConnectionStatus) updateStatus;
  14. final ClashService clashService = Get.find<ClashService>();
  15. final ServiceController serviceController = Get.find<ServiceController>();
  16. ConnectionService(this.globalController, this.updateStatus);
  17. Future<void> startConnection() async {
  18. try {
  19. updateStatus(ConnectionStatus.connecting);
  20. globalController.updateMsg("正在启动连接...");
  21. if(Platform.isMacOS){
  22. if (serviceController.serviceIsRuning) {
  23. await globalController.makeProxy();
  24. await serviceController.reloadClashCore();
  25. // await serviceController.fetchSetProxy();
  26. } else {
  27. if (controllers.service.clashServiceIsRuning) {
  28. await globalController.makeProxy();
  29. await reloadClashCore();
  30. }
  31. }
  32. } else {
  33. if (controllers.service.clashServiceIsRuning) {
  34. await globalController.makeProxy();
  35. await reloadClashCore();
  36. }
  37. }
  38. await globalController.updateNode();
  39. // 等待连接建立
  40. await Future.delayed(Duration(seconds: 2));
  41. if(Platform.isMacOS){
  42. if (serviceController.serviceIsRuning) {
  43. //await serviceController.fetchStartInit();
  44. await serviceController.fetchSetProxy();
  45. } else {
  46. if (controllers.service.clashServiceIsRuning) {
  47. globalController.connectStatus.value = true;
  48. await globalController.openProxy();
  49. } else {
  50. throw Exception("内核启动失败");
  51. }
  52. }
  53. } else {
  54. if (controllers.service.clashServiceIsRuning) {
  55. updateStatus(ConnectionStatus.connected);
  56. globalController.connectStatus.value = true;
  57. globalController.updateMsg("连接成功");
  58. } else {
  59. throw Exception("内核启动失败");
  60. }
  61. }
  62. updateStatus(ConnectionStatus.connected);
  63. globalController.updateMsg("连接成功");
  64. } catch (e) {
  65. updateStatus(ConnectionStatus.disconnected);
  66. globalController.handleApiError("连接失败: $e");
  67. }
  68. }
  69. Future<void> stopConnection() async {
  70. try {
  71. updateStatus(ConnectionStatus.disconnected);
  72. globalController.updateMsg("正在断开连接...");
  73. if(Platform.isMacOS){
  74. if (serviceController.serviceIsRuning) {
  75. await serviceController.stopClash();
  76. await serviceController.fetchSetProxyStop();
  77. } else {
  78. if (controllers.service.clashServiceIsRuning){
  79. await controllers.service.stopClash();
  80. await globalController.closeProxy();
  81. globalController.updateMsg("内核已重新加载");
  82. } else {
  83. globalController.updateMsg("内核启动失败");
  84. }
  85. }
  86. } else{
  87. if (controllers.service.clashServiceIsRuning){
  88. await controllers.service.stopClash();
  89. await globalController.closeProxy();
  90. globalController.updateMsg("内核已重新加载");
  91. } else {
  92. globalController.updateMsg("内核启动失败");
  93. }
  94. }
  95. globalController.connectStatus.value = false;
  96. updateStatus(ConnectionStatus.disconnected);
  97. globalController.updateMsg("已断开连接");
  98. } catch (e) {
  99. globalController.handleApiError("断开连接失败: $e");
  100. }
  101. }
  102. Future<void> reloadClashCore() async {
  103. try {
  104. globalController.updateMsg("正在重新加载内核...");
  105. if (serviceController.serviceIsRuning) {
  106. await serviceController.reloadClashCore();
  107. globalController.updateMsg("内核已重新加载");
  108. globalController.swift(globalController.selectedNode.value?.name ?? "");
  109. return;
  110. }
  111. if (controllers.service.clashServiceIsRuning){
  112. await controllers.service.reloadClashCore();
  113. globalController.updateMsg("内核已重新加载");
  114. globalController.swift(globalController.selectedNode.value?.name ?? "");
  115. } else {
  116. globalController.updateMsg("内核启动失败");
  117. }
  118. } catch (e) {
  119. globalController.handleApiError("重新加载内核失败: $e");
  120. }
  121. }
  122. Future<void> coreInit() async {
  123. try {
  124. globalController.updateMsg("正在初始化核心...");
  125. if (serviceController.installStatus.value) {
  126. //每次启动多要初始化
  127. await controllers.cc_service.fetchStartInit();
  128. } else {
  129. await controllers.service.initClashCoreConfig();
  130. }
  131. globalController.updateMsg("核心初始化完成");
  132. } catch (e) {
  133. globalController.updateMsg("失败...");
  134. globalController.handleApiError("核心初始化失败: $e");
  135. }
  136. }
  137. Future<void> installService() async{
  138. if(serviceController.serviceStatus.value == RunningState.stoped){
  139. controllers.service.stopClashCore();
  140. globalController.updateMsg("正在安装服务");
  141. await serviceController.serviceModeSwitch(true);
  142. }
  143. }
  144. Future<void> UninstallService() async{
  145. if(serviceController.serviceStatus.value == RunningState.running){
  146. controllers.global.updateMsg("正在卸载服务");
  147. await serviceController.serviceModeSwitch(false);
  148. }
  149. }
  150. }