#112 new: check hash during upgrade

This commit is contained in:
lollipopkit
2023-08-08 16:31:28 +08:00
parent 693eef8f7e
commit 159942de95
12 changed files with 79 additions and 47 deletions

View File

@@ -6,6 +6,8 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:logging/logging.dart';
import 'package:r_upgrade/r_upgrade.dart';
import 'package:toolbox/core/extension/navigator.dart';
import 'package:toolbox/core/utils/misc.dart' hide pathJoin;
import 'package:toolbox/data/model/app/update.dart';
import 'package:toolbox/data/res/path.dart';
import '../data/provider/app.dart';
@@ -16,6 +18,7 @@ import 'utils/platform.dart';
import 'utils/ui.dart';
final _logger = Logger('UPDATE');
late final String _dlDir;
Future<bool> isFileAvailable(String url) async {
try {
@@ -28,7 +31,7 @@ Future<bool> isFileAvailable(String url) async {
}
Future<void> doUpdate(BuildContext context, {bool force = false}) async {
_rmDownloadApks();
await _rmDownloadApks();
final update = await locator<AppService>().getUpdate();
@@ -68,7 +71,7 @@ Future<void> doUpdate(BuildContext context, {bool force = false}) async {
child: Text(s.updateTipTooLow(newest)),
actions: [
TextButton(
onPressed: () => _doUpdate(url, context, s),
onPressed: () => _doUpdate(update, context, s),
child: Text(s.ok),
)
],
@@ -80,17 +83,36 @@ Future<void> doUpdate(BuildContext context, {bool force = false}) async {
context,
'${s.updateTip(newest)} \n${update.changelog.current}',
s.update,
() => _doUpdate(url, context, s),
() => _doUpdate(update, context, s),
);
}
Future<void> _doUpdate(String url, BuildContext context, S s) async {
Future<void> _doUpdate(AppUpdate update, BuildContext context, S s) async {
if (isAndroid) {
await RUpgrade.upgrade(
final url = update.url.current;
if (url == null) return;
final fileName = url.split('/').last;
final id = await RUpgrade.upgrade(
url,
fileName: url.split('/').last,
isAutoRequestInstall: true,
fileName: fileName,
isAutoRequestInstall: false,
);
if (id == null) {
showSnackBar(context, const Text('install id is null'));
return;
}
final sha256 = update.sha256.current;
if (sha256 == null) {
showSnackBar(context, const Text('sha256 is null'));
return;
}
final dlPath = pathJoin(_dlDir, fileName);
final computed = await getFileSha256(dlPath);
if (computed != sha256) {
showSnackBar(context, Text('Mismatch sha256: $computed, $sha256'));
return;
}
RUpgrade.install(id);
} else if (isIOS) {
await RUpgrade.upgradeFromAppStore('1586449703');
} else {
@@ -110,7 +132,8 @@ Future<void> _doUpdate(String url, BuildContext context, S s) async {
// rmdir Download
Future<void> _rmDownloadApks() async {
if (!isAndroid) return;
final dlDir = Directory(pathJoin((await docDir).path, 'Download'));
_dlDir = pathJoin((await docDir).path, 'Download');
final dlDir = Directory(_dlDir);
if (await dlDir.exists()) {
await dlDir.delete(recursive: true);
}

View File

@@ -1,4 +1,5 @@
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
@@ -77,3 +78,12 @@ String getTime(int? unixMill) {
String pathJoin(String path1, String path2) {
return path1 + (path1.endsWith('/') ? '' : '/') + path2;
}
Future<String?> getFileSha256(String path) async {
final file = File(path);
if (!(await file.exists())) {
return null;
}
final digest = await sha256.bind(file.openRead()).first;
return digest.toString();
}

View File

@@ -75,12 +75,14 @@ esac''');
}
}
abstract class _CmdType {
extension EnumX on Enum {
/// Find out the required segment from [segments]
String find(List<String> segments);
String find(List<String> segments) {
return segments[index];
}
}
enum StatusCmdType implements _CmdType {
enum StatusCmdType {
net,
sys,
cpu,
@@ -92,21 +94,11 @@ enum StatusCmdType implements _CmdType {
tempVal,
host,
sysRhel;
@override
String find(List<String> segments) {
return segments[index];
}
}
enum DockerCmdType implements _CmdType {
enum DockerCmdType {
version,
ps,
stats,
images;
@override
String find(List<String> segments) {
return segments[index];
}
}

View File

@@ -34,11 +34,13 @@ class AppUpdate {
required this.changelog,
required this.build,
required this.url,
required this.sha256,
});
final AppUpdatePlatformSpecific<String> changelog;
final Build build;
final AppUpdatePlatformSpecific<String> url;
final AppUpdatePlatformSpecific<String?> sha256;
factory AppUpdate.fromRawJson(String str) =>
AppUpdate.fromJson(json.decode(str));
@@ -49,12 +51,14 @@ class AppUpdate {
changelog: AppUpdatePlatformSpecific.fromJson(json["changelog"]),
build: Build.fromJson(json["build"]),
url: AppUpdatePlatformSpecific.fromJson(json["url"]),
sha256: AppUpdatePlatformSpecific.fromJson(json["sha256"]),
);
Map<String, dynamic> toJson() => {
"changelog": changelog.toJson(),
"build": build.toJson(),
"url": url.toJson(),
"sha256": sha256.toJson(),
};
}

View File

@@ -2,8 +2,8 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 451;
static const int build = 452;
static const String engine = "3.10.6";
static const String buildAt = "2023-08-08 15:07:48.571249";
static const int modifications = 3;
static const String buildAt = "2023-08-08 16:29:00.513310";
static const int modifications = 11;
}

View File

@@ -32,7 +32,7 @@ final shellCmd = """
# Script for app `${BuildData.name} v1.0.${BuildData.build}`
# Delete this file while app is running will cause app crash
export LANG=en_US.utf-8
export LANG="en_US.UTF-8"
${AppShellFuncType.shellScript}
""";

View File

@@ -479,7 +479,9 @@ class _ServerPageState extends State<ServerPage>
final file = File(path);
final shouldGenKey = spi.pubKeyId != null;
if (shouldGenKey) {
await file.delete();
if (await file.exists()) {
await file.delete();
}
await file.writeAsString(getPrivateKey(spi.pubKeyId!));
extraArgs.addAll(["-i", path]);
}