Update s.js
Browse files
s.js
CHANGED
|
@@ -3,29 +3,44 @@ const { createProxyMiddleware } = require("http-proxy-middleware");
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
if (!/^\d+$/.test(port)) {
|
| 10 |
-
return
|
| 11 |
}
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return createProxyMiddleware({
|
| 17 |
-
target,
|
| 18 |
changeOrigin: true,
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
});
|
| 24 |
|
|
|
|
| 25 |
app.get("/", (req, res) => {
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
});
|
| 28 |
|
| 29 |
app.listen(7860, () => {
|
| 30 |
-
console.log("Servidor proxy rodando em http://localhost:7860");
|
| 31 |
});
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
|
| 6 |
+
// Guarda as portas acessadas
|
| 7 |
+
const activePorts = new Set();
|
| 8 |
|
| 9 |
+
// Função que cria as configs do proxy
|
| 10 |
+
function dynamicProxy(req) {
|
| 11 |
+
const port = req.params.port;
|
| 12 |
if (!/^\d+$/.test(port)) {
|
| 13 |
+
return null;
|
| 14 |
}
|
| 15 |
+
activePorts.add(port); // ✅ registra a porta usada
|
| 16 |
+
return {
|
| 17 |
+
target: `http://localhost:${port}`,
|
|
|
|
|
|
|
|
|
|
| 18 |
changeOrigin: true,
|
| 19 |
+
ws: true, // suporte a WebSocket
|
| 20 |
+
pathRewrite: { [`^/s${port}`]: "" },
|
| 21 |
+
};
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// Middleware dinâmico: /sPORTA -> localhost:PORTA
|
| 25 |
+
app.use("/s:port", (req, res, next) => {
|
| 26 |
+
const opts = dynamicProxy(req);
|
| 27 |
+
if (!opts) {
|
| 28 |
+
return res.status(400).send("Porta inválida");
|
| 29 |
+
}
|
| 30 |
+
return createProxyMiddleware(opts)(req, res, next);
|
| 31 |
});
|
| 32 |
|
| 33 |
+
// Página inicial: lista portas já acessadas
|
| 34 |
app.get("/", (req, res) => {
|
| 35 |
+
if (activePorts.size === 0) {
|
| 36 |
+
return res.send("Nenhuma porta ativa ainda. Acesse /sPORTA (ex: /s8080).");
|
| 37 |
+
}
|
| 38 |
+
const list = Array.from(activePorts)
|
| 39 |
+
.map(p => `<li><a href="/s${p}" target="_blank">/s${p}</a></li>`)
|
| 40 |
+
.join("");
|
| 41 |
+
res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
|
| 42 |
});
|
| 43 |
|
| 44 |
app.listen(7860, () => {
|
| 45 |
+
console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
|
| 46 |
});
|