diff --git a/lib/data/model/app/shell_func.dart b/lib/data/model/app/shell_func.dart index 809c841b..88360693 100644 --- a/lib/data/model/app/shell_func.dart +++ b/lib/data/model/app/shell_func.dart @@ -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 = {}; + 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) { diff --git a/lib/data/provider/server.dart b/lib/data/provider/server.dart index 85714f88..7b6798db 100644 --- a/lib/data/provider/server.dart +++ b/lib/data/provider/server.dart @@ -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 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 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> runSnippetsMulti( - // List ids, - // Snippet snippet, - // ) async { - // return await Future.wait(ids.map((id) async => runSnippet(id, snippet))); - // } } diff --git a/lib/main.dart b/lib/main.dart index 5830fa44..1985fd0a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -95,6 +95,12 @@ Future _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(); diff --git a/lib/view/page/process.dart b/lib/view/page/process.dart index a81ec708..e2907a95 100644 --- a/lib/view/page/process.dart +++ b/lib/view/page/process.dart @@ -51,7 +51,8 @@ class _ProcessPageState extends State { Future _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; diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index fab8d657..b1c04821 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -356,7 +356,7 @@ class _ServerPageState extends State 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 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 IconTextBtn( onPressed: () => _askFor( func: () => srv.client?.execWithPwd( - ShellFunc.reboot.exec, + ShellFunc.reboot.exec(srv.id), context: context, id: srv.id, ), diff --git a/pubspec.lock b/pubspec.lock index 72d0c36e..8b35ae6b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index c25c1883..4603efcd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: