add warp config, update to flutter 1.22

This commit is contained in:
hiddify-com
2024-05-31 13:41:35 +02:00
parent 218c6d49bc
commit e2b67c317b
21 changed files with 423 additions and 385 deletions

View File

@@ -4,7 +4,6 @@ import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:combine/combine.dart';
import 'package:ffi/ffi.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/core/model/directories.dart';
@@ -52,10 +51,7 @@ class FFISingboxService with InfraLogger implements SingboxService {
Future<void> init() async {
loggy.debug("initializing");
_statusReceiver = ReceivePort('service status receiver');
final source = _statusReceiver
.asBroadcastStream()
.map((event) => jsonDecode(event as String))
.map(SingboxStatus.fromEvent);
final source = _statusReceiver.asBroadcastStream().map((event) => jsonDecode(event as String)).map(SingboxStatus.fromEvent);
_status = ValueConnectableStream.seeded(
source,
const SingboxStopped(),
@@ -69,8 +65,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
) {
final port = _statusReceiver.sendPort.nativePort;
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
_box.setupOnce(NativeApi.initializeApiDLData);
final err = _box
.setup(
@@ -98,8 +94,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
bool debug,
) {
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final err = _box
.parse(
path.toNativeUtf8().cast(),
@@ -120,13 +116,10 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
TaskEither<String, Unit> changeOptions(SingboxConfigOption options) {
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final json = jsonEncode(options.toJson());
final err = _box
.changeConfigOptions(json.toNativeUtf8().cast())
.cast<Utf8>()
.toDartString();
final err = _box.changeConfigOptions(json.toNativeUtf8().cast()).cast<Utf8>().toDartString();
if (err.isNotEmpty) {
return left(err);
}
@@ -141,8 +134,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
String path,
) {
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final response = _box
.generateConfig(
path.toNativeUtf8().cast(),
@@ -166,8 +159,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
) {
loggy.debug("starting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final err = _box
.start(
configPath.toNativeUtf8().cast(),
@@ -187,8 +180,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
TaskEither<String, Unit> stop() {
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final err = _box.stop().cast<Utf8>().toDartString();
if (err.isNotEmpty) {
return left(err);
@@ -207,8 +200,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
) {
loggy.debug("restarting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final err = _box
.restart(
configPath.toNativeUtf8().cast(),
@@ -265,10 +258,7 @@ class FFISingboxService with InfraLogger implements SingboxService {
},
);
final err = _box
.startCommandClient(1, receiver.sendPort.nativePort)
.cast<Utf8>()
.toDartString();
final err = _box.startCommandClient(1, receiver.sendPort.nativePort).cast<Utf8>().toDartString();
if (err.isNotEmpty) {
loggy.error("error starting status command: $err");
throw err;
@@ -310,10 +300,7 @@ class FFISingboxService with InfraLogger implements SingboxService {
);
try {
final err = _box
.startCommandClient(5, receiver.sendPort.nativePort)
.cast<Utf8>()
.toDartString();
final err = _box.startCommandClient(5, receiver.sendPort.nativePort).cast<Utf8>().toDartString();
if (err.isNotEmpty) {
logger.error("error starting group command: $err");
throw err;
@@ -357,10 +344,7 @@ class FFISingboxService with InfraLogger implements SingboxService {
);
try {
final err = _box
.startCommandClient(13, receiver.sendPort.nativePort)
.cast<Utf8>()
.toDartString();
final err = _box.startCommandClient(13, receiver.sendPort.nativePort).cast<Utf8>().toDartString();
if (err.isNotEmpty) {
logger.error("error starting: $err");
throw err;
@@ -376,8 +360,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
TaskEither<String, Unit> selectOutbound(String groupTag, String outboundTag) {
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final err = _box
.selectOutbound(
groupTag.toNativeUtf8().cast(),
@@ -397,12 +381,9 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
TaskEither<String, Unit> urlTest(String groupTag) {
return TaskEither(
() => CombineWorker().execute(
() {
final err = _box
.urlTest(groupTag.toNativeUtf8().cast())
.cast<Utf8>()
.toDartString();
() => Future.microtask(
() async {
final err = _box.urlTest(groupTag.toNativeUtf8().cast()).cast<Utf8>().toDartString();
if (err.isNotEmpty) {
return left(err);
}
@@ -418,9 +399,7 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
Stream<List<String>> watchLogs(String path) async* {
yield await _readLogFile(File(path));
yield* Watcher(path, pollingDelay: const Duration(seconds: 1))
.events
.asyncMap((event) async {
yield* Watcher(path, pollingDelay: const Duration(seconds: 1)).events.asyncMap((event) async {
if (event.type == ChangeType.MODIFY) {
await _readLogFile(File(path));
}
@@ -431,17 +410,18 @@ class FFISingboxService with InfraLogger implements SingboxService {
@override
TaskEither<String, Unit> clearLogs() {
return TaskEither(
() async {
_logBuffer.clear();
return right(unit);
},
() => Future.microtask(
() async {
_logBuffer.clear();
return right(unit);
},
),
);
}
Future<List<String>> _readLogFile(File file) async {
if (_logFilePosition == 0 && file.lengthSync() == 0) return [];
final content =
await file.openRead(_logFilePosition).transform(utf8.decoder).join();
final content = await file.openRead(_logFilePosition).transform(utf8.decoder).join();
_logFilePosition = file.lengthSync();
final lines = const LineSplitter().convert(content);
if (lines.length > 300) {
@@ -464,8 +444,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
}) {
loggy.debug("generating warp config");
return TaskEither(
() => CombineWorker().execute(
() {
() => Future.microtask(
() async {
final response = _box
.generateWarpConfig(
licenseKey.toNativeUtf8().cast(),