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-11-09 20:17:57 +08:00
|
|
|
import 'package:fl_clash/enum/enum.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-07-02 08:08:31 +08:00
|
|
|
import 'package:package_info_plus/package_info_plus.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-11-09 20:17:57 +08:00
|
|
|
import 'common/common.dart';
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'controller.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'models/models.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-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-12-09 01:40:39 +08:00
|
|
|
final safeMessageOffsetNotifier = ValueNotifier(Offset.zero);
|
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-12-03 21:47:12 +08:00
|
|
|
bool lastTunEnable = false;
|
|
|
|
|
int? lastProfileModified;
|
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-12-03 21:47:12 +08:00
|
|
|
Future<void> initCore({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
required Config config,
|
|
|
|
|
}) async {
|
|
|
|
|
await globalState.init(
|
|
|
|
|
appState: appState,
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
|
|
|
|
await applyProfile(
|
|
|
|
|
appState: appState,
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<void> updateClashConfig({
|
2024-12-03 21:47:12 +08:00
|
|
|
required AppState appState,
|
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-12-03 21:47:12 +08:00
|
|
|
final useClashConfig = clashConfig.copyWith();
|
|
|
|
|
if (clashConfig.tun.enable != lastTunEnable &&
|
|
|
|
|
lastTunEnable == false &&
|
|
|
|
|
!Platform.isAndroid) {
|
|
|
|
|
final code = await system.authorizeCore();
|
|
|
|
|
switch (code) {
|
|
|
|
|
case AuthorizeCode.none:
|
|
|
|
|
break;
|
|
|
|
|
case AuthorizeCode.success:
|
|
|
|
|
lastTunEnable = useClashConfig.tun.enable;
|
|
|
|
|
await restartCore(
|
|
|
|
|
appState: appState,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
config: config,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
case AuthorizeCode.error:
|
|
|
|
|
useClashConfig.tun = useClashConfig.tun.copyWith(
|
|
|
|
|
enable: false,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (config.appSetting.openLogs) {
|
|
|
|
|
clashCore.startLog();
|
|
|
|
|
} else {
|
|
|
|
|
clashCore.stopLog();
|
|
|
|
|
}
|
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-12-03 21:47:12 +08:00
|
|
|
config: useClashConfig,
|
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-09-26 14:29:04 +08:00
|
|
|
testUrl: config.appSetting.testUrl,
|
2024-07-02 08:08:31 +08:00
|
|
|
),
|
2024-06-29 21:42:00 +08:00
|
|
|
),
|
|
|
|
|
);
|
2024-06-03 18:02:05 +08:00
|
|
|
if (res.isNotEmpty) throw res;
|
2024-12-03 21:47:12 +08:00
|
|
|
lastTunEnable = useClashConfig.tun.enable;
|
|
|
|
|
lastProfileModified = await config.getCurrentProfile()?.profileLastModified;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-27 16:59:23 +08:00
|
|
|
handleStart() async {
|
2024-12-03 21:47:12 +08:00
|
|
|
await clashCore.startListener();
|
2024-09-18 10:27:53 +08:00
|
|
|
if (globalState.isVpnService) {
|
2024-09-26 14:29:04 +08:00
|
|
|
await vpn?.startVpn();
|
2024-08-15 16:18:00 +08:00
|
|
|
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-12-03 21:47:12 +08:00
|
|
|
restartCore({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
required Config config,
|
|
|
|
|
bool isPatch = true,
|
|
|
|
|
}) async {
|
|
|
|
|
await clashService?.startCore();
|
|
|
|
|
await initCore(
|
|
|
|
|
appState: appState,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
config: config,
|
|
|
|
|
);
|
|
|
|
|
if (isStart) {
|
|
|
|
|
await handleStart();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 16:18:00 +08:00
|
|
|
updateStartTime() {
|
2024-12-03 21:47:12 +08:00
|
|
|
startTime = clashLib?.getRunTime();
|
2024-08-15 16:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 14:32:57 +08:00
|
|
|
Future handleStop() async {
|
2024-08-15 16:18:00 +08:00
|
|
|
startTime = null;
|
2024-12-03 21:47:12 +08:00
|
|
|
await clashCore.stopListener();
|
|
|
|
|
clashLib?.stopTun();
|
|
|
|
|
await service?.destroy();
|
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-11-09 20:17:57 +08:00
|
|
|
clashCore.requestGc();
|
2024-06-03 18:02:05 +08:00
|
|
|
await updateClashConfig(
|
2024-12-03 21:47:12 +08:00
|
|
|
appState: appState,
|
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 {
|
2024-12-03 21:47:12 +08:00
|
|
|
appState.isInit = await clashCore.isInit;
|
2024-04-30 23:38:49 +08:00
|
|
|
if (!appState.isInit) {
|
2024-12-03 21:47:12 +08:00
|
|
|
appState.isInit = await clashCore.init(
|
2024-04-30 23:38:49 +08:00
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
2024-12-03 21:47:12 +08:00
|
|
|
clashLib?.setState(
|
|
|
|
|
CoreState(
|
|
|
|
|
enable: config.vpnProps.enable,
|
|
|
|
|
accessControl: config.isAccessControl ? config.accessControl : null,
|
|
|
|
|
ipv6: config.vpnProps.ipv6,
|
|
|
|
|
allowBypass: config.vpnProps.allowBypass,
|
|
|
|
|
systemProxy: config.vpnProps.systemProxy,
|
|
|
|
|
onlyProxy: config.appSetting.onlyProxy,
|
|
|
|
|
bypassDomain: config.networkProps.bypassDomain,
|
|
|
|
|
routeAddress: clashConfig.routeAddress,
|
|
|
|
|
currentProfileName:
|
|
|
|
|
config.currentProfile?.label ?? config.currentProfileId ?? "",
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
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-07-24 01:27:49 +08:00
|
|
|
changeProxy({
|
|
|
|
|
required Config config,
|
|
|
|
|
required String groupName,
|
|
|
|
|
required String proxyName,
|
2024-12-03 21:47:12 +08:00
|
|
|
}) async {
|
|
|
|
|
await clashCore.changeProxy(
|
2024-07-24 01:27:49 +08:00
|
|
|
ChangeProxyParams(
|
|
|
|
|
groupName: groupName,
|
|
|
|
|
proxyName: proxyName,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-09-26 14:29:04 +08:00
|
|
|
if (config.appSetting.closeConnections) {
|
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,
|
2024-09-08 21:21:21 +08:00
|
|
|
bool dismissible = true,
|
2024-04-30 23:38:49 +08:00
|
|
|
}) async {
|
|
|
|
|
return await showModal<T>(
|
|
|
|
|
context: navigatorKey.currentState!.context,
|
2024-09-08 21:21:21 +08:00
|
|
|
configuration: FadeScaleTransitionConfiguration(
|
2024-04-30 23:38:49 +08:00
|
|
|
barrierColor: Colors.black38,
|
2024-09-08 21:21:21 +08:00
|
|
|
barrierDismissible: dismissible,
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
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({
|
2024-12-03 21:47:12 +08:00
|
|
|
required Config config,
|
2024-09-18 10:27:53 +08:00
|
|
|
AppFlowingState? appFlowingState,
|
2024-12-03 21:47:12 +08:00
|
|
|
}) async {
|
|
|
|
|
final onlyProxy = config.appSetting.onlyProxy;
|
|
|
|
|
final traffic = await clashCore.getTraffic(onlyProxy);
|
2024-07-13 16:36:08 +08:00
|
|
|
if (Platform.isAndroid && isVpnService == true) {
|
2024-08-15 16:18:00 +08:00
|
|
|
vpn?.startForeground(
|
2024-12-03 21:47:12 +08:00
|
|
|
title: clashLib?.getCurrentProfileName() ?? "",
|
2024-04-30 23:38:49 +08:00
|
|
|
content: "$traffic",
|
|
|
|
|
);
|
2024-07-13 16:36:08 +08:00
|
|
|
} else {
|
2024-09-18 10:27:53 +08:00
|
|
|
if (appFlowingState != null) {
|
|
|
|
|
appFlowingState.addTraffic(traffic);
|
2024-12-03 21:47:12 +08:00
|
|
|
appFlowingState.totalTraffic =
|
|
|
|
|
await clashCore.getTotalTraffic(onlyProxy);
|
2024-07-13 16:36:08 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
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,
|
2025-01-09 18:38:52 +08:00
|
|
|
bool silence = true,
|
2024-06-03 18:02:05 +08:00
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final res = await futureFunction();
|
|
|
|
|
return res;
|
|
|
|
|
} catch (e) {
|
2025-01-09 18:38:52 +08:00
|
|
|
if (silence) {
|
|
|
|
|
showNotifier(e.toString());
|
|
|
|
|
} else {
|
|
|
|
|
showMessage(
|
|
|
|
|
title: title ?? appLocalizations.tip,
|
|
|
|
|
message: TextSpan(
|
|
|
|
|
text: e.toString(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-03 18:02:05 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-21 21:51:56 +08:00
|
|
|
|
2024-12-09 01:40:39 +08:00
|
|
|
showNotifier(String text) {
|
|
|
|
|
navigatorKey.currentContext?.showNotifier(text);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|