|
@@ -8,12 +8,14 @@ import 'package:naiyouwl/app/clash/service/clash_service.dart';
|
|
import 'package:naiyouwl/app/controller/service.dart';
|
|
import 'package:naiyouwl/app/controller/service.dart';
|
|
import 'package:naiyouwl/app/utils/shell.dart';
|
|
import 'package:naiyouwl/app/utils/shell.dart';
|
|
import 'package:wl_base_help/wl_base_help.dart';
|
|
import 'package:wl_base_help/wl_base_help.dart';
|
|
-
|
|
|
|
|
|
+import 'package:path/path.dart' as path;
|
|
|
|
+import '../const/const.dart';
|
|
import '../controller/GlobalController.dart';
|
|
import '../controller/GlobalController.dart';
|
|
import '../modules/home/controllers/home_controller.dart';
|
|
import '../modules/home/controllers/home_controller.dart';
|
|
import '../utils/WindowsProxyManager.dart';
|
|
import '../utils/WindowsProxyManager.dart';
|
|
import '../utils/utils.dart';
|
|
import '../utils/utils.dart';
|
|
-
|
|
|
|
|
|
+import 'dart:convert';
|
|
|
|
+import 'dart:async';
|
|
class ConnectionService {
|
|
class ConnectionService {
|
|
final GlobalController globalController;
|
|
final GlobalController globalController;
|
|
final Function(ConnectionStatus) updateStatus;
|
|
final Function(ConnectionStatus) updateStatus;
|
|
@@ -22,17 +24,45 @@ class ConnectionService {
|
|
|
|
|
|
ConnectionService(this.globalController, this.updateStatus);
|
|
ConnectionService(this.globalController, this.updateStatus);
|
|
Future<void> startConnectionMac() async {
|
|
Future<void> startConnectionMac() async {
|
|
- updateStatus(ConnectionStatus.connecting);
|
|
|
|
- globalController.updateMsg("正在启动连接...");
|
|
|
|
- await globalController.updateNode();
|
|
|
|
- await controllers.cc_service.reloadClashCore();
|
|
|
|
- await globalController.swift(globalController.selectedNode.value?.name ?? "");
|
|
|
|
- // 等待连接建立
|
|
|
|
- await Future.delayed(Duration(seconds: 2));
|
|
|
|
- updateStatus(ConnectionStatus.connected);
|
|
|
|
- globalController.connectStatus.value = true;
|
|
|
|
- globalController.updateMsg("连接成功");
|
|
|
|
- await globalController.openProxy();
|
|
|
|
|
|
+ try {
|
|
|
|
+ updateStatus(ConnectionStatus.connecting);
|
|
|
|
+ globalController.updateMsg("正在启动连接...");
|
|
|
|
+
|
|
|
|
+ // 更新节点
|
|
|
|
+ await globalController.updateNode();
|
|
|
|
+
|
|
|
|
+ bool success;
|
|
|
|
+ // 检查是否是TUN模式
|
|
|
|
+ if (globalController.routeModesSelect.value == "tun") {
|
|
|
|
+ final password = await getKeychainPassword();
|
|
|
|
+ if (password == null) {
|
|
|
|
+ throw Exception("未保存管理员密码,请先保存密码");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ await globalController.makeProxy();
|
|
|
|
+ success = await clashService.startTunCore(password);
|
|
|
|
+ if (!success) {
|
|
|
|
+ throw Exception("内核启动失败");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ await controllers.cc_service.reloadClashCore();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ await globalController.swift(globalController.selectedNode.value?.name ?? "");
|
|
|
|
+ await Future.delayed(const Duration(seconds: 2));
|
|
|
|
+
|
|
|
|
+ updateStatus(ConnectionStatus.connected);
|
|
|
|
+ globalController.connectStatus.value = true;
|
|
|
|
+ globalController.updateMsg("连接成功");
|
|
|
|
+ await globalController.openProxy();
|
|
|
|
+
|
|
|
|
+ } catch (e) {
|
|
|
|
+ updateStatus(ConnectionStatus.disconnected);
|
|
|
|
+ globalController.handleApiError("连接失败: $e");
|
|
|
|
+ throw e;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> startConnectionWin() async {
|
|
Future<void> startConnectionWin() async {
|
|
@@ -173,40 +203,20 @@ class ConnectionService {
|
|
try {
|
|
try {
|
|
updateStatus(ConnectionStatus.disconnected);
|
|
updateStatus(ConnectionStatus.disconnected);
|
|
globalController.updateMsg("正在断开连接...");
|
|
globalController.updateMsg("正在断开连接...");
|
|
-
|
|
|
|
|
|
+
|
|
if(Platform.isMacOS){
|
|
if(Platform.isMacOS){
|
|
if (serviceController.serviceIsRuning) {
|
|
if (serviceController.serviceIsRuning) {
|
|
- //await serviceController.stopClash();
|
|
|
|
await serviceController.fetchSetProxyStop();
|
|
await serviceController.fetchSetProxyStop();
|
|
-
|
|
|
|
- } else {
|
|
|
|
- if (controllers.service.clashServiceIsRuning){
|
|
|
|
- await controllers.service.stopClash();
|
|
|
|
- await globalController.closeProxy();
|
|
|
|
- globalController.updateMsg("内核已重新加载");
|
|
|
|
- } else {
|
|
|
|
- globalController.updateMsg("内核启动失败");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- } else{
|
|
|
|
- if (controllers.service.clashServiceIsRuning){
|
|
|
|
- await controllers.service.stopClash();
|
|
|
|
- final proxyManager = WlBaseHelp();
|
|
|
|
- await proxyManager.stopProxy();
|
|
|
|
- // await globalController.closeProxy();
|
|
|
|
- globalController.updateMsg("内核已重新加载");
|
|
|
|
} else {
|
|
} else {
|
|
- globalController.updateMsg("内核启动失败");
|
|
|
|
|
|
+ await controllers.cc_service.stopClash();
|
|
|
|
+ await globalController.closeProxy();
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+
|
|
globalController.connectStatus.value = false;
|
|
globalController.connectStatus.value = false;
|
|
updateStatus(ConnectionStatus.disconnected);
|
|
updateStatus(ConnectionStatus.disconnected);
|
|
globalController.updateMsg("已断开连接");
|
|
globalController.updateMsg("已断开连接");
|
|
- controllers.global.connectStatus.value = false;
|
|
|
|
|
|
+
|
|
} catch (e) {
|
|
} catch (e) {
|
|
globalController.handleApiError("断开连接失败: $e");
|
|
globalController.handleApiError("断开连接失败: $e");
|
|
}
|
|
}
|