Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
ma4z-sys committed Jan 3, 2025
1 parent c10e592 commit e5cd348
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
37 changes: 36 additions & 1 deletion routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const { v4: uuidv4 } = require('uuid');
const bcrypt = require('bcrypt');
const WebSocket = require('ws');
const axios = require('axios');
const { v4: uuid } = require('uuid');
const { sendPasswordResetEmail } = require('../handlers/email.js');
Expand Down Expand Up @@ -77,7 +78,7 @@ router.post('/api/getUser', validateApiKey, async (req, res) => {

router.get('/api/auth/create-user', validateApiKey, async (req, res) => {
try {
let { username, email, password, userId, admin } = req.query; // Use 'let' instead of 'const'
let { username, email, password, userId, admin } = req.query;

if (!username || !email || !password) {
return res.status(400).json({ error: 'Username and password are required' });
Expand Down Expand Up @@ -153,6 +154,40 @@ router.get('/api/instances', validateApiKey, async (req, res) => {
}
});

router.ws("/api/instance/console/:id", async (ws, req) => {
if (!req.user) return ws.close(1008, "Authorization required");

const { id } = req.params;
const instance = await db.get(id + '_instance');

if (!instance || !id) return ws.close(1008, "Invalid instance or ID");

const node = instance.Node;
const socket = new WebSocket(`ws://${node.address}:${node.port}/exec/${instance.ContainerId}`);

socket.onopen = () => {
socket.send(JSON.stringify({ "event": "auth", "args": [node.apiKey] }));
};

socket.onmessage = msg => {
ws.send(msg.data);
};

socket.onerror = (error) => {
ws.send('\x1b[31;1mHydraDaemon instance appears to be down')
};

socket.onclose = (event) => {};

ws.onmessage = msg => {
socket.send(msg.data);
};

ws.on('close', () => {
socket.close();
});
});

router.post('/api/instances/deploy', validateApiKey, async (req, res) => {
const { image, imagename, memory, cpu, ports, nodeId, name, user, primary, variables } =
req.body;
Expand Down
11 changes: 7 additions & 4 deletions views/instance/instance.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,12 @@ const eulaPopupHTML = `
command: '<%= instance.imageData.StopCommand %>' || 'stop',
containerId: instanceId // Replace with your actual instanceId variable
}));
} else {
} else if (action === 'restart') {
ws.send(JSON.stringify({
event: `power:restart`,
containerId: instanceId // Replace with your actual instanceId variable
}));
} {
ws.send(JSON.stringify({
event: `power:${action}`,
containerId: instanceId
Expand Down Expand Up @@ -494,13 +499,11 @@ const eulaPopupHTML = `
ws.onmessage = handleWebSocketMessage;
ws.onclose = () => {
console.log('WebSocket connection closed, attempting to reconnect...');
showAlert('error', 'Connection Lost', 'Unable to Connect to WebSocket Please check your internet')
setTimeout(initWebSocket, 1);
};
ws.onerror = (error) => {
console.log('WebSocket encountered an error:', error, 'Attempting to reconnect...');
showAlert('error', 'Connection Lost', 'Unable to Connect to WebSocket Please check your internet')
setTimeout(initWebSocket, 5000);
setTimeout(initWebSocket, 1);
};
}
Expand Down

0 comments on commit e5cd348

Please sign in to comment.