fix: termux compatibility (#472)

This commit is contained in:
lollipopkit🏳️‍⚧️
2024-07-20 20:35:30 +08:00
committed by GitHub
parent 2907ac74d4
commit b0936c5e6e
7 changed files with 53 additions and 82 deletions

View File

@@ -19,11 +19,24 @@ enum ShellFunc {
/// srvboxm -> ServerBox Mobile
static const scriptFile = 'srvboxm_v${BuildData.script}.sh';
static const scriptPath = '/dev/shm/$scriptFile';
static const installShellCmd = """
cat > $scriptPath
chmod 744 $scriptPath
static const scriptPathShm = '/dev/shm/$scriptFile';
static const scriptPathHome = '~/.config/server_box/$scriptFile';
static final _scriptPathMap = <String, String>{};
static String getScriptPath(String id) {
return _scriptPathMap.putIfAbsent(id, () => scriptPathShm);
}
static String setScriptPath(String id, String path) {
return _scriptPathMap[id] = path;
}
static String installShellCmd(String id) {
final path = getScriptPath(id);
return """
cat > $path
chmod 744 $path
""";
}
String get flag {
switch (this) {
@@ -42,7 +55,7 @@ chmod 744 $scriptPath
}
}
String get exec => 'sh $scriptPath -$flag';
String exec(String id) => 'sh ${getScriptPath(id)} -$flag';
String get name {
switch (this) {

View File

@@ -1,5 +1,5 @@
import 'dart:async';
import 'dart:io';
// import 'dart:io';
import 'package:computer/computer.dart';
import 'package:dartssh2/dartssh2.dart';
@@ -10,8 +10,8 @@ import 'package:server_box/core/utils/ssh_auth.dart';
import 'package:server_box/data/model/app/error.dart';
import 'package:server_box/data/model/app/shell_func.dart';
import 'package:server_box/data/model/server/system.dart';
import 'package:server_box/data/model/sftp/req.dart';
import 'package:server_box/data/res/provider.dart';
// import 'package:server_box/data/model/sftp/req.dart';
// import 'package:server_box/data/res/provider.dart';
import 'package:server_box/data/res/store.dart';
import '../../core/utils/server.dart';
@@ -313,17 +313,25 @@ class ServerProvider extends ChangeNotifier {
_setServerState(s, ServerConn.connected);
// Write script to server
// by ssh
final scriptRaw = ShellFunc.allScript(spi.custom?.cmds).uint8List;
try {
Future<void> fn(String scriptPath) async {
await s.client?.runForOutput(
ShellFunc.installShellCmd,
scriptPath,
action: (session) async {
session.stdin.add(scriptRaw);
session.stdin.close();
},
);
ShellFunc.setScriptPath(spi.id, scriptPath);
}
try {
try {
await fn(ShellFunc.scriptPathShm);
} catch (_) {
await fn(ShellFunc.scriptPathHome);
}
} on SSHAuthAbortError catch (e) {
TryLimiter.inc(sid);
s.status.err = SSHErr(type: SSHErrType.auth, message: e.toString());
@@ -335,44 +343,10 @@ class ServerProvider extends ChangeNotifier {
_setServerState(s, ServerConn.failed);
return;
} catch (e) {
TryLimiter.inc(sid);
s.status.err = SSHErr(type: SSHErrType.auth, message: e.toString());
_setServerState(s, ServerConn.failed);
Loggers.app.warning('Write script to ${spi.name} by shell', e);
/// by sftp
final localPath = Paths.doc.joinPath('install.sh');
final file = File(localPath);
try {
file.writeAsBytes(scriptRaw);
final completer = Completer();
final homePath = (await s.client?.run('echo \$HOME').string)?.trim();
if (homePath == null || homePath.isEmpty) {
throw Exception('Got empty home path');
}
final reqId = Pros.sftp.add(
SftpReq(
spi,
ShellFunc.scriptPath,
localPath,
SftpReqType.upload,
),
completer: completer,
);
await completer.future;
final err = Pros.sftp.get(reqId)?.error;
if (err != null) {
throw err;
}
} catch (ee) {
TryLimiter.inc(sid);
s.status.err = SSHErr(
type: SSHErrType.writeScript,
message: '$e\n\n$ee',
);
_setServerState(s, ServerConn.failed);
Loggers.app.warning('Write script to ${spi.name} by sftp', ee);
return;
} finally {
if (await file.exists()) await file.delete();
}
}
}
@@ -389,7 +363,7 @@ class ServerProvider extends ChangeNotifier {
String? raw;
try {
raw = await s.client?.run(ShellFunc.status.exec).string;
raw = await s.client?.run(ShellFunc.status.exec(spi.id)).string;
segments = raw?.split(ShellFunc.seperator).map((e) => e.trim()).toList();
if (raw == null || raw.isEmpty || segments == null || segments.isEmpty) {
if (Stores.setting.keepStatusWhenErr.fetch()) {
@@ -462,26 +436,4 @@ class ServerProvider extends ChangeNotifier {
// reset try times only after prepared successfully
TryLimiter.reset(sid);
}
// Future<SnippetResult?> runSnippet(String id, Snippet snippet) async {
// final server = _servers[id];
// if (server == null) return null;
// final watch = Stopwatch()..start();
// final result = await server.client?.run(snippet.fmtWithArgs(server.spi)).string;
// final time = watch.elapsed;
// watch.stop();
// if (result == null) return null;
// return SnippetResult(
// dest: _servers[id]?.spi.name,
// result: result,
// time: time,
// );
// }
// Future<List<SnippetResult?>> runSnippetsMulti(
// List<String> ids,
// Snippet snippet,
// ) async {
// return await Future.wait(ids.map((id) async => runSnippet(id, snippet)));
// }
}

View File

@@ -95,6 +95,12 @@ Future<void> _initData() async {
Hive.registerAdapter(ServerCustomAdapter()); // 7
Hive.registerAdapter(WakeOnLanCfgAdapter()); // 8
try {
/// Apps' data on other platforms are stored in a container that prevents
/// access by other apps. Therefore, there is no need to encrypt the data.
if (isLinux || isWindows) await SecureStore.init();
} catch (_) {}
await Stores.setting.init();
await Stores.server.init();
await Stores.key.init();

View File

@@ -51,7 +51,8 @@ class _ProcessPageState extends State<ProcessPage> {
Future<void> _refresh() async {
if (mounted) {
final result = await _client?.run(ShellFunc.process.exec).string;
final result =
await _client?.run(ShellFunc.process.exec(widget.spi.id)).string;
if (result == null || result.isEmpty) {
context.showSnackBar(l10n.noResult);
return;

View File

@@ -356,7 +356,7 @@ class _ServerPageState extends State<ServerPage>
Stores.setting.showSuspendTip.put(false);
}
srv.client?.execWithPwd(
ShellFunc.suspend.exec,
ShellFunc.suspend.exec(srv.id),
context: context,
id: srv.id,
);
@@ -370,7 +370,7 @@ class _ServerPageState extends State<ServerPage>
IconTextBtn(
onPressed: () => _askFor(
func: () => srv.client?.execWithPwd(
ShellFunc.shutdown.exec,
ShellFunc.shutdown.exec(srv.id),
context: context,
id: srv.id,
),
@@ -383,7 +383,7 @@ class _ServerPageState extends State<ServerPage>
IconTextBtn(
onPressed: () => _askFor(
func: () => srv.client?.execWithPwd(
ShellFunc.reboot.exec,
ShellFunc.reboot.exec(srv.id),
context: context,
id: srv.id,
),

View File

@@ -385,8 +385,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "v1.0.72"
resolved-ref: "7b57c8e08ff199247bb174d499876627b10f86b5"
ref: "v1.0.74"
resolved-ref: "42000d49fd62ee202a4b37d0a97c29f77c83d0d8"
url: "https://github.com/lppcg/fl_lib"
source: git
version: "0.0.1"
@@ -662,10 +662,10 @@ packages:
dependency: transitive
description:
name: js
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.7.1"
version: "0.6.7"
json_annotation:
dependency: transitive
description:

View File

@@ -21,7 +21,6 @@ dependencies:
code_text_field: ^1.1.0
shared_preferences: ^2.1.1
dynamic_color: ^1.6.6
#flutter_secure_storage: ^9.0.0
xml: ^6.4.2 # for parsing nvidia-smi
flutter_displaymode: ^0.6.0
flutter_background_service: ^5.0.5
@@ -62,7 +61,7 @@ dependencies:
fl_lib:
git:
url: https://github.com/lppcg/fl_lib
ref: v1.0.72
ref: v1.0.74
dependency_overrides:
# dartssh2: