agosh commited on
Commit
7ee4127
·
verified ·
1 Parent(s): 4f4e3e8

Update s.js

Browse files
Files changed (1) hide show
  1. s.js +12 -14
s.js CHANGED
@@ -5,40 +5,37 @@ const http = require("http");
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
 
22
- // Cria rotas fixas
23
  for (const port of activePorts) {
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)) {
31
  return res.status(400).send("Porta inválida");
32
  }
33
- if (!activePorts.has(port)) {
34
- activePorts.add(port);
35
- app.use(`/s${port}`, makeProxy(port));
36
- console.log(`Nova porta registrada: ${port}`);
37
- }
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,12 +43,13 @@ app.get("/", (req, res) => {
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 {
 
5
  const app = express();
6
  const server = http.createServer(app);
7
 
8
+ // Portas fixas + dinâmicas
9
  const activePorts = new Set(["8080", "8000", "5000"]);
10
 
11
+ // Função que cria proxy
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: "warn", // pode mudar para "debug" se quiser rastrear
19
  });
20
  }
21
 
22
+ // Cria rotas fixas de imediato
23
  for (const port of activePorts) {
24
  app.use(`/s${port}`, makeProxy(port));
25
  }
26
 
27
+ // Middleware para rotas dinâmicas
28
  app.use("/s:port", (req, res, next) => {
29
  const port = req.params.port;
30
  if (!/^\d+$/.test(port)) {
31
  return res.status(400).send("Porta inválida");
32
  }
33
+ activePorts.add(port); // registra para aparecer no /
34
+ // 🔥 cria o proxy e chama imediatamente
35
+ return makeProxy(port)(req, res, next);
 
 
 
36
  });
37
 
38
+ // Página inicial: lista portas ativas
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
  res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
44
  });
45
 
46
+ // 🔥 Suporte explícito a upgrade WS
47
  server.on("upgrade", (req, socket, head) => {
48
  const url = req.url || "";
49
  const match = url.match(/^\/s(\d+)/);
50
  if (match) {
51
  const port = match[1];
52
+ activePorts.add(port); // registra também se for WS
53
  const proxy = makeProxy(port);
54
  proxy.upgrade(req, socket, head);
55
  } else {