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

@ -13,22 +13,36 @@ function startDockerStatsInterval(ws, client, user, docker) {
console.log(`Skipping docker stats interval: ${user === 'Unknown' ? 'User unknown' : 'Not subscribed to 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 } }));
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` }));
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);