mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-04 09:46:40 +00:00
Add internal proxy for gerbil endpoints
This commit is contained in:
@@ -2,3 +2,4 @@ export * from "./getConfig";
|
|||||||
export * from "./receiveBandwidth";
|
export * from "./receiveBandwidth";
|
||||||
export * from "./updateHolePunch";
|
export * from "./updateHolePunch";
|
||||||
export * from "./getAllRelays";
|
export * from "./getAllRelays";
|
||||||
|
export { default as proxyRouter } from "./proxy";
|
||||||
101
server/routers/gerbil/proxy.ts
Normal file
101
server/routers/gerbil/proxy.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { Router } from "express";
|
||||||
|
import axios from "axios";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import config from "@server/lib/config";
|
||||||
|
|
||||||
|
const proxyRouter = Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proxy function that forwards requests to the remote cloud server
|
||||||
|
*/
|
||||||
|
async function proxyToRemote(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction,
|
||||||
|
endpoint: string
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const remoteConfig = config.getRawConfig().hybrid;
|
||||||
|
|
||||||
|
if (!remoteConfig?.endpoint) {
|
||||||
|
logger.error("Remote endpoint not configured in hybrid.endpoint config");
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Remote endpoint not configured"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const remoteUrl = `${remoteConfig.endpoint.replace(/\/$/, '')}/api/v1/gerbil/${endpoint}`;
|
||||||
|
|
||||||
|
logger.debug(`Proxying request to remote server: ${remoteUrl}`);
|
||||||
|
|
||||||
|
// Forward the request to the remote server
|
||||||
|
const response = await axios({
|
||||||
|
method: req.method as any,
|
||||||
|
url: remoteUrl,
|
||||||
|
data: req.body,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
params: req.query,
|
||||||
|
timeout: 30000, // 30 second timeout
|
||||||
|
validateStatus: () => true // Don't throw on non-2xx status codes
|
||||||
|
});
|
||||||
|
|
||||||
|
// Forward the response status and data
|
||||||
|
return res.status(response.status).json(response.data);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error proxying request to remote server:", error);
|
||||||
|
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
if (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND') {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.SERVICE_UNAVAILABLE,
|
||||||
|
"Remote server is unavailable"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (error.code === 'ECONNABORTED') {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.REQUEST_TIMEOUT,
|
||||||
|
"Request to remote server timed out"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Error communicating with remote server"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Proxy endpoints for each gerbil route
|
||||||
|
proxyRouter.post("/get-config", (req, res, next) =>
|
||||||
|
proxyToRemote(req, res, next, "get-config")
|
||||||
|
);
|
||||||
|
|
||||||
|
proxyRouter.post("/receive-bandwidth", (req, res, next) =>
|
||||||
|
proxyToRemote(req, res, next, "receive-bandwidth")
|
||||||
|
);
|
||||||
|
|
||||||
|
proxyRouter.post("/update-hole-punch", (req, res, next) =>
|
||||||
|
proxyToRemote(req, res, next, "update-hole-punch")
|
||||||
|
);
|
||||||
|
|
||||||
|
proxyRouter.post("/get-all-relays", (req, res, next) =>
|
||||||
|
proxyToRemote(req, res, next, "get-all-relays")
|
||||||
|
);
|
||||||
|
|
||||||
|
export default proxyRouter;
|
||||||
@@ -7,6 +7,8 @@ import * as auth from "@server/routers/auth";
|
|||||||
import * as supporterKey from "@server/routers/supporterKey";
|
import * as supporterKey from "@server/routers/supporterKey";
|
||||||
import * as license from "@server/routers/license";
|
import * as license from "@server/routers/license";
|
||||||
import * as idp from "@server/routers/idp";
|
import * as idp from "@server/routers/idp";
|
||||||
|
import proxyRouter from "@server/routers/gerbil/proxy";
|
||||||
|
import config from "@server/lib/config";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import {
|
import {
|
||||||
verifyResourceAccess,
|
verifyResourceAccess,
|
||||||
@@ -49,10 +51,16 @@ internalRouter.get("/idp/:idpId", idp.getIdp);
|
|||||||
const gerbilRouter = Router();
|
const gerbilRouter = Router();
|
||||||
internalRouter.use("/gerbil", gerbilRouter);
|
internalRouter.use("/gerbil", gerbilRouter);
|
||||||
|
|
||||||
gerbilRouter.post("/get-config", gerbil.getConfig);
|
if (config.getRawConfig().hybrid) {
|
||||||
gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth);
|
// Use proxy router to forward requests to remote cloud server
|
||||||
gerbilRouter.post("/update-hole-punch", gerbil.updateHolePunch);
|
gerbilRouter.use("/", proxyRouter);
|
||||||
gerbilRouter.post("/get-all-relays", gerbil.getAllRelays);
|
} else {
|
||||||
|
// Use local gerbil endpoints
|
||||||
|
gerbilRouter.post("/get-config", gerbil.getConfig);
|
||||||
|
gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth);
|
||||||
|
gerbilRouter.post("/update-hole-punch", gerbil.updateHolePunch);
|
||||||
|
gerbilRouter.post("/get-all-relays", gerbil.getAllRelays);
|
||||||
|
}
|
||||||
|
|
||||||
// Badger routes
|
// Badger routes
|
||||||
const badgerRouter = Router();
|
const badgerRouter = Router();
|
||||||
|
|||||||
Reference in New Issue
Block a user