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

View File

@ -622,10 +622,16 @@ document.addEventListener('DOMContentLoaded', () => {
} }
function updateSftpCacheUI(message) { function updateSftpCacheUI(message) {
console.log(message)
if (message.data?.hostname && message.data?.port && message.data?.user && message.data?.password) { 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 = { sftpCredentials = {
hostname: message.data.hostname, hostname: message.data.ipAddress,
port: message.data.port, port: 22,
user: message.data.user, user: message.data.user,
password: message.data.password password: message.data.password
}; };
@ -1562,10 +1568,11 @@ document.addEventListener('DOMContentLoaded', () => {
} }
async function connectSftp() { async function connectSftp() {
if (!isSftpOnline) { // Only enable this if we are using Jump Box based SFTP Connections
showNotification('SFTP is offline. Please try again later.', 'error', 'sftp-offline'); // if (!isSftpOnline) {
return; // showNotification('SFTP is offline. Please try again later.', 'error', 'sftp-offline');
} // return;
// }
if (!sftpCredentials) { if (!sftpCredentials) {
showNotification('SFTP credentials not available.', 'error', 'sftp-credentials'); showNotification('SFTP credentials not available.', 'error', 'sftp-credentials');