mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-17 18:36:37 +00:00
Make the proxy more general
This commit is contained in:
@@ -6,17 +6,16 @@ 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(
|
||||
|
||||
export const proxyToRemote = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
endpoint: string
|
||||
): Promise<any> {
|
||||
): Promise<any> => {
|
||||
try {
|
||||
const remoteConfig = config.getRawConfig().hybrid;
|
||||
|
||||
@@ -30,7 +29,7 @@ async function proxyToRemote(
|
||||
);
|
||||
}
|
||||
|
||||
const remoteUrl = `${remoteConfig.endpoint.replace(/\/$/, '')}/api/v1/gerbil/${endpoint}`;
|
||||
const remoteUrl = `${remoteConfig.endpoint.replace(/\/$/, '')}/api/v1/${endpoint}`;
|
||||
|
||||
logger.debug(`Proxying request to remote server: ${remoteUrl}`);
|
||||
|
||||
@@ -79,23 +78,4 @@ async function proxyToRemote(
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from "./getConfig";
|
||||
export * from "./receiveBandwidth";
|
||||
export * from "./updateHolePunch";
|
||||
export * from "./getAllRelays";
|
||||
export { default as proxyRouter } from "./proxy";
|
||||
export * from "./getAllRelays";
|
||||
@@ -7,7 +7,7 @@ import * as auth from "@server/routers/auth";
|
||||
import * as supporterKey from "@server/routers/supporterKey";
|
||||
import * as license from "@server/routers/license";
|
||||
import * as idp from "@server/routers/idp";
|
||||
import proxyRouter from "@server/routers/gerbil/proxy";
|
||||
import { proxyToRemote } from "@server/remoteProxy";
|
||||
import config from "@server/lib/config";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import {
|
||||
@@ -53,7 +53,22 @@ internalRouter.use("/gerbil", gerbilRouter);
|
||||
|
||||
if (config.getRawConfig().hybrid) {
|
||||
// Use proxy router to forward requests to remote cloud server
|
||||
gerbilRouter.use("/", proxyRouter);
|
||||
// Proxy endpoints for each gerbil route
|
||||
gerbilRouter.post("/get-config", (req, res, next) =>
|
||||
proxyToRemote(req, res, next, "gerbil/get-config")
|
||||
);
|
||||
|
||||
gerbilRouter.post("/receive-bandwidth", (req, res, next) =>
|
||||
proxyToRemote(req, res, next, "gerbil/receive-bandwidth")
|
||||
);
|
||||
|
||||
gerbilRouter.post("/update-hole-punch", (req, res, next) =>
|
||||
proxyToRemote(req, res, next, "gerbil/update-hole-punch")
|
||||
);
|
||||
|
||||
gerbilRouter.post("/get-all-relays", (req, res, next) =>
|
||||
proxyToRemote(req, res, next, "gerbil/get-all-relays")
|
||||
);
|
||||
} else {
|
||||
// Use local gerbil endpoints
|
||||
gerbilRouter.post("/get-config", gerbil.getConfig);
|
||||
@@ -67,6 +82,13 @@ const badgerRouter = Router();
|
||||
internalRouter.use("/badger", badgerRouter);
|
||||
|
||||
badgerRouter.post("/verify-session", badger.verifyResourceSession);
|
||||
badgerRouter.post("/exchange-session", badger.exchangeSession);
|
||||
|
||||
if (config.getRawConfig().hybrid) {
|
||||
badgerRouter.post("/exchange-session", (req, res, next) =>
|
||||
proxyToRemote(req, res, next, "badger/exchange-session")
|
||||
);
|
||||
} else {
|
||||
badgerRouter.post("/exchange-session", badger.exchangeSession);
|
||||
}
|
||||
|
||||
export default internalRouter;
|
||||
|
||||
Reference in New Issue
Block a user