rollback: write script to /dev/shm (#474)

This commit is contained in:
lollipopkit🏳️‍⚧️
2024-07-21 17:58:14 +08:00
committed by GitHub
parent b0936c5e6e
commit 255abe8b11
25 changed files with 226 additions and 211 deletions

View File

@@ -19,43 +19,24 @@ enum ShellFunc {
/// srvboxm -> ServerBox Mobile
static const scriptFile = 'srvboxm_v${BuildData.script}.sh';
static const scriptPathShm = '/dev/shm/$scriptFile';
static const scriptPathHome = '~/.config/server_box/$scriptFile';
static const scriptDir = '~/.config/server_box';
static const scriptPath = '$scriptDir/$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
static const String installShellCmd = """
cat > $scriptPath
chmod 744 $scriptPath
""";
}
String get flag {
switch (this) {
case ShellFunc.status:
return 's';
// case ShellFunc.docker:
// return 'd';
case ShellFunc.process:
return 'p';
case ShellFunc.shutdown:
return 'sd';
case ShellFunc.reboot:
return 'r';
case ShellFunc.suspend:
return 'sp';
}
}
String get flag => switch (this) {
ShellFunc.process => 'p',
ShellFunc.shutdown => 'sd',
ShellFunc.reboot => 'r',
ShellFunc.suspend => 'sp',
ShellFunc.status => 's',
// ShellFunc.docker=> 'd',
};
String exec(String id) => 'sh ${getScriptPath(id)} -$flag';
String get exec => 'sh $scriptPath -$flag';
String get name {
switch (this) {

View File

@@ -315,23 +315,14 @@ class ServerProvider extends ChangeNotifier {
final scriptRaw = ShellFunc.allScript(spi.custom?.cmds).uint8List;
Future<void> fn(String scriptPath) async {
try {
await s.client?.runForOutput(
scriptPath,
ShellFunc.installShellCmd,
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());
@@ -343,10 +334,11 @@ class ServerProvider extends ChangeNotifier {
_setServerState(s, ServerConn.failed);
return;
} catch (e) {
final err = e.toString();
TryLimiter.inc(sid);
s.status.err = SSHErr(type: SSHErrType.auth, message: e.toString());
s.status.err = SSHErr(type: SSHErrType.writeScript, message: err);
_setServerState(s, ServerConn.failed);
Loggers.app.warning('Write script to ${spi.name} by shell', e);
Loggers.app.warning('Write script to ${spi.name} by shell', err);
}
}
@@ -363,7 +355,7 @@ class ServerProvider extends ChangeNotifier {
String? raw;
try {
raw = await s.client?.run(ShellFunc.status.exec(spi.id)).string;
raw = await s.client?.run(ShellFunc.status.exec).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()) {

View File

@@ -2,6 +2,6 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 1018;
static const int script = 51;
static const int build = 1021;
static const int script = 53;
}