agosh commited on
Commit
7d21ec1
·
verified ·
1 Parent(s): c56283d

Update s.js

Browse files
Files changed (1) hide show
  1. s.js +30 -15
s.js CHANGED
@@ -3,29 +3,44 @@ const { createProxyMiddleware } = require("http-proxy-middleware");
3
 
4
  const app = express();
5
 
6
- app.use("/s:port", (req, res, next) => {
7
- const port = req.params.port;
8
 
 
 
 
9
  if (!/^\d+$/.test(port)) {
10
- return res.status(400).send("Porta inválida");
11
  }
12
-
13
- const target = `http://localhost:${port}`;
14
- console.log(`Proxying ${req.originalUrl} -> ${target}`);
15
-
16
- return createProxyMiddleware({
17
- target,
18
  changeOrigin: true,
19
- pathRewrite: {
20
- [`^/s${port}`]: ""
21
- }
22
- })(req, res, next);
 
 
 
 
 
 
 
 
23
  });
24
 
 
25
  app.get("/", (req, res) => {
26
- res.send("Proxy dinâmico ativo! Use /sPORTA (ex: /s8080).");
 
 
 
 
 
 
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
  });