|
@@ -16,6 +16,7 @@ final Map<String, dynamic> _defaultConfig = {
|
|
|
'startAtLogin': false,
|
|
|
'breakConnections': false,
|
|
|
'language': 'zh_CN',
|
|
|
+ 'port': 9899,
|
|
|
'subs': [],
|
|
|
};
|
|
|
|
|
@@ -27,8 +28,10 @@ class ConfigController extends GetxController {
|
|
|
var clashCoreApiSecret = ''.obs;
|
|
|
var clashCoreDns = ''.obs;
|
|
|
var clashCoreTunEnable = false.obs;
|
|
|
-
|
|
|
+ var servicePort = 0.obs;
|
|
|
Future<void> initConfig() async {
|
|
|
+
|
|
|
+ var port = await getFreePort();
|
|
|
//dio.addSentry();
|
|
|
dio = Dio(BaseOptions(baseUrl: clashCoreApiAddress.value));
|
|
|
if (!await Paths.config.exists()) await Paths.config.create(recursive: true);
|
|
@@ -43,6 +46,8 @@ class ConfigController extends GetxController {
|
|
|
} else {
|
|
|
config.value = Config.fromJson(_defaultConfig);
|
|
|
}
|
|
|
+ config.value.port = port;
|
|
|
+
|
|
|
if (config.value.subs.isEmpty) {
|
|
|
if (!await Files.configExample.exists()) await Files.assetsExample.copy(Files.configExample.path);
|
|
|
config.value.subs.add(ConfigSub(name: 'example.yaml', url: '', updateTime: 0));
|
|
@@ -119,8 +124,19 @@ class ConfigController extends GetxController {
|
|
|
sub.updateTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
|
sub.info = null;
|
|
|
if (subInfo != null) {
|
|
|
- final info = Map.fromEntries(
|
|
|
- subInfo.first.split(RegExp(r';\s*')).where((s) => s.isNotEmpty).map((e) => e.split('=')).map((e) => MapEntry(e[0], int.parse(e[1]))));
|
|
|
+ // final info = Map.fromEntries(
|
|
|
+ // subInfo.first.split(RegExp(r';\s*')).where((s) => s.isNotEmpty).map((e) => e.split('=')).map((e) => MapEntry(e[0], int.parse(e[1]))));
|
|
|
+ final entries = subInfo.first.split(RegExp(r';\s*'))
|
|
|
+ .where((s) => s.isNotEmpty)
|
|
|
+ .map((e) => e.split('='))
|
|
|
+ .toList();
|
|
|
+
|
|
|
+ final info = <String, dynamic>{};
|
|
|
+
|
|
|
+ for (var entry in entries) {
|
|
|
+ info[entry[0]] = int.parse(entry[1]);
|
|
|
+ }
|
|
|
+
|
|
|
sub.info = ConfigSubInfo.fromJson(info);
|
|
|
}
|
|
|
await setSub(sub.name, sub);
|
|
@@ -159,4 +175,12 @@ class ConfigController extends GetxController {
|
|
|
await save();
|
|
|
config.refresh();
|
|
|
}
|
|
|
+
|
|
|
+ Future<int> getFreePort() async {
|
|
|
+ var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
|
+ int port = server.port;
|
|
|
+ await server.close();
|
|
|
+ return port;
|
|
|
+ }
|
|
|
+
|
|
|
}
|