Files
MWClash/lib/main.dart

185 lines
4.8 KiB
Dart
Raw Normal View History

2024-04-30 23:38:49 +08:00
import 'dart:async';
import 'dart:io';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/clash/clash.dart';
import 'package:fl_clash/plugins/app.dart';
import 'package:fl_clash/plugins/tile.dart';
import 'package:fl_clash/plugins/vpn.dart';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/state.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
2024-04-30 23:38:49 +08:00
import 'application.dart';
import 'common/common.dart';
2024-04-30 23:38:49 +08:00
import 'l10n/l10n.dart';
import 'models/models.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
clashLib?.initMessage();
globalState.packageInfo = await PackageInfo.fromPlatform();
final version = await system.version;
2024-04-30 23:38:49 +08:00
final config = await preferences.getConfig() ?? Config();
await AppLocalizations.load(
other.getLocaleForString(config.appSetting.locale) ??
WidgetsBinding.instance.platformDispatcher.locale,
);
2024-04-30 23:38:49 +08:00
final clashConfig = await preferences.getClashConfig() ?? ClashConfig();
await android?.init();
await window?.init(config.windowProps, version);
2024-05-07 13:50:00 +08:00
final appState = AppState(
mode: clashConfig.mode,
version: version,
selectedMap: config.currentSelectedMap,
2024-05-07 13:50:00 +08:00
);
final appFlowingState = AppFlowingState();
appState.navigationItems = navigation.getItems(
openLogs: config.appSetting.openLogs,
hasProxies: false,
2024-04-30 23:38:49 +08:00
);
tray.update(
appState: appState,
appFlowingState: appFlowingState,
config: config,
clashConfig: clashConfig,
);
HttpOverrides.global = FlClashHttpOverrides();
2024-04-30 23:38:49 +08:00
runAppWithPreferences(
const Application(),
appState: appState,
appFlowingState: appFlowingState,
2024-04-30 23:38:49 +08:00
config: config,
clashConfig: clashConfig,
);
}
@pragma('vm:entry-point')
Future<void> vpnService() async {
WidgetsFlutterBinding.ensureInitialized();
2024-07-13 16:36:08 +08:00
globalState.isVpnService = true;
globalState.packageInfo = await PackageInfo.fromPlatform();
final version = await system.version;
2024-04-30 23:38:49 +08:00
final config = await preferences.getConfig() ?? Config();
final clashConfig = await preferences.getClashConfig() ?? ClashConfig();
await AppLocalizations.load(
other.getLocaleForString(config.appSetting.locale) ??
WidgetsBinding.instance.platformDispatcher.locale,
);
2024-05-07 13:50:00 +08:00
final appState = AppState(
mode: clashConfig.mode,
selectedMap: config.currentSelectedMap,
version: version,
2024-05-07 13:50:00 +08:00
);
2024-07-13 16:36:08 +08:00
await globalState.init(
appState: appState,
config: config,
clashConfig: clashConfig,
);
await app?.tip(appLocalizations.startVpn);
globalState
.updateClashConfig(
appState: appState,
clashConfig: clashConfig,
config: config,
isPatch: false,
)
.then(
(_) async {
await globalState.handleStart();
tile?.addListener(
TileListenerWithVpn(
onStop: () async {
await app?.tip(appLocalizations.stopVpn);
await globalState.handleStop();
clashCore.shutdown();
exit(0);
},
),
);
globalState.updateTraffic(config: config);
globalState.updateFunctionLists = [
() {
globalState.updateTraffic(config: config);
}
];
},
);
vpn?.setServiceMessageHandler(
2024-07-13 16:36:08 +08:00
ServiceMessageHandler(
onProtect: (Fd fd) async {
await vpn?.setProtect(fd.value);
clashLib?.setFdMap(fd.id);
},
onProcess: (ProcessData process) async {
final packageName = await vpn?.resolverProcess(process);
clashLib?.setProcessMap(
2024-07-13 16:36:08 +08:00
ProcessMapItem(
id: process.id,
value: packageName ?? "",
),
);
},
onLoaded: (String groupName) {
final currentSelectedMap = config.currentSelectedMap;
final proxyName = currentSelectedMap[groupName];
if (proxyName == null) return;
globalState.changeProxy(
config: config,
groupName: groupName,
proxyName: proxyName,
);
},
),
);
2024-07-13 16:36:08 +08:00
}
2024-04-30 23:38:49 +08:00
2024-07-13 16:36:08 +08:00
@immutable
class ServiceMessageHandler with ServiceMessageListener {
final Function(Fd fd) _onProtect;
final Function(ProcessData process) _onProcess;
2024-08-05 19:25:35 +08:00
final Function(String providerName) _onLoaded;
2024-07-13 16:36:08 +08:00
const ServiceMessageHandler({
required Function(Fd fd) onProtect,
required Function(ProcessData process) onProcess,
2024-08-05 19:25:35 +08:00
required Function(String providerName) onLoaded,
}) : _onProtect = onProtect,
2024-07-13 16:36:08 +08:00
_onProcess = onProcess,
_onLoaded = onLoaded;
2024-04-30 23:38:49 +08:00
@override
2024-07-13 16:36:08 +08:00
onProtect(Fd fd) {
_onProtect(fd);
}
@override
onProcess(ProcessData process) {
2024-07-13 16:36:08 +08:00
_onProcess(process);
}
@override
2024-08-05 19:25:35 +08:00
onLoaded(String providerName) {
_onLoaded(providerName);
}
2024-04-30 23:38:49 +08:00
}
2024-07-15 16:14:19 +08:00
@immutable
2024-04-30 23:38:49 +08:00
class TileListenerWithVpn with TileListener {
final Function() _onStop;
2024-07-15 16:14:19 +08:00
const TileListenerWithVpn({
2024-04-30 23:38:49 +08:00
required Function() onStop,
}) : _onStop = onStop;
@override
void onStop() {
_onStop();
}
}