Update s.js
Browse files
s.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
| 1 |
const express = require("express");
|
| 2 |
const { createProxyMiddleware } = require("http-proxy-middleware");
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
|
|
|
| 5 |
|
| 6 |
-
// Portas fixas +
|
| 7 |
const activePorts = new Set(["8080", "8000", "5000"]);
|
| 8 |
|
| 9 |
-
// Função que cria
|
| 10 |
function makeProxy(port) {
|
| 11 |
return createProxyMiddleware({
|
| 12 |
target: `http://localhost:${port}`,
|
| 13 |
changeOrigin: true,
|
| 14 |
-
ws: true, // suporte WebSocket
|
| 15 |
pathRewrite: { [`^/s${port}`]: "" },
|
|
|
|
| 16 |
});
|
| 17 |
}
|
| 18 |
|
|
@@ -21,7 +24,7 @@ for (const port of activePorts) {
|
|
| 21 |
app.use(`/s${port}`, makeProxy(port));
|
| 22 |
}
|
| 23 |
|
| 24 |
-
// Rota dinâmica:
|
| 25 |
app.use("/s:port", (req, res, next) => {
|
| 26 |
const port = req.params.port;
|
| 27 |
if (!/^\d+$/.test(port)) {
|
|
@@ -35,7 +38,7 @@ app.use("/s:port", (req, res, next) => {
|
|
| 35 |
next();
|
| 36 |
});
|
| 37 |
|
| 38 |
-
// Página inicial: lista portas
|
| 39 |
app.get("/", (req, res) => {
|
| 40 |
const list = Array.from(activePorts)
|
| 41 |
.map(p => `<li><a href="/s${p}" target="_blank">/s${p}</a></li>`)
|
|
@@ -43,6 +46,19 @@ app.get("/", (req, res) => {
|
|
| 43 |
res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
|
| 44 |
});
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
|
| 48 |
});
|
|
|
|
| 1 |
const express = require("express");
|
| 2 |
const { createProxyMiddleware } = require("http-proxy-middleware");
|
| 3 |
+
const http = require("http");
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
+
const server = http.createServer(app);
|
| 7 |
|
| 8 |
+
// Portas fixas + novas acessadas dinamicamente
|
| 9 |
const activePorts = new Set(["8080", "8000", "5000"]);
|
| 10 |
|
| 11 |
+
// Função que cria proxy com suporte HTTP + WS
|
| 12 |
function makeProxy(port) {
|
| 13 |
return createProxyMiddleware({
|
| 14 |
target: `http://localhost:${port}`,
|
| 15 |
changeOrigin: true,
|
| 16 |
+
ws: true, // 🔥 suporte a WebSocket
|
| 17 |
pathRewrite: { [`^/s${port}`]: "" },
|
| 18 |
+
logLevel: "debug", // útil para depuração
|
| 19 |
});
|
| 20 |
}
|
| 21 |
|
|
|
|
| 24 |
app.use(`/s${port}`, makeProxy(port));
|
| 25 |
}
|
| 26 |
|
| 27 |
+
// Rota dinâmica: /sPORTA ainda não registrada
|
| 28 |
app.use("/s:port", (req, res, next) => {
|
| 29 |
const port = req.params.port;
|
| 30 |
if (!/^\d+$/.test(port)) {
|
|
|
|
| 38 |
next();
|
| 39 |
});
|
| 40 |
|
| 41 |
+
// Página inicial: lista portas ativas
|
| 42 |
app.get("/", (req, res) => {
|
| 43 |
const list = Array.from(activePorts)
|
| 44 |
.map(p => `<li><a href="/s${p}" target="_blank">/s${p}</a></li>`)
|
|
|
|
| 46 |
res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
|
| 47 |
});
|
| 48 |
|
| 49 |
+
// 🔥 Suporte WS explícito
|
| 50 |
+
server.on("upgrade", (req, socket, head) => {
|
| 51 |
+
const url = req.url || "";
|
| 52 |
+
const match = url.match(/^\/s(\d+)/);
|
| 53 |
+
if (match) {
|
| 54 |
+
const port = match[1];
|
| 55 |
+
const proxy = makeProxy(port);
|
| 56 |
+
proxy.upgrade(req, socket, head);
|
| 57 |
+
} else {
|
| 58 |
+
socket.destroy();
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
server.listen(7860, () => {
|
| 63 |
console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
|
| 64 |
});
|