agosh commited on
Commit
8bd4d27
·
verified ·
1 Parent(s): 7ee4127

Update s.js

Browse files
Files changed (1) hide show
  1. s.js +11 -12
s.js CHANGED
@@ -8,34 +8,33 @@ const server = http.createServer(app);
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,13 +42,13 @@ app.get("/", (req, res) => {
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 {
@@ -57,6 +56,6 @@ server.on("upgrade", (req, socket, head) => {
57
  }
58
  });
59
 
60
- server.listen(7860, () => {
61
  console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
62
  });
 
8
  // Portas fixas + dinâmicas
9
  const activePorts = new Set(["8080", "8000", "5000"]);
10
 
11
+ // Função que cria proxy para uma porta
12
  function makeProxy(port) {
13
  return createProxyMiddleware({
14
+ target: `http://127.0.0.1:${port}`,
15
  changeOrigin: true,
16
+ ws: true, // suporte WebSocket
17
  pathRewrite: { [`^/s${port}`]: "" },
18
+ logLevel: "warn"
19
  });
20
  }
21
 
22
+ // Rotas fixas registradas logo no início
23
  for (const port of activePorts) {
24
  app.use(`/s${port}`, makeProxy(port));
25
  }
26
 
27
+ // Rotas dinâmicas HTTP
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);
 
34
  return makeProxy(port)(req, res, next);
35
  });
36
 
37
+ // Página inicial: lista portas ativas
38
  app.get("/", (req, res) => {
39
  const list = Array.from(activePorts)
40
  .map(p => `<li><a href="/s${p}" target="_blank">/s${p}</a></li>`)
 
42
  res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
43
  });
44
 
45
+ // 🔥 Tratamento de upgrade para WebSocket
46
  server.on("upgrade", (req, socket, head) => {
47
  const url = req.url || "";
48
  const match = url.match(/^\/s(\d+)/);
49
  if (match) {
50
  const port = match[1];
51
+ activePorts.add(port);
52
  const proxy = makeProxy(port);
53
  proxy.upgrade(req, socket, head);
54
  } else {
 
56
  }
57
  });
58
 
59
+ server.listen(7860, "0.0.0.0", () => {
60
  console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
61
  });