fix comments

This commit is contained in:
SrGooglo 2025-04-01 01:00:11 +00:00
parent ba6e99f72a
commit 50b5ddbf2f

View File

@ -1,6 +1,6 @@
import httpProxy from "http-proxy" import httpProxy from "http-proxy"
import defaults from "linebridge/dist/defaults" import defaults from "linebridge/dist/defaults"
import pkg from "../../../package.json" // Ajustado la ruta para que coincida con tu estructura import pkg from "../../../package.json"
import http from "node:http" import http from "node:http"
import https from "node:https" import https from "node:https"
@ -32,19 +32,19 @@ export default class Proxy {
this.routes = new Map() this.routes = new Map()
this.config = config this.config = config
// Crear servidor HTTP // Create HTTP server
this.server = getHttpServerEngine({}, this.handleRequest.bind(this)) this.server = getHttpServerEngine({}, this.handleRequest.bind(this))
// Manejar upgrades de WebSocket // Handle WebSocket upgrades
this.server.on("upgrade", this.handleUpgrade.bind(this)) this.server.on("upgrade", this.handleUpgrade.bind(this))
// Crear una única instancia de proxy que se reutilizará // Create a single proxy instance that will be reused
this.proxyServer = httpProxy.createProxyServer({ this.proxyServer = httpProxy.createProxyServer({
changeOrigin: true, changeOrigin: true,
xfwd: true, xfwd: true,
}) })
// Manejar errores del proxy // Handle proxy errors
this.proxyServer.on("error", (err, req, res) => { this.proxyServer.on("error", (err, req, res) => {
console.error("Proxy error:", err) console.error("Proxy error:", err)
if (res && !res.headersSent) { if (res && !res.headersSent) {
@ -108,7 +108,7 @@ export default class Proxy {
} }
initialize = async () => { initialize = async () => {
// No es necesario inicializar nada, el servidor ya está configurado // No initialization needed, the server is already configured
return true return true
} }
@ -142,12 +142,12 @@ export default class Proxy {
} }
applyConfiguration = async () => { applyConfiguration = async () => {
// No se necesita aplicar configuración específica // No specific configuration needs to be applied
return true return true
} }
reload = async () => { reload = async () => {
// No es necesario recargar nada // No need to reload anything
return true return true
} }
@ -190,14 +190,14 @@ export default class Proxy {
handleRequest = (req, res) => { handleRequest = (req, res) => {
this.setCorsHeaders(res) this.setCorsHeaders(res)
// Si es una solicitud OPTIONS, responder inmediatamente // If it's an OPTIONS request, respond immediately
if (req.method === "OPTIONS") { if (req.method === "OPTIONS") {
res.statusCode = 204 res.statusCode = 204
res.end() res.end()
return return
} }
// Responder a solicitudes raíz // Respond to root requests
if (req.url === "/") { if (req.url === "/") {
res.setHeader("Content-Type", "application/json") res.setHeader("Content-Type", "application/json")
res.end( res.end(
@ -210,7 +210,7 @@ export default class Proxy {
return return
} }
// Encontrar la ruta para esta solicitud // Find the route for this request
const route = this.findRouteForPath(req.url) const route = this.findRouteForPath(req.url)
if (!route) { if (!route) {
@ -227,7 +227,7 @@ export default class Proxy {
return return
} }
// Preparar opciones de proxy // Prepare proxy options
const proxyOptions = { const proxyOptions = {
target: route.target, target: route.target,
changeOrigin: true, changeOrigin: true,
@ -237,14 +237,14 @@ export default class Proxy {
// save original url // save original url
req.originalUrl = req.url req.originalUrl = req.url
// Aplicar reescritura de ruta si está configurada // Apply path rewriting if configured
if (route.pathRewrite) { if (route.pathRewrite) {
req.url = this.rewritePath(route.pathRewrite, req.url) req.url = this.rewritePath(route.pathRewrite, req.url)
} else { } else {
req.url = this.rewritePath({ [`^${route.path}`]: "" }, req.url) req.url = this.rewritePath({ [`^${route.path}`]: "" }, req.url)
} }
// Agregar encabezados personalizados // Add custom headers
this.proxyServer.on("proxyReq", (proxyReq, req, res, options) => { this.proxyServer.on("proxyReq", (proxyReq, req, res, options) => {
proxyReq.setHeader("x-linebridge-version", pkg.version) proxyReq.setHeader("x-linebridge-version", pkg.version)
proxyReq.setHeader( proxyReq.setHeader(
@ -258,7 +258,7 @@ export default class Proxy {
) )
}) })
// Proxy la solicitud // Proxy the request
this.proxyServer.web(req, res, proxyOptions, (err) => { this.proxyServer.web(req, res, proxyOptions, (err) => {
if (err) { if (err) {
console.error("Proxy error:", err) console.error("Proxy error:", err)
@ -277,7 +277,7 @@ export default class Proxy {
} }
handleUpgrade = (req, socket, head) => { handleUpgrade = (req, socket, head) => {
// Encontrar la ruta para esta conexión WebSocket // Find the route for this WebSocket connection
const route = this.findRouteForPath(req.url) const route = this.findRouteForPath(req.url)
if (!route) { if (!route) {
@ -290,21 +290,21 @@ export default class Proxy {
// save original url // save original url
req.originalUrl = req.url req.originalUrl = req.url
// Aplicar reescritura de ruta si está configurada // Apply path rewriting if configured
if (route.pathRewrite) { if (route.pathRewrite) {
req.url = this.rewritePath(route.pathRewrite, req.url) req.url = this.rewritePath(route.pathRewrite, req.url)
} else { } else {
req.url = this.rewritePath({ [`^${route.path}`]: "" }, req.url) req.url = this.rewritePath({ [`^${route.path}`]: "" }, req.url)
} }
// Crear un objeto de opciones de proxy específico para WebSocket // Create WebSocket-specific proxy options
const wsProxyOptions = { const wsProxyOptions = {
target: route.target, target: route.target,
ws: true, ws: true,
changeOrigin: true, changeOrigin: true,
} }
// Proxy la conexión WebSocket // Proxy the WebSocket connection
this.proxyServer.ws(req, socket, head, wsProxyOptions, (err) => { this.proxyServer.ws(req, socket, head, wsProxyOptions, (err) => {
if (err) { if (err) {
console.error("WebSocket proxy error:", err) console.error("WebSocket proxy error:", err)