Files
vpn/lib/services/singbox_runtime.dart
2026-04-20 01:34:03 +03:00

58 lines
1.6 KiB
Dart

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
final class SingBoxRuntime {
static const String _linuxAssetPath = 'assets/sing-box/linux-amd64/sing-box';
String? _cachedBinaryPath;
Future<String> ensureBinary() async {
if (_cachedBinaryPath != null) {
final cachedFile = File(_cachedBinaryPath!);
if (await cachedFile.exists()) {
return _cachedBinaryPath!;
}
}
if (!Platform.isLinux) {
throw UnsupportedError('Sing-box runtime currently supports Linux only');
}
final appDir = await getApplicationSupportDirectory();
final runtimeDir = Directory(
'${appDir.path}${Platform.pathSeparator}sing-box-runtime',
);
if (!await runtimeDir.exists()) {
await runtimeDir.create(recursive: true);
}
final binaryPath =
'${runtimeDir.path}${Platform.pathSeparator}sing-box';
final binaryFile = File(binaryPath);
final ByteData byteData = await rootBundle.load(_linuxAssetPath);
await binaryFile.writeAsBytes(
byteData.buffer.asUint8List(),
flush: true,
);
await Process.run('chmod', <String>['755', binaryPath]);
_cachedBinaryPath = binaryPath;
return binaryPath;
}
Future<String> configPath() async {
final appDir = await getApplicationSupportDirectory();
final runtimeDir = Directory(
'${appDir.path}${Platform.pathSeparator}sing-box-runtime',
);
if (!await runtimeDir.exists()) {
await runtimeDir.create(recursive: true);
}
return '${runtimeDir.path}${Platform.pathSeparator}config.json';
}
}