Add dpaste for overflow

This commit is contained in:
dlinux-host 2024-10-02 18:50:33 -04:00
parent 35e4c48991
commit 440fd75f13
2 changed files with 121 additions and 82 deletions

View File

@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"dependencies": { "dependencies": {
"cmd-promise": "^1.2.0",
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"jsonfile": "^6.1.0", "jsonfile": "^6.1.0",
"mysql2": "^3.11.3", "mysql2": "^3.11.3",

View File

@ -4,7 +4,8 @@ import unirest from 'unirest';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import mysql from 'mysql2'; import mysql from 'mysql2';
const userWorkingDirectories = new Map(); const userWorkingDirectories = new Map();
import fs from 'fs';
import cmd from 'cmd-promise';
let sshSurfID; // Variable to store the user ID from the database let sshSurfID; // Variable to store the user ID from the database
// Paths to config and tokens files // Paths to config and tokens files
@ -316,89 +317,126 @@ client.on('interactionCreate', async interaction => {
case 'x': { case 'x': {
const command = interaction.options.getString('command'); const command = interaction.options.getString('command');
// Get the user's current working directory or default to root (/) // Get the user's current working directory or default to root (/)
let userPWD = userWorkingDirectories.get(sshSurfID) || '/'; let userPWD = userWorkingDirectories.get(sshSurfID) || '/';
// Handle 'cd' command logic // Handle 'cd' command logic
if (command.startsWith('cd')) { if (command.startsWith('cd')) {
let argscmd = command.replace('cd ', '').trim(); let argscmd = command.replace('cd ', '').trim();
// Handle 'cd ..' for going up one directory // Handle 'cd ..' for going up one directory
if (argscmd === '..') { if (argscmd === '..') {
if (userPWD !== '/') { if (userPWD !== '/') {
// Remove the last part of the current path // Remove the last part of the current path
const newPWD = userPWD.split('/').slice(0, -1).join('/') || '/'; const newPWD = userPWD.split('/').slice(0, -1).join('/') || '/';
userPWD = newPWD; userPWD = newPWD;
userWorkingDirectories.set(sshSurfID, newPWD); userWorkingDirectories.set(sshSurfID, newPWD);
await interaction.editReply(`Directory changed to: ${newPWD}`); await interaction.editReply(`Directory changed to: ${newPWD}`);
} else { } else {
await interaction.editReply(`Already at the root directory: ${userPWD}`); await interaction.editReply(`Already at the root directory: ${userPWD}`);
} }
return; return;
} }
// Handle '~' for home directory // Handle '~' for home directory
if (argscmd === '~') { if (argscmd === '~') {
userPWD = '/root'; userPWD = '/root';
userWorkingDirectories.set(sshSurfID, userPWD); userWorkingDirectories.set(sshSurfID, userPWD);
await interaction.editReply(`Directory changed to: ${userPWD}`); await interaction.editReply(`Directory changed to: ${userPWD}`);
return; return;
} }
// Handle absolute and relative paths // Handle absolute and relative paths
let newPWD; let newPWD;
if (argscmd.startsWith('/')) { if (argscmd.startsWith('/')) {
// Absolute path // Absolute path
newPWD = argscmd; newPWD = argscmd;
} else { } else {
// Relative path // Relative path
newPWD = `${userPWD}/${argscmd}`; newPWD = `${userPWD}/${argscmd}`;
} }
// Normalize the path (remove extra slashes) // Normalize the path (remove extra slashes)
newPWD = newPWD.replace(/([^:]\/)\/+/g, "$1"); newPWD = newPWD.replace(/([^:]\/)\/+/g, "$1");
// Check if the user is trying to go back multiple directories (e.g., 'cd ../../') // Check if the user is trying to go back multiple directories (e.g., 'cd ../../')
if (argscmd.includes('../')) { if (argscmd.includes('../')) {
const numDirsBack = argscmd.split('../').length - 1; const numDirsBack = argscmd.split('../').length - 1;
newPWD = RemoveLastDirectoryPartOf(userPWD, numDirsBack); newPWD = RemoveLastDirectoryPartOf(userPWD, numDirsBack);
} }
// Update the working directory // Update the working directory
userWorkingDirectories.set(sshSurfID, newPWD); userWorkingDirectories.set(sshSurfID, newPWD);
await interaction.editReply(`Directory changed to: ${newPWD}`); await interaction.editReply(`Directory changed to: ${newPWD}`);
return; return;
} }
// If the command is not 'cd', run the command in the current working directory (or default to '/') // If the command is not 'cd', run the command in the current working directory (or default to '/')
const execResponse = await makeApiRequest('/exec', apiToken, interaction, 'post', { const execResponse = await makeApiRequest('/exec', apiToken, interaction, 'post', {
cmd: command, cmd: command,
pwd: userPWD // Use the current directory or default to '/' pwd: userPWD // Use the current directory or default to '/'
}); });
// Format the command output in a markdown code block // Handle the command output
let replyMessage = `\`\`\`\n${execResponse.stdout || 'No output'}\n\`\`\``; if (execResponse.stdout.length > 2020) {
// If the output is too large, save it to a temporary file and upload to a paste service
// If there is an error, append the error message in another markdown code block
if (execResponse.stderr && execResponse.stderr.trim()) {
replyMessage += `\n**Error:**\n\`\`\`\n${execResponse.stderr}\n\`\`\``;
}
// Reply with the formatted message const tempFilePath = '/tmp/paste';
await interaction.editReply(replyMessage); const pasteCommand = `Command: ${command} | Container Owner: ${interaction.user.username}\n${execResponse.stdout}`;
break;
} fs.writeFileSync(tempFilePath, pasteCommand, (err) => {
if (err) {
console.error(err);
return;
}
});
const pasteout = await cmd("sleep 2; cat /tmp/paste | dpaste");
// Create an embed with the paste link
const mainEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(`Container Owner: ${interaction.user.username}`)
.setDescription(`The command: ${command} was too large for Discord.`)
.addFields(
{
name: 'Please check the below output log:',
value: pasteout.stdout.replace("Pro tip: you can password protect your paste just by typing a username and password after your paste command.", "")
.replace("Paste Saved: ", "")
.replace("-------------------------------------------------------", "")
}
)
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: `${interaction.user.displayAvatarURL()}` });
await interaction.editReply({ embeds: [mainEmbed] });
} else {
// If the output is small enough, format it in a markdown code block
let replyMessage = `\`\`\`\n${execResponse.stdout || 'No output'}\n\`\`\``;
// If there is an error, append the error message in another markdown code block
if (execResponse.stderr && execResponse.stderr.trim()) {
replyMessage += `\n**Error:**\n\`\`\`\n${execResponse.stderr}\n\`\`\``;
}
// Reply with the formatted message
await interaction.editReply(replyMessage);
}
break;
}
// Helper function to remove directories when using '../'
function RemoveLastDirectoryPartOf(the_url, num) {
var the_arr = the_url.split('/');
the_arr.splice(-num, num);
return the_arr.join('/') || '/';
}
// Helper function to remove directories when using '../'
function RemoveLastDirectoryPartOf(the_url, num) {
var the_arr = the_url.split('/');
the_arr.splice(-num, num);
return the_arr.join('/') || '/';
}
case 'notify': case 'notify':