2024-04-30 23:38:49 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:animations/animations.dart';
|
|
|
|
|
import 'package:fl_clash/clash/clash.dart';
|
2024-08-15 16:18:00 +08:00
|
|
|
import 'package:fl_clash/plugins/service.dart';
|
|
|
|
|
import 'package:fl_clash/plugins/vpn.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/widgets/scaffold.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-09-05 08:59:45 +08:00
|
|
|
import 'package:intl/intl.dart';
|
2024-07-02 08:08:31 +08:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2024-09-05 08:59:45 +08:00
|
|
|
import 'package:tray_manager/tray_manager.dart';
|
2024-07-21 21:51:56 +08:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'controller.dart';
|
2024-07-13 16:36:08 +08:00
|
|
|
|
2024-09-05 08:59:45 +08:00
|
|
|
import 'enum/enum.dart';
|
|
|
|
|
import 'l10n/l10n.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'models/models.dart';
|
|
|
|
|
import 'common/common.dart';
|
|
|
|
|
|
|
|
|
|
class GlobalState {
|
|
|
|
|
Timer? timer;
|
2024-05-10 10:11:27 +08:00
|
|
|
Timer? groupsUpdateTimer;
|
2024-07-13 16:36:08 +08:00
|
|
|
var isVpnService = false;
|
2024-08-23 18:33:40 +08:00
|
|
|
var autoRun = false;
|
2024-07-02 08:08:31 +08:00
|
|
|
late PackageInfo packageInfo;
|
2024-05-10 10:11:27 +08:00
|
|
|
Function? updateCurrentDelayDebounce;
|
2024-04-30 23:38:49 +08:00
|
|
|
PageController? pageController;
|
2024-09-05 08:59:45 +08:00
|
|
|
late Measure measure;
|
2024-08-15 16:18:00 +08:00
|
|
|
DateTime? startTime;
|
2024-04-30 23:38:49 +08:00
|
|
|
final navigatorKey = GlobalKey<NavigatorState>();
|
2024-05-11 17:02:34 +08:00
|
|
|
late AppController appController;
|
2024-04-30 23:38:49 +08:00
|
|
|
GlobalKey<CommonScaffoldState> homeScaffoldKey = GlobalKey();
|
|
|
|
|
List<Function> updateFunctionLists = [];
|
2024-09-05 08:59:45 +08:00
|
|
|
var isTrayInit = false;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2024-08-15 16:18:00 +08:00
|
|
|
bool get isStart => startTime != null && startTime!.isBeforeNow;
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
startListenUpdate() {
|
|
|
|
|
if (timer != null && timer!.isActive == true) return;
|
|
|
|
|
timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
|
|
|
|
|
for (final function in updateFunctionLists) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopListenUpdate() {
|
|
|
|
|
if (timer == null || timer?.isActive == false) return;
|
|
|
|
|
timer?.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<void> updateClashConfig({
|
2024-04-30 23:38:49 +08:00
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
required Config config,
|
|
|
|
|
bool isPatch = true,
|
|
|
|
|
}) async {
|
2024-05-20 15:15:09 +08:00
|
|
|
await config.currentProfile?.checkAndUpdate();
|
2024-06-29 21:42:00 +08:00
|
|
|
final res = await clashCore.updateConfig(
|
|
|
|
|
UpdateConfigParams(
|
2024-08-04 08:21:14 +08:00
|
|
|
profileId: config.currentProfileId ?? "",
|
2024-06-29 21:42:00 +08:00
|
|
|
config: clashConfig,
|
2024-07-02 08:08:31 +08:00
|
|
|
params: ConfigExtendedParams(
|
|
|
|
|
isPatch: isPatch,
|
2024-07-24 01:27:49 +08:00
|
|
|
isCompatible: true,
|
2024-07-02 08:08:31 +08:00
|
|
|
selectedMap: config.currentSelectedMap,
|
2024-08-26 20:44:30 +08:00
|
|
|
overrideDns: config.overrideDns,
|
2024-07-02 08:08:31 +08:00
|
|
|
testUrl: config.testUrl,
|
|
|
|
|
),
|
2024-06-29 21:42:00 +08:00
|
|
|
),
|
|
|
|
|
);
|
2024-06-03 18:02:05 +08:00
|
|
|
if (res.isNotEmpty) throw res;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCoreVersionInfo(AppState appState) {
|
|
|
|
|
appState.versionInfo = clashCore.getVersionInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 16:18:00 +08:00
|
|
|
handleStart({
|
2024-04-30 23:38:49 +08:00
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
2024-08-15 16:18:00 +08:00
|
|
|
clashCore.start();
|
|
|
|
|
if (globalState.isVpnService) {
|
|
|
|
|
await vpn?.startVpn(clashConfig.mixedPort);
|
|
|
|
|
startListenUpdate();
|
|
|
|
|
return;
|
2024-07-17 17:02:25 +08:00
|
|
|
}
|
2024-08-15 16:18:00 +08:00
|
|
|
startTime ??= DateTime.now();
|
|
|
|
|
await service?.init();
|
2024-04-30 23:38:49 +08:00
|
|
|
startListenUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 16:18:00 +08:00
|
|
|
updateStartTime() {
|
|
|
|
|
startTime = clashCore.getRunTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleStop() async {
|
|
|
|
|
clashCore.stop();
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
clashCore.stopTun();
|
|
|
|
|
}
|
|
|
|
|
await service?.destroy();
|
|
|
|
|
startTime = null;
|
2024-04-30 23:38:49 +08:00
|
|
|
stopListenUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-02 08:08:31 +08:00
|
|
|
Future applyProfile({
|
2024-05-04 16:39:21 +08:00
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
2024-06-03 18:02:05 +08:00
|
|
|
await updateClashConfig(
|
2024-05-04 16:39:21 +08:00
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
config: config,
|
|
|
|
|
isPatch: false,
|
|
|
|
|
);
|
|
|
|
|
await updateGroups(appState);
|
2024-08-04 08:21:14 +08:00
|
|
|
await updateProviders(appState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateProviders(AppState appState) async {
|
|
|
|
|
appState.providers = await clashCore.getExternalProviders();
|
2024-05-04 16:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
init({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
|
|
|
|
appState.isInit = clashCore.isInit;
|
|
|
|
|
if (!appState.isInit) {
|
|
|
|
|
appState.isInit = await clashService.init(
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
2024-08-01 23:51:00 +08:00
|
|
|
clashCore.setState(
|
|
|
|
|
CoreState(
|
2024-08-15 16:18:00 +08:00
|
|
|
enable: config.vpnProps.enable,
|
2024-08-01 23:51:00 +08:00
|
|
|
accessControl: config.isAccessControl ? config.accessControl : null,
|
2024-08-15 16:18:00 +08:00
|
|
|
allowBypass: config.vpnProps.allowBypass,
|
|
|
|
|
systemProxy: config.vpnProps.systemProxy,
|
2024-08-01 23:51:00 +08:00
|
|
|
mixedPort: clashConfig.mixedPort,
|
|
|
|
|
onlyProxy: config.onlyProxy,
|
2024-08-15 16:18:00 +08:00
|
|
|
currentProfileName:
|
|
|
|
|
config.currentProfile?.label ?? config.currentProfileId ?? "",
|
2024-08-01 23:51:00 +08:00
|
|
|
),
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-05-04 16:39:21 +08:00
|
|
|
updateCoreVersionInfo(appState);
|
2024-05-02 00:32:11 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-03 14:31:10 +08:00
|
|
|
Future<void> updateGroups(AppState appState) async {
|
2024-05-04 16:39:21 +08:00
|
|
|
appState.groups = await clashCore.getProxiesGroups();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showMessage({
|
|
|
|
|
required String title,
|
|
|
|
|
required InlineSpan message,
|
|
|
|
|
Function()? onTab,
|
2024-05-31 09:59:18 +08:00
|
|
|
String? confirmText,
|
2024-04-30 23:38:49 +08:00
|
|
|
}) {
|
|
|
|
|
showCommonDialog(
|
|
|
|
|
child: Builder(
|
|
|
|
|
builder: (context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: Text(title),
|
2024-05-31 09:59:18 +08:00
|
|
|
content: Container(
|
2024-04-30 23:38:49 +08:00
|
|
|
width: 300,
|
2024-06-03 18:02:05 +08:00
|
|
|
constraints: const BoxConstraints(maxHeight: 200),
|
2024-05-31 09:59:18 +08:00
|
|
|
child: SingleChildScrollView(
|
2024-07-26 08:05:22 +08:00
|
|
|
child: SelectableText.rich(
|
|
|
|
|
TextSpan(
|
2024-05-31 09:59:18 +08:00
|
|
|
style: Theme.of(context).textTheme.labelLarge,
|
|
|
|
|
children: [message],
|
|
|
|
|
),
|
2024-07-26 08:05:22 +08:00
|
|
|
style: const TextStyle(
|
|
|
|
|
overflow: TextOverflow.visible,
|
|
|
|
|
),
|
2024-05-20 15:15:09 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: onTab ??
|
|
|
|
|
() {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
},
|
2024-05-31 09:59:18 +08:00
|
|
|
child: Text(confirmText ?? appLocalizations.confirm),
|
2024-04-30 23:38:49 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 08:59:45 +08:00
|
|
|
_updateOtherTray() async {
|
|
|
|
|
if (isTrayInit == false) {
|
|
|
|
|
await trayManager.setIcon(
|
|
|
|
|
other.getTrayIconPath(),
|
|
|
|
|
);
|
|
|
|
|
await trayManager.setToolTip(
|
|
|
|
|
appName,
|
|
|
|
|
);
|
|
|
|
|
isTrayInit = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_updateLinuxTray() async {
|
|
|
|
|
await trayManager.destroy();
|
|
|
|
|
await trayManager.setIcon(
|
|
|
|
|
other.getTrayIconPath(),
|
|
|
|
|
);
|
|
|
|
|
await trayManager.setToolTip(
|
|
|
|
|
appName,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateTray({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
|
|
|
|
final appLocalizations = await AppLocalizations.load(
|
|
|
|
|
other.getLocaleForString(config.locale) ??
|
|
|
|
|
WidgetsBinding.instance.platformDispatcher.locale,
|
|
|
|
|
);
|
|
|
|
|
if (!Platform.isLinux) {
|
|
|
|
|
_updateOtherTray();
|
|
|
|
|
}
|
|
|
|
|
List<MenuItem> menuItems = [];
|
|
|
|
|
final showMenuItem = MenuItem(
|
|
|
|
|
label: appLocalizations.show,
|
|
|
|
|
onClick: (_) {
|
|
|
|
|
window?.show();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(showMenuItem);
|
|
|
|
|
final startMenuItem = MenuItem.checkbox(
|
|
|
|
|
label: appState.isStart ? appLocalizations.stop : appLocalizations.start,
|
|
|
|
|
onClick: (_) async {
|
|
|
|
|
globalState.appController.updateStatus(!appState.isStart);
|
|
|
|
|
},
|
|
|
|
|
checked: false,
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(startMenuItem);
|
|
|
|
|
menuItems.add(MenuItem.separator());
|
|
|
|
|
for (final mode in Mode.values) {
|
|
|
|
|
menuItems.add(
|
|
|
|
|
MenuItem.checkbox(
|
|
|
|
|
label: Intl.message(mode.name),
|
|
|
|
|
onClick: (_) {
|
|
|
|
|
globalState.appController.clashConfig.mode = mode;
|
|
|
|
|
},
|
|
|
|
|
checked: mode == appState.mode,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
menuItems.add(MenuItem.separator());
|
|
|
|
|
if (appState.isStart) {
|
|
|
|
|
menuItems.add(
|
|
|
|
|
MenuItem.checkbox(
|
|
|
|
|
label: appLocalizations.tun,
|
|
|
|
|
onClick: (_) {
|
|
|
|
|
final clashConfig = globalState.appController.clashConfig;
|
|
|
|
|
clashConfig.tun = clashConfig.tun.copyWith(
|
|
|
|
|
enable: !clashConfig.tun.enable,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
checked: clashConfig.tun.enable,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(
|
|
|
|
|
MenuItem.checkbox(
|
|
|
|
|
label: appLocalizations.systemProxy,
|
|
|
|
|
onClick: (_) {
|
|
|
|
|
final config = globalState.appController.config;
|
|
|
|
|
config.desktopProps = config.desktopProps.copyWith(
|
|
|
|
|
systemProxy: !config.desktopProps.systemProxy,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
checked: config.desktopProps.systemProxy,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(MenuItem.separator());
|
|
|
|
|
}
|
|
|
|
|
final autoStartMenuItem = MenuItem.checkbox(
|
|
|
|
|
label: appLocalizations.autoLaunch,
|
|
|
|
|
onClick: (_) async {
|
|
|
|
|
globalState.appController.config.autoLaunch =
|
|
|
|
|
!globalState.appController.config.autoLaunch;
|
|
|
|
|
},
|
|
|
|
|
checked: config.autoLaunch,
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(autoStartMenuItem);
|
|
|
|
|
menuItems.add(MenuItem.separator());
|
|
|
|
|
final exitMenuItem = MenuItem(
|
|
|
|
|
label: appLocalizations.exit,
|
|
|
|
|
onClick: (_) async {
|
|
|
|
|
await globalState.appController.handleExit();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
menuItems.add(exitMenuItem);
|
|
|
|
|
final menu = Menu();
|
|
|
|
|
menu.items = menuItems;
|
|
|
|
|
trayManager.setContextMenu(menu);
|
|
|
|
|
if (Platform.isLinux) {
|
|
|
|
|
_updateLinuxTray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 01:27:49 +08:00
|
|
|
changeProxy({
|
|
|
|
|
required Config config,
|
|
|
|
|
required String groupName,
|
|
|
|
|
required String proxyName,
|
|
|
|
|
}) {
|
|
|
|
|
clashCore.changeProxy(
|
|
|
|
|
ChangeProxyParams(
|
|
|
|
|
groupName: groupName,
|
|
|
|
|
proxyName: proxyName,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-07-26 08:05:22 +08:00
|
|
|
if (config.isCloseConnections) {
|
2024-07-24 01:27:49 +08:00
|
|
|
clashCore.closeConnections();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:15:09 +08:00
|
|
|
Future<T?> showCommonDialog<T>({
|
2024-04-30 23:38:49 +08:00
|
|
|
required Widget child,
|
|
|
|
|
}) async {
|
|
|
|
|
return await showModal<T>(
|
|
|
|
|
context: navigatorKey.currentState!.context,
|
|
|
|
|
configuration: const FadeScaleTransitionConfiguration(
|
|
|
|
|
barrierColor: Colors.black38,
|
|
|
|
|
),
|
|
|
|
|
builder: (_) => child,
|
2024-05-20 15:15:09 +08:00
|
|
|
filter: filter,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
2024-05-31 09:59:18 +08:00
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
updateTraffic({
|
|
|
|
|
AppState? appState,
|
|
|
|
|
}) {
|
|
|
|
|
final traffic = clashCore.getTraffic();
|
2024-07-13 16:36:08 +08:00
|
|
|
if (Platform.isAndroid && isVpnService == true) {
|
2024-08-15 16:18:00 +08:00
|
|
|
vpn?.startForeground(
|
2024-08-04 08:21:14 +08:00
|
|
|
title: clashCore.getState().currentProfileName,
|
2024-04-30 23:38:49 +08:00
|
|
|
content: "$traffic",
|
|
|
|
|
);
|
2024-07-13 16:36:08 +08:00
|
|
|
} else {
|
|
|
|
|
if (appState != null) {
|
|
|
|
|
appState.addTraffic(traffic);
|
|
|
|
|
appState.totalTraffic = clashCore.getTotalTraffic();
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-10 10:11:27 +08:00
|
|
|
|
|
|
|
|
showSnackBar(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
required String message,
|
|
|
|
|
SnackBarAction? action,
|
|
|
|
|
}) {
|
2024-07-13 16:36:08 +08:00
|
|
|
final width = context.width;
|
|
|
|
|
EdgeInsets margin;
|
|
|
|
|
if (width < 600) {
|
|
|
|
|
margin = const EdgeInsets.only(
|
|
|
|
|
bottom: 16,
|
|
|
|
|
right: 16,
|
|
|
|
|
left: 16,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
margin = EdgeInsets.only(
|
|
|
|
|
bottom: 16,
|
|
|
|
|
left: 16,
|
|
|
|
|
right: width - 316,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
action: action,
|
|
|
|
|
content: Text(message),
|
|
|
|
|
behavior: SnackBarBehavior.floating,
|
|
|
|
|
duration: const Duration(milliseconds: 1500),
|
|
|
|
|
margin: margin,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-05-10 10:11:27 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<T?> safeRun<T>(
|
|
|
|
|
FutureOr<T> Function() futureFunction, {
|
|
|
|
|
String? title,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final res = await futureFunction();
|
|
|
|
|
return res;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMessage(
|
|
|
|
|
title: title ?? appLocalizations.tip,
|
|
|
|
|
message: TextSpan(
|
|
|
|
|
text: e.toString(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-21 21:51:56 +08:00
|
|
|
|
|
|
|
|
openUrl(String url) {
|
|
|
|
|
showMessage(
|
|
|
|
|
message: TextSpan(text: url),
|
|
|
|
|
title: appLocalizations.externalLink,
|
|
|
|
|
confirmText: appLocalizations.go,
|
|
|
|
|
onTab: () {
|
|
|
|
|
launchUrl(Uri.parse(url));
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final globalState = GlobalState();
|