Convert SFTP Client to use local internal docker IPs for connection.

This removes the need for SFTP-Links to be online!
This commit is contained in:
MCHost
2025-06-25 02:45:37 -04:00
parent ac3542628b
commit 4c32ebae09
2 changed files with 62 additions and 15 deletions

View File

@ -14,21 +14,35 @@ function startDockerStatsInterval(ws, client, user, docker) {
return null;
}
// Send initial stats immediately
(async () => {
try {
const initialStats = await getContainerStats(docker, user);
// Get container info to retrieve IP address
const container = docker.getContainer(user);
const containerInfo = await container.inspect();
const ipAddress = containerInfo.NetworkSettings.Networks?.minecraft_network?.IPAddress || 'N/A';
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({ type: 'docker', data: { ...initialStats, user } }));
ws.send(JSON.stringify({
type: 'docker',
data: {
...initialStats,
user,
ipAddress
}
}));
}
} catch (error) {
console.error(`Error sending initial docker stats for ${user}:`, error.message);
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({ type: 'docker', error: `Failed to fetch initial stats: ${error.message}` }));
ws.send(JSON.stringify({
type: 'docker',
error: `Failed to fetch initial stats: ${error.message}`
}));
}
}
})();
// Start interval for periodic stats
// Start interval for periodic stats
const intervalId = setInterval(async () => {
try {
@ -47,11 +61,20 @@ function startDockerStatsInterval(ws, client, user, docker) {
}
const stats = await getContainerStats(docker, user);
const ipAddress = inspect.NetworkSettings.Networks?.minecraft_network?.IPAddress || 'N/A';
if (stats.error) {
console.error(`Error fetching stats for ${user}: ${stats.error}`);
ws.send(JSON.stringify({ type: 'docker', error: stats.error }));
} else {
ws.send(JSON.stringify({ type: 'docker', data: { ...stats, user } }));
ws.send(JSON.stringify({
type: 'docker',
data: {
...stats,
user,
ipAddress
}
}));
}
} catch (error) {
console.error(`Error in docker stats interval for ${user}:`, error.message);
@ -137,14 +160,30 @@ async function fetchAndSendUpdate(ws, endpoint, client, docker) {
try {
const container = docker.getContainer(client.user);
const inspect = await container.inspect();
const ipAddress = inspect.NetworkSettings.Networks?.minecraft_network?.IPAddress || 'N/A';
if (inspect.State.Status === 'running' && response.hostname && response.port) {
const status = await checkSftpStatus(response.hostname, response.port);
ws.send(JSON.stringify({ type: 'sftp-status', data: { isOnline: status.isOnline } }));
} else {
ws.send(JSON.stringify({ type: 'sftp-status', error: `Container ${client.user} is not running` }));
ws.send(JSON.stringify({
type: 'sftp-status',
data: {
isOnline: status.isOnline,
ipAddress
}
}));
} else {
ws.send(JSON.stringify({
type: 'sftp-status',
error: `Container ${client.user} is not running`,
ipAddress
}));
}
// Add IP address to my-sftp-cache response
response.ipAddress = ipAddress;
} catch (error) {
ws.send(JSON.stringify({ type: 'sftp-status', error: `Failed to check container status: ${error.message}` }));
ws.send(JSON.stringify({
type: 'sftp-status',
error: `Failed to check container status: ${error.message}`
}));
}
}
}
@ -158,6 +197,7 @@ async function fetchAndSendUpdate(ws, endpoint, client, docker) {
}
}
async function manageStatusChecks(ws, client, user, docker) {
try {
const container = docker.getContainer(user);

View File

@ -622,10 +622,16 @@ document.addEventListener('DOMContentLoaded', () => {
}
function updateSftpCacheUI(message) {
console.log(message)
if (message.data?.hostname && message.data?.port && message.data?.user && message.data?.password) {
// For testing, this is currently configured to be internally networked to port 22 for the given container.
// The IP Address is sent from server side on page load, in theory, this should allow us to always allow
// SFTP Client to WORK! Even if SFTP Holesail ports are down!
// To Revert, move hostname to message.data.hostname and port to message.data.port
// Doing so will configure the connection to the Jump node.
sftpCredentials = {
hostname: message.data.hostname,
port: message.data.port,
hostname: message.data.ipAddress,
port: 22,
user: message.data.user,
password: message.data.password
};
@ -1562,10 +1568,11 @@ document.addEventListener('DOMContentLoaded', () => {
}
async function connectSftp() {
if (!isSftpOnline) {
showNotification('SFTP is offline. Please try again later.', 'error', 'sftp-offline');
return;
}
// Only enable this if we are using Jump Box based SFTP Connections
// if (!isSftpOnline) {
// showNotification('SFTP is offline. Please try again later.', 'error', 'sftp-offline');
// return;
// }
if (!sftpCredentials) {
showNotification('SFTP credentials not available.', 'error', 'sftp-credentials');