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

Update s.js

Browse files
Files changed (1) hide show
  1. s.js +38 -18
s.js CHANGED
@@ -5,57 +5,77 @@ const http = require("http");
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 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>`)
 
41
  .join("");
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 {
55
  socket.destroy();
56
  }
57
  });
58
 
59
  server.listen(7860, "0.0.0.0", () => {
60
- console.log("Servidor proxy dinâmico rodando em http://localhost:7860");
 
61
  });
 
5
  const app = express();
6
  const server = http.createServer(app);
7
 
8
+ // Lista de portas ativas
9
+ const activePorts = new Set(["5000"]); // começa com 5000 só
10
 
11
+ // Função que cria proxy HTTP/WS
12
+ function makeProxy(port, wsOnly = false) {
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
+ // Se wsOnly for true, bloqueia requests HTTP normais
20
+ onProxyReq: (proxyReq, req, res) => {
21
+ if (wsOnly && req.method !== "GET" && req.headers.upgrade !== "websocket") {
22
+ res.writeHead(400, { "Content-Type": "text/plain" });
23
+ res.end("Somente conexões WebSocket são aceitas nesta rota");
24
+ }
25
+ }
26
  });
27
  }
28
 
29
+ // 🔒 Rotas fixas dedicadas a WS
30
+ app.use("/s8000", (req, res, next) => {
31
+ res.status(400).send("Use WebSocket (wss://) nesta rota");
32
+ });
33
+ app.use("/s8080", (req, res, next) => {
34
+ res.status(400).send("Use WebSocket (wss://) nesta rota");
35
+ });
36
 
37
+ // HTTP dinâmico para outras portas
38
  app.use("/s:port", (req, res, next) => {
39
  const port = req.params.port;
40
  if (!/^\d+$/.test(port)) {
41
  return res.status(400).send("Porta inválida");
42
  }
43
+ if (port === "8000" || port === "8080") {
44
+ return res.status(400).send("Esta porta aceita apenas WebSocket (wss://)");
45
+ }
46
  activePorts.add(port);
47
  return makeProxy(port)(req, res, next);
48
  });
49
 
50
+ // Página inicial
51
  app.get("/", (req, res) => {
52
  const list = Array.from(activePorts)
53
+ .concat(["8000 (WS only)", "8080 (WS only)"])
54
+ .map(p => `<li><a href="/s${p.replace(' (WS only)','')}" target="_blank">/s${p}</a></li>`)
55
  .join("");
56
  res.send(`<h1>Portas ativas</h1><ul>${list}</ul>`);
57
  });
58
 
59
+ // 🔥 Upgrade WS (inclui 8000 e 8080)
60
  server.on("upgrade", (req, socket, head) => {
61
  const url = req.url || "";
62
  const match = url.match(/^\/s(\d+)/);
63
  if (match) {
64
  const port = match[1];
65
+ if (port === "8000" || port === "8080") {
66
+ console.log(`Conexão WebSocket em /s${port}`);
67
+ makeProxy(port, true).upgrade(req, socket, head);
68
+ } else {
69
+ activePorts.add(port);
70
+ console.log(`Conexão WebSocket em /s${port}`);
71
+ makeProxy(port).upgrade(req, socket, head);
72
+ }
73
  } else {
74
  socket.destroy();
75
  }
76
  });
77
 
78
  server.listen(7860, "0.0.0.0", () => {
79
+ console.log("Proxy ativo em http://localhost:7860");
80
+ console.log("Portas WS dedicadas: 8000, 8080");
81
  });