-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5afdbd
commit a0d49b7
Showing
14 changed files
with
277 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* This file is part of SudoBot. | ||
* | ||
* Copyright (C) 2021-2024 OSN Developers. | ||
* | ||
* SudoBot is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* SudoBot is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
export type StringLike = { toString: () => string } | string; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import Shell from "@main/shell/core/Shell"; | ||
import axios, { AxiosError } from "axios"; | ||
import chalk from "chalk"; | ||
|
||
async function main() { | ||
if (!process.env.SYSTEM_SHELL_KEY) { | ||
throw new Error("Environment variable SYSTEM_SHELL_KEY is not defined"); | ||
} | ||
|
||
const shell = new Shell(); | ||
|
||
await shell.awaitReady(); | ||
|
||
for await (const input of shell) { | ||
if (input.startsWith("$")) { | ||
if (input.slice(1).trim().length === 0) { | ||
console.error(`${chalk.red.bold("sbsh:")} expected raw shell command after "$"`); | ||
continue; | ||
} | ||
|
||
try { | ||
shell.executeCommand(input.slice(1)); | ||
|
||
await new Promise<void>(resolve => { | ||
const handler = (message: MessageEvent) => { | ||
const { type, payload } = JSON.parse(message.data.toString("utf-8")); | ||
|
||
if (type === "exit" || type === "signal") { | ||
const code = +payload; | ||
shell.ws.removeEventListener("message", handler); | ||
shell.setExitCode(isNaN(code) ? 128 : code); | ||
resolve(); | ||
} | ||
}; | ||
|
||
shell.ws.addEventListener("message", handler); | ||
}); | ||
} catch (error) { | ||
if (error instanceof Error && error.name === "AbortError") { | ||
console.error( | ||
`${chalk.red.bold("sbsh:")} ${chalk.white.bold("SIGINT")} received, aborting command execution` | ||
); | ||
continue; | ||
} | ||
|
||
if (error instanceof AxiosError) { | ||
console.error( | ||
`${chalk.red.bold("sbsh:")} ${error.response?.data.shortError ?? error.response?.data.error ?? error?.message}` | ||
); | ||
|
||
shell.setExitCode(error.response?.data.code ?? 1); | ||
continue; | ||
} | ||
|
||
console.error(error); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
const [command, ...args] = input.split(/\s+/); | ||
|
||
if (shell.handleBuiltInCommands(command, args)) { | ||
continue; | ||
} | ||
|
||
try { | ||
const { data } = await axios.post( | ||
`${process.env.SYSTEM_API_URL}/shell/command`, | ||
{ | ||
command, | ||
args | ||
}, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${process.env.SYSTEM_SHELL_KEY}` | ||
} | ||
} | ||
); | ||
|
||
if (data.output !== null) { | ||
console.log(data.output); | ||
} | ||
|
||
shell.setExitCode(0); | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
console.error( | ||
`${chalk.red.bold("sbsh:")} ${error.response?.data.error ?? error?.message}` | ||
); | ||
|
||
shell.setExitCode(error.response?.data.code ?? 1); | ||
continue; | ||
} | ||
|
||
console.error(error); | ||
} | ||
} | ||
} | ||
|
||
main().then(); |
16 changes: 16 additions & 0 deletions
16
src/main/typescript/shell/commands/AdminTestShellCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ShellCommand from "@main/shell/core/ShellCommand"; | ||
import type { ShellCommandContext } from "@main/shell/core/ShellCommandContext"; | ||
|
||
class AdminTestShellCommand extends ShellCommand { | ||
public override readonly name: string = "admintest"; | ||
|
||
public override async execute(context: ShellCommandContext): Promise<void> { | ||
if (context.elevatedPrivileges) { | ||
context.println("Looks good! You have elevated privileges."); | ||
} else { | ||
context.println("You do not have elevated privileges."); | ||
} | ||
} | ||
} | ||
|
||
export default AdminTestShellCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ShellCommand from "@main/shell/core/ShellCommand"; | ||
import type { ShellCommandContext } from "@main/shell/core/ShellCommandContext"; | ||
|
||
class RebootCommand extends ShellCommand { | ||
public override readonly name: string = "reboot"; | ||
public override readonly aliases: string[] = ["restart"]; | ||
|
||
public override async execute(context: ShellCommandContext): Promise<unknown> { | ||
if (!context.elevatedPrivileges) { | ||
return { | ||
code: 1, | ||
error: "reboot: Operation not permitted\nreboot: You may need elevated privileges to perform this action." | ||
}; | ||
} | ||
|
||
context.println("Rebooting in 5 seconds. You will lose connection to the shell."); | ||
setTimeout(() => this.application.service("startupManager").requestRestart(), 5000); | ||
} | ||
} | ||
|
||
export default RebootCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ShellCommand from "@main/shell/core/ShellCommand"; | ||
import type { ShellCommandContext } from "@main/shell/core/ShellCommandContext"; | ||
|
||
class SudoShellCommand extends ShellCommand { | ||
public override readonly name: string = "sudo"; | ||
|
||
public override async execute(context: ShellCommandContext): Promise<unknown> { | ||
if (!context.args[0]) { | ||
return { code: 1, error: "Usage: sudo <command>" }; | ||
} | ||
|
||
const service = this.application.service("shellService"); | ||
|
||
return await service.simpleExecute(context.args[0], { | ||
elevatedPrivileges: true, | ||
args: context.args.slice(1) | ||
}); | ||
} | ||
} | ||
|
||
export default SudoShellCommand; |
Oops, something went wrong.