Fix profile update bug

This commit is contained in:
problematicconsumer
2023-09-22 23:52:20 +03:30
parent 9cd4fe48bd
commit 92538d137b
11 changed files with 73 additions and 32 deletions

View File

@@ -24,10 +24,12 @@ AppDatabase appDatabase(AppDatabaseRef ref) => AppDatabase.connect();
SharedPreferences sharedPreferences(SharedPreferencesRef ref) =>
throw UnimplementedError('sharedPreferences must be overridden');
// TODO: set options for dio
@Riverpod(keepAlive: true)
Dio dio(DioRef ref) => Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 15),
sendTimeout: const Duration(seconds: 15),
receiveTimeout: const Duration(seconds: 15),
headers: {
"User-Agent": ref.watch(appInfoProvider).userAgent,
},

View File

@@ -57,11 +57,15 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
}
@override
TaskEither<CoreServiceFailure, Unit> parseConfig(String path) {
TaskEither<CoreServiceFailure, Unit> parseConfig(
String path,
String tempPath,
bool debug,
) {
return exceptionHandler(
() {
return singbox
.parseConfig(path)
.parseConfig(path, tempPath, debug)
.mapLeft(CoreServiceFailure.invalidConfig)
.run();
},

View File

@@ -198,21 +198,27 @@ class ProfilesRepositoryImpl
) {
return TaskEither(
() async {
final tempPath = filesEditor.configPath("temp_$fileName");
final path = filesEditor.configPath(fileName);
final response = await dio.download(url.trim(), path);
final headers = await _populateHeaders(response.headers.map, path);
final parseResult = await singbox.parseConfig(path).run();
return parseResult.fold(
(l) async {
await File(path).delete();
loggy.warning("error parsing config: $l");
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) async {
final profile = Profile.fromResponse(url, headers);
return right(profile);
},
);
try {
final response = await dio.download(url.trim(), tempPath);
final headers =
await _populateHeaders(response.headers.map, tempPath);
final parseResult =
await singbox.parseConfig(path, tempPath, false).run();
return parseResult.fold(
(l) async {
loggy.warning("error parsing config: $l");
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) async {
final profile = Profile.fromResponse(url, headers);
return right(profile);
},
);
} finally {
if (await File(tempPath).exists()) await File(tempPath).delete();
}
},
);
}

View File

@@ -8,7 +8,11 @@ import 'package:hiddify/domain/singbox/outbounds.dart';
abstract interface class SingboxFacade {
TaskEither<CoreServiceFailure, Unit> setup();
TaskEither<CoreServiceFailure, Unit> parseConfig(String path);
TaskEither<CoreServiceFailure, Unit> parseConfig(
String path,
String tempPath,
bool debug,
);
TaskEither<CoreServiceFailure, Unit> changeConfigOptions(
ConfigOptions options,

View File

@@ -895,17 +895,23 @@ class SingboxNativeLibrary {
ffi.Pointer<ffi.Char> parse(
ffi.Pointer<ffi.Char> path,
ffi.Pointer<ffi.Char> tempPath,
int debug,
) {
return _parse(
path,
tempPath,
debug,
);
}
late final _parsePtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>>('parse');
late final _parse = _parsePtr
.asFunction<ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>();
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Char>, ffi.Pointer<ffi.Char>, GoUint8)>>('parse');
late final _parse = _parsePtr.asFunction<
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Char>, ffi.Pointer<ffi.Char>, int)>();
ffi.Pointer<ffi.Char> changeConfigOptions(
ffi.Pointer<ffi.Char> configOptionsJson,
@@ -1099,6 +1105,7 @@ final class GoSlice extends ffi.Struct {
typedef GoInt = GoInt64;
typedef GoInt64 = ffi.LongLong;
typedef GoUint8 = ffi.UnsignedChar;
const int _VCRT_COMPILER_PREPROCESSOR = 1;

View File

@@ -84,12 +84,20 @@ class FFISingboxService
}
@override
TaskEither<String, Unit> parseConfig(String path) {
TaskEither<String, Unit> parseConfig(
String path,
String tempPath,
bool debug,
) {
return TaskEither(
() => CombineWorker().execute(
() {
final err = _box
.parse(path.toNativeUtf8().cast())
.parse(
path.toNativeUtf8().cast(),
tempPath.toNativeUtf8().cast(),
debug ? 1 : 0,
)
.cast<Utf8>()
.toDartString();
if (err.isNotEmpty) {

View File

@@ -42,12 +42,16 @@ class MobileSingboxService
TaskEither.of(unit);
@override
TaskEither<String, Unit> parseConfig(String path) {
TaskEither<String, Unit> parseConfig(
String path,
String tempPath,
bool debug,
) {
return TaskEither(
() async {
final message = await _methodChannel.invokeMethod<String>(
"parse_config",
{"path": path},
{"path": path, "tempPath": tempPath, "debug": debug},
);
if (message == null || message.isEmpty) return right(unit);
return left(message);

View File

@@ -24,7 +24,11 @@ abstract interface class SingboxService {
String tempDir,
);
TaskEither<String, Unit> parseConfig(String path);
TaskEither<String, Unit> parseConfig(
String path,
String tempPath,
bool debug,
);
TaskEither<String, Unit> changeConfigOptions(ConfigOptions options);