Spaces:
Running
Running
Commit
·
aa22602
1
Parent(s):
f1f5bd3
added more status information and a button to try to get the user's name from hf whoami-v2 endpoint
Browse files- myNodeServer.js +125 -29
myNodeServer.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
* All rights reserved.
|
4 |
*/
|
5 |
//@ts-check
|
6 |
-
const VERSION = '0.0.
|
7 |
|
8 |
const express = require('express');
|
9 |
const http = require('http');
|
@@ -18,6 +18,13 @@ const CONTAINER_HOST = '127.0.0.1';
|
|
18 |
const OMNI_URL = 'http://127.0.0.1:1688'; // URL of the OMNITOOL service
|
19 |
const PROXY_PORT_OMNITOOL = 4444;
|
20 |
const CONTAINER_PORT_OMNITOOL = 1688;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
const DELAY_OMNITOOL_SET_TO_RUNNING = 2000; // 2 seconds
|
23 |
const CHECK_OMNI_INTERVAL = 60000; // 1 minute
|
@@ -32,6 +39,8 @@ global.PROTOCOL = "";
|
|
32 |
global.PROXY_STARTED = false;
|
33 |
global.logs = [];
|
34 |
global.OMNITOOL_PROXY = null;
|
|
|
|
|
35 |
|
36 |
|
37 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
@@ -55,7 +64,22 @@ const COMMON_STYLES = `
|
|
55 |
background-color: yellow;
|
56 |
color: black;
|
57 |
font-weight: bold;
|
58 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
@keyframes pulseAnimation {
|
60 |
0% { transform: scale(1); }
|
61 |
50% { transform: scale(1.1); }
|
@@ -80,7 +104,7 @@ async function startOmnitoolServer()
|
|
80 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
81 |
{
|
82 |
console.log('Omnitool server started successfully');
|
83 |
-
setOmnitoolRunning(true);
|
84 |
}
|
85 |
|
86 |
});
|
@@ -124,7 +148,7 @@ function setOmnitoolRunning(set_running)
|
|
124 |
global.OMNITOOL_RUNNING = true;
|
125 |
}, DELAY_OMNITOOL_SET_TO_RUNNING); // Delay by 2 second
|
126 |
}
|
127 |
-
|
128 |
}
|
129 |
else
|
130 |
{
|
@@ -183,7 +207,7 @@ async function handleGetStartOmnitoolServer(req, res)
|
|
183 |
async function handleGetOmnitoolLogs(req, res)
|
184 |
{
|
185 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
186 |
-
const reply = { logs: global.logs, ready:global.OMNITOOL_READY };
|
187 |
res.end(JSON.stringify(reply));
|
188 |
return;
|
189 |
}
|
@@ -218,6 +242,19 @@ async function proxyRequest(req, res)
|
|
218 |
|
219 |
function getButtonsString(buttonsHTML)
|
220 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
return `
|
223 |
<html>
|
@@ -226,9 +263,22 @@ function getButtonsString(buttonsHTML)
|
|
226 |
${COMMON_STYLES}
|
227 |
</head>
|
228 |
<body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
${buttonsHTML}
|
|
|
230 |
${LOG_CONTAINER_HTML}
|
231 |
<script>
|
|
|
|
|
232 |
function startServer() {
|
233 |
document.getElementById('startServerButton').classList.remove
|
234 |
('highlight-button');
|
@@ -273,15 +323,34 @@ function getButtonsString(buttonsHTML)
|
|
273 |
element.scrollTop = element.scrollHeight;
|
274 |
}
|
275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
startLogPolling();
|
277 |
</script>
|
278 |
</body>
|
279 |
</html>`;
|
280 |
}
|
281 |
|
|
|
282 |
async function handleGetRoot(req, res)
|
283 |
{
|
284 |
-
|
|
|
285 |
console.log(`req.session.isVisited = ${req.session.isVisited}`);
|
286 |
console.log(`global.OMNITOOL_RUNNING = ${global.OMNITOOL_RUNNING}`);
|
287 |
|
@@ -292,13 +361,16 @@ async function handleGetRoot(req, res)
|
|
292 |
|
293 |
if (!global.OMNITOOL_RUNNING)
|
294 |
{
|
295 |
-
console.log('Omnitool server is not running')
|
296 |
let startButtonClass = '';
|
|
|
297 |
if (!global.ALREADY_STARTING) startButtonClass = 'highlight-button';
|
298 |
const gotoButtonClass = '';
|
299 |
-
|
300 |
-
let buttonsHTML =
|
301 |
-
|
|
|
|
|
302 |
|
303 |
const html = getButtonsString(buttonsHTML);
|
304 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
@@ -309,7 +381,7 @@ async function handleGetRoot(req, res)
|
|
309 |
|
310 |
if (!req.session.isVisited)
|
311 |
{
|
312 |
-
console.log('First time visitor')
|
313 |
const gotoButtonClass = 'highlight-button';
|
314 |
const buttonsHTML = `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
|
315 |
const html = getButtonsString(buttonsHTML);
|
@@ -321,17 +393,36 @@ async function handleGetRoot(req, res)
|
|
321 |
|
322 |
proxyRequest(req, res);
|
323 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
function setGlobals(req)
|
325 |
{
|
326 |
-
|
327 |
global.LOCAL_URL = req.headers['host'];
|
328 |
if (req.protocol === "https") global.PROTOCOL = 'https'; else global.PROTOCOL = 'http';
|
|
|
|
|
|
|
329 |
|
330 |
-
if (!global.LOCAL_URL) throw new Error('No host header found in request' + JSON.stringify(req.headers, null, 2));
|
331 |
-
|
332 |
const hostname = global.LOCAL_URL.split(':')[0];
|
333 |
const newUrl = `${global.PROTOCOL}://${hostname}`;//:${PROXY_PORT_OMNITOOL}`;
|
334 |
-
global.PING_URL = `${newUrl}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
}
|
336 |
|
337 |
async function startServer()
|
@@ -341,18 +432,21 @@ async function startServer()
|
|
341 |
{
|
342 |
console.log(`Server running on port ${PROXY_PORT_OMNITOOL}`);
|
343 |
});
|
344 |
-
|
345 |
}
|
346 |
|
347 |
-
function omnitoolProxyMiddleware(req, res, next)
|
348 |
-
|
|
|
|
|
349 |
// Proxy all requests to OMNITOOL when conditions are met
|
350 |
return createProxyMiddleware({
|
351 |
target: OMNI_URL,
|
352 |
changeOrigin: true,
|
353 |
ws: true // if you need WebSocket support
|
354 |
})(req, res, next);
|
355 |
-
} else
|
|
|
356 |
// Continue with normal processing for other cases
|
357 |
next();
|
358 |
}
|
@@ -361,9 +455,7 @@ function omnitoolProxyMiddleware(req, res, next) {
|
|
361 |
|
362 |
async function main(app)
|
363 |
{
|
364 |
-
|
365 |
-
|
366 |
-
checkOmnitoolStatus();
|
367 |
|
368 |
// Configure session middleware
|
369 |
app.use(session({
|
@@ -380,21 +472,21 @@ async function main(app)
|
|
380 |
console.error(`There was an error starting the server: ${error}`);
|
381 |
return;
|
382 |
}
|
383 |
-
|
384 |
-
|
385 |
app.use(omnitoolProxyMiddleware);
|
386 |
|
387 |
app.get('/favicon.ico', (req, res) => res.status(204));
|
388 |
app.get('/start-omnitool-server', (req, res) => handleGetStartOmnitoolServer(req, res));
|
389 |
app.get('/omnitool-logs', (req, res) => handleGetOmnitoolLogs(req, res));
|
390 |
app.get('/burst-iframe', (req, res) => handleGetBurstIframe(req, res));
|
391 |
-
app.get('/', (req, res) =>
|
392 |
|
393 |
setInterval(async () =>
|
394 |
{
|
395 |
await checkOmnitoolStatus();
|
396 |
}, CHECK_OMNI_INTERVAL);
|
397 |
-
|
398 |
}
|
399 |
|
400 |
// Define the proxy middleware outside the function
|
@@ -404,11 +496,14 @@ global.OMNITOOL_PROXY = createProxyMiddleware({
|
|
404 |
ws: true // if you need WebSocket support
|
405 |
});
|
406 |
|
407 |
-
function omnitoolProxyMiddleware(req, res, next)
|
408 |
-
|
|
|
|
|
409 |
// Use the predefined proxy middleware
|
410 |
return global.OMNITOOL_PROXY(req, res, next);
|
411 |
-
} else
|
|
|
412 |
// Continue with normal processing for other cases
|
413 |
next();
|
414 |
}
|
@@ -416,3 +511,4 @@ function omnitoolProxyMiddleware(req, res, next) {
|
|
416 |
main(app);
|
417 |
|
418 |
|
|
|
|
3 |
* All rights reserved.
|
4 |
*/
|
5 |
//@ts-check
|
6 |
+
const VERSION = '0.0.8e';
|
7 |
|
8 |
const express = require('express');
|
9 |
const http = require('http');
|
|
|
18 |
const OMNI_URL = 'http://127.0.0.1:1688'; // URL of the OMNITOOL service
|
19 |
const PROXY_PORT_OMNITOOL = 4444;
|
20 |
const CONTAINER_PORT_OMNITOOL = 1688;
|
21 |
+
const OMNITOOL_HEALTH_PING = '/api/v1/mercenaries/ping';
|
22 |
+
|
23 |
+
|
24 |
+
const OMNITOOL_SPACE_NAME = 'omnitool_test_3';
|
25 |
+
const OMNITOOL_SPACE_OWNER = 'manu-sapiens';
|
26 |
+
const HF_SPACE_URL = `https://huggingface.co/spaces/${OMNITOOL_SPACE_OWNER}/${OMNITOOL_SPACE_NAME}`;
|
27 |
+
const HF_SPACE_DUPLICATE_URL = `${HF_SPACE_URL}?duplicate=true`;
|
28 |
|
29 |
const DELAY_OMNITOOL_SET_TO_RUNNING = 2000; // 2 seconds
|
30 |
const CHECK_OMNI_INTERVAL = 60000; // 1 minute
|
|
|
39 |
global.PROXY_STARTED = false;
|
40 |
global.logs = [];
|
41 |
global.OMNITOOL_PROXY = null;
|
42 |
+
global.CONNECTED_TO_MASTER = false;
|
43 |
+
global.SPACE_OWNER = "";
|
44 |
|
45 |
|
46 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
|
|
64 |
background-color: yellow;
|
65 |
color: black;
|
66 |
font-weight: bold;
|
67 |
+
}
|
68 |
+
.button-like-link {
|
69 |
+
display: inline-block;
|
70 |
+
background-color: #f0f0f0; /* Button color */
|
71 |
+
color: #000; /* Text color */
|
72 |
+
padding: 10px 15px;
|
73 |
+
text-align: center;
|
74 |
+
text-decoration: none;
|
75 |
+
border: 1px solid #ddd;
|
76 |
+
border-radius: 5px;
|
77 |
+
font-size: 16px;
|
78 |
+
cursor: pointer;
|
79 |
+
}
|
80 |
+
.button-like-link:hover {
|
81 |
+
background-color: #e9e9e9;
|
82 |
+
}
|
83 |
@keyframes pulseAnimation {
|
84 |
0% { transform: scale(1); }
|
85 |
50% { transform: scale(1.1); }
|
|
|
104 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
105 |
{
|
106 |
console.log('Omnitool server started successfully');
|
107 |
+
setOmnitoolRunning(true);
|
108 |
}
|
109 |
|
110 |
});
|
|
|
148 |
global.OMNITOOL_RUNNING = true;
|
149 |
}, DELAY_OMNITOOL_SET_TO_RUNNING); // Delay by 2 second
|
150 |
}
|
151 |
+
|
152 |
}
|
153 |
else
|
154 |
{
|
|
|
207 |
async function handleGetOmnitoolLogs(req, res)
|
208 |
{
|
209 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
210 |
+
const reply = { logs: global.logs, ready: global.OMNITOOL_READY };
|
211 |
res.end(JSON.stringify(reply));
|
212 |
return;
|
213 |
}
|
|
|
242 |
|
243 |
function getButtonsString(buttonsHTML)
|
244 |
{
|
245 |
+
// Add a button for the Hugging Face API call
|
246 |
+
const huggingFaceButton = '<button onclick="getHuggingFaceUserName()">Get Hugging Face User Name</button>';
|
247 |
+
|
248 |
+
|
249 |
+
// Message to display based on CONNECTED_TO_MASTER
|
250 |
+
let connectionStatusMessage;
|
251 |
+
if (global.CONNECTED_TO_MASTER)
|
252 |
+
{
|
253 |
+
connectionStatusMessage = '<div style="color: red;">You are now connected DIRECTLY to the REFERENCE Omnitool Space on Huggingface. It is HIGHLY recommended that you duplicate this space and make it private to secure your keys, recipes and outputs.</div>';
|
254 |
+
} else
|
255 |
+
{
|
256 |
+
connectionStatusMessage = `<div style="color: green;">You are now connected to the space of ${global.SPACE_OWNER}. If this is NOT YOU, it is HIGHLY recommended that you duplicate this space and make it private to secure your keys, recipes and outputs.</div><div>In addition, consider subscribing to the 20GB local storage option from Huggingface to preserve your keys and work between reboot of this server.</div>`;
|
257 |
+
}
|
258 |
|
259 |
return `
|
260 |
<html>
|
|
|
263 |
${COMMON_STYLES}
|
264 |
</head>
|
265 |
<body>
|
266 |
+
<h1>Proxy Server</h1>
|
267 |
+
<p>Version: ${VERSION}</p>
|
268 |
+
<p>Local URL: ${global.LOCAL_URL}</p>
|
269 |
+
<p>Space Owner: ${global.SPACE_OWNER}</p>
|
270 |
+
<p>Connected to master: ${global.CONNECTED_TO_MASTER}</p>
|
271 |
+
<p>Omni URL: ${OMNI_URL}</p>
|
272 |
+
<p>Omni Running: ${global.OMNITOOL_RUNNING}</p>
|
273 |
+
<p>Omni Ready: ${global.OMNITOOL_READY}</p>
|
274 |
+
|
275 |
+
${connectionStatusMessage} <!-- Display the connection status message -->
|
276 |
${buttonsHTML}
|
277 |
+
${huggingFaceButton}
|
278 |
${LOG_CONTAINER_HTML}
|
279 |
<script>
|
280 |
+
// Script to check Hugging Face login status and control 'Clone Repo' button
|
281 |
+
|
282 |
function startServer() {
|
283 |
document.getElementById('startServerButton').classList.remove
|
284 |
('highlight-button');
|
|
|
323 |
element.scrollTop = element.scrollHeight;
|
324 |
}
|
325 |
|
326 |
+
// Function to handle the Hugging Face API call
|
327 |
+
function getHuggingFaceUserName() {
|
328 |
+
fetch('https://huggingface.co/api/whoami-v2')
|
329 |
+
.then(response => response.json())
|
330 |
+
.then(data => {
|
331 |
+
if(data && data.name) {
|
332 |
+
alert('Hugging Face User Name: ' + data.name);
|
333 |
+
} else {
|
334 |
+
alert('No name field in the response.');
|
335 |
+
}
|
336 |
+
})
|
337 |
+
.catch(error => {
|
338 |
+
console.error('Error fetching Hugging Face user name:', error);
|
339 |
+
alert('Error fetching data.');
|
340 |
+
});
|
341 |
+
}
|
342 |
+
|
343 |
startLogPolling();
|
344 |
</script>
|
345 |
</body>
|
346 |
</html>`;
|
347 |
}
|
348 |
|
349 |
+
|
350 |
async function handleGetRoot(req, res)
|
351 |
{
|
352 |
+
setGlobals(req);
|
353 |
+
|
354 |
console.log(`req.session.isVisited = ${req.session.isVisited}`);
|
355 |
console.log(`global.OMNITOOL_RUNNING = ${global.OMNITOOL_RUNNING}`);
|
356 |
|
|
|
361 |
|
362 |
if (!global.OMNITOOL_RUNNING)
|
363 |
{
|
364 |
+
console.log('Omnitool server is not running');
|
365 |
let startButtonClass = '';
|
366 |
+
let cloneButtonClass = '';
|
367 |
if (!global.ALREADY_STARTING) startButtonClass = 'highlight-button';
|
368 |
const gotoButtonClass = '';
|
369 |
+
|
370 |
+
let buttonsHTML = `
|
371 |
+
<button id="startServerButton" class="${startButtonClass}" onclick="startServer()">Start Omnitool Server</button>
|
372 |
+
<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>
|
373 |
+
<a href="${HF_SPACE_DUPLICATE_URL}" target="_blank" class="button-like-link" id="duplicateRepoButton">DUPLICATE Repo</a>`;
|
374 |
|
375 |
const html = getButtonsString(buttonsHTML);
|
376 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
|
381 |
|
382 |
if (!req.session.isVisited)
|
383 |
{
|
384 |
+
console.log('First time visitor');
|
385 |
const gotoButtonClass = 'highlight-button';
|
386 |
const buttonsHTML = `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
|
387 |
const html = getButtonsString(buttonsHTML);
|
|
|
393 |
|
394 |
proxyRequest(req, res);
|
395 |
}
|
396 |
+
|
397 |
+
function extractSpaceOwner(url, spaceName)
|
398 |
+
{
|
399 |
+
const regex = new RegExp(`^(?:https?:\\/\\/)?([^\\/]*?)-${spaceName}\\.hf\\.space\\/?$`);
|
400 |
+
const match = url.match(regex);
|
401 |
+
return match ? match[1] : null;
|
402 |
+
}
|
403 |
+
|
404 |
function setGlobals(req)
|
405 |
{
|
406 |
+
console.log(`Setting global using req.headers['host'] = ${req.headers['host']}`);
|
407 |
global.LOCAL_URL = req.headers['host'];
|
408 |
if (req.protocol === "https") global.PROTOCOL = 'https'; else global.PROTOCOL = 'http';
|
409 |
+
console.log(`global.LOCAL_URL = ${global.LOCAL_URL}\nglobal.PROTOCOL = ${global.PROTOCOL}`);
|
410 |
+
|
411 |
+
if (!global.LOCAL_URL) throw new Error('No host header found in request' + JSON.stringify(req.headers, null, 2));
|
412 |
|
|
|
|
|
413 |
const hostname = global.LOCAL_URL.split(':')[0];
|
414 |
const newUrl = `${global.PROTOCOL}://${hostname}`;//:${PROXY_PORT_OMNITOOL}`;
|
415 |
+
global.PING_URL = `${newUrl}${OMNITOOL_HEALTH_PING}`;
|
416 |
+
console.log(`global.PING_URL = ${global.PING_URL}`);
|
417 |
+
|
418 |
+
// New logic to set CONNECTED_TO_MASTER
|
419 |
+
global.CONNECTED_TO_MASTER = false;
|
420 |
+
global.SPACE_OWNER = extractSpaceOwner(global.LOCAL_URL, OMNITOOL_SPACE_NAME);
|
421 |
+
console.log(`global.SPACE_OWNER = ${global.SPACE_OWNER}`);
|
422 |
+
|
423 |
+
if (global.SPACE_OWNER === OMNITOOL_SPACE_OWNER) { global.CONNECTED_TO_MASTER = true; }
|
424 |
+
console.log(`global.CONNECTED_TO_MASTER = ${global.CONNECTED_TO_MASTER}`);
|
425 |
+
|
426 |
}
|
427 |
|
428 |
async function startServer()
|
|
|
432 |
{
|
433 |
console.log(`Server running on port ${PROXY_PORT_OMNITOOL}`);
|
434 |
});
|
435 |
+
|
436 |
}
|
437 |
|
438 |
+
function omnitoolProxyMiddleware(req, res, next)
|
439 |
+
{
|
440 |
+
if (global.OMNITOOL_RUNNING && req.session.isVisited)
|
441 |
+
{
|
442 |
// Proxy all requests to OMNITOOL when conditions are met
|
443 |
return createProxyMiddleware({
|
444 |
target: OMNI_URL,
|
445 |
changeOrigin: true,
|
446 |
ws: true // if you need WebSocket support
|
447 |
})(req, res, next);
|
448 |
+
} else
|
449 |
+
{
|
450 |
// Continue with normal processing for other cases
|
451 |
next();
|
452 |
}
|
|
|
455 |
|
456 |
async function main(app)
|
457 |
{
|
458 |
+
await checkOmnitoolStatus();
|
|
|
|
|
459 |
|
460 |
// Configure session middleware
|
461 |
app.use(session({
|
|
|
472 |
console.error(`There was an error starting the server: ${error}`);
|
473 |
return;
|
474 |
}
|
475 |
+
|
476 |
+
|
477 |
app.use(omnitoolProxyMiddleware);
|
478 |
|
479 |
app.get('/favicon.ico', (req, res) => res.status(204));
|
480 |
app.get('/start-omnitool-server', (req, res) => handleGetStartOmnitoolServer(req, res));
|
481 |
app.get('/omnitool-logs', (req, res) => handleGetOmnitoolLogs(req, res));
|
482 |
app.get('/burst-iframe', (req, res) => handleGetBurstIframe(req, res));
|
483 |
+
app.get('/', (req, res) => handleGetRoot(req, res));
|
484 |
|
485 |
setInterval(async () =>
|
486 |
{
|
487 |
await checkOmnitoolStatus();
|
488 |
}, CHECK_OMNI_INTERVAL);
|
489 |
+
|
490 |
}
|
491 |
|
492 |
// Define the proxy middleware outside the function
|
|
|
496 |
ws: true // if you need WebSocket support
|
497 |
});
|
498 |
|
499 |
+
function omnitoolProxyMiddleware(req, res, next)
|
500 |
+
{
|
501 |
+
if (global.OMNITOOL_RUNNING && req.session.isVisited)
|
502 |
+
{
|
503 |
// Use the predefined proxy middleware
|
504 |
return global.OMNITOOL_PROXY(req, res, next);
|
505 |
+
} else
|
506 |
+
{
|
507 |
// Continue with normal processing for other cases
|
508 |
next();
|
509 |
}
|
|
|
511 |
main(app);
|
512 |
|
513 |
|
514 |
+
|