Hydrate Aeroflot shell in local dev
This commit is contained in:
+129
-5
@@ -21,8 +21,10 @@ const PUBLIC_PORT = 8080;
|
||||
const MODERNJS_PORT = 8081;
|
||||
const API_TARGET = process.env.API_TARGET || "https://flights.test.aeroflot.ru";
|
||||
const TRACKER_TARGET = process.env.TRACKER_TARGET || "https://platform.test.aeroflot.ru";
|
||||
const AEROFLOT_STATIC_TARGET = process.env.AEROFLOT_STATIC_TARGET || "https://www.aeroflot.ru";
|
||||
const SYSTEM_PROXY = process.env.https_proxy || process.env.HTTPS_PROXY || "";
|
||||
const DEBUG_PROXY_BODY = process.env.DEBUG_PROXY_BODY === "1";
|
||||
const LOCAL_PUBLIC_ORIGIN = `http://localhost:${PUBLIC_PORT}`;
|
||||
|
||||
// Shared cookie jar so the Ngenix WAF cookie challenge (`ngenix_valid` +
|
||||
// 307-to-self) only runs once per dev-server lifetime, not per request.
|
||||
@@ -40,11 +42,19 @@ const modernBin = resolve("node_modules", ".bin", "modern");
|
||||
const modernProcess = existsSync(modernBin)
|
||||
? spawn(modernBin, ["dev"], {
|
||||
stdio: ["ignore", "inherit", "inherit"],
|
||||
env: { ...process.env, PORT: String(MODERNJS_PORT) },
|
||||
env: {
|
||||
...process.env,
|
||||
PORT: String(MODERNJS_PORT),
|
||||
AEROFLOT_SHELL_LOADER_PROXY: "1",
|
||||
},
|
||||
})
|
||||
: spawn(process.execPath, [resolve("node_modules", "@modern-js/app-tools", "bin", "modern.js"), "dev"], {
|
||||
stdio: ["ignore", "inherit", "inherit"],
|
||||
env: { ...process.env, PORT: String(MODERNJS_PORT) },
|
||||
env: {
|
||||
...process.env,
|
||||
PORT: String(MODERNJS_PORT),
|
||||
AEROFLOT_SHELL_LOADER_PROXY: "1",
|
||||
},
|
||||
});
|
||||
modernProcess.on("error", (err) => {
|
||||
console.error("Modern.js failed:", err);
|
||||
@@ -209,16 +219,108 @@ const trackerProxy = createProxyMiddleware({
|
||||
});
|
||||
app.use("/tracker", trackerProxy);
|
||||
|
||||
function execCurlWithFallback(buildArgs, extraArgs, res) {
|
||||
// --- Aeroflot frontend loader static proxy ---
|
||||
// The loader derives follow-up asset URLs from its own script origin. Serving
|
||||
// it through localhost keeps Header/Footer hydration visible in dev Chrome
|
||||
// without browser CORS errors from www.aeroflot.ru.
|
||||
app.use(
|
||||
"/frontend/static",
|
||||
createProxyMiddleware({
|
||||
target: AEROFLOT_STATIC_TARGET,
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
logLevel: "warn",
|
||||
pathRewrite: (path) => `/frontend/static${path}`,
|
||||
selfHandleResponse: true,
|
||||
on: {
|
||||
proxyRes: responseInterceptor(async (buffer, proxyRes) => {
|
||||
const contentType = proxyRes.headers["content-type"] ?? "";
|
||||
if (!/(css|html|javascript|json|text)/i.test(String(contentType))) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return buffer
|
||||
.toString("utf8")
|
||||
.replaceAll("https://gw.aeroflot.ru", "/gw")
|
||||
.replaceAll("https://www.aeroflot.ru", "")
|
||||
.replaceAll("https://aeroflot.ru", "");
|
||||
}),
|
||||
},
|
||||
...(SYSTEM_PROXY ? { agent: new HttpsProxyAgent(SYSTEM_PROXY) } : {}),
|
||||
}),
|
||||
);
|
||||
|
||||
app.use("/personal/services/internal/v.0.0.1/json/get_member_info", (_req, res) => {
|
||||
res.status(200).json({ data: null });
|
||||
});
|
||||
|
||||
app.use("/gw/api/pr/LKAB/Profile/v3/get", (_req, res) => {
|
||||
res.status(200).json({ data: null });
|
||||
});
|
||||
|
||||
app.use(["/ws2/v.0.0.1/json/currency/RU", "/ws2/v.0.0.1/json/calcurr/"], (_req, res) => {
|
||||
res.status(200).json({ data: { currency: "RUB" }, errors: [], isSuccess: true });
|
||||
});
|
||||
|
||||
app.use("/pkl/ws/json/v1/member/get", (_req, res) => {
|
||||
res.status(200).json({ data: null, errors: [], isSuccess: true });
|
||||
});
|
||||
|
||||
app.use(
|
||||
["/ws2", "/cms2", "/personal", "/offers", "/feedback"],
|
||||
express.raw({ type: "*/*" }),
|
||||
(req, res) => {
|
||||
const targetUrl = `${AEROFLOT_STATIC_TARGET}${req.originalUrl}`;
|
||||
const requestBody = req.body?.length
|
||||
? req.body.toString("utf8").replaceAll(LOCAL_PUBLIC_ORIGIN, API_TARGET)
|
||||
: "";
|
||||
|
||||
const buildArgs = (noproxy) => [
|
||||
"-s",
|
||||
"-L",
|
||||
...(noproxy ? ["--noproxy", "*"] : []),
|
||||
"-H", `Accept: ${req.headers.accept || "application/json"}`,
|
||||
"-H", `User-Agent: ${req.headers["user-agent"] || "Mozilla/5.0"}`,
|
||||
"-H", `Accept-Language: ${req.headers["accept-language"] || "ru"}`,
|
||||
"-w", "\n%{http_code}",
|
||||
targetUrl,
|
||||
];
|
||||
|
||||
const bodyArgs =
|
||||
req.method === "GET" || req.method === "HEAD"
|
||||
? []
|
||||
: [
|
||||
"-X", req.method,
|
||||
"-H", `Content-Type: ${req.headers["content-type"] || "application/json"}`,
|
||||
...(requestBody ? ["--data-raw", requestBody] : []),
|
||||
];
|
||||
|
||||
execCurlWithFallback(buildArgs, bodyArgs, res, respondWithAeroflotFrontendResult);
|
||||
},
|
||||
);
|
||||
|
||||
app.use(
|
||||
"/media",
|
||||
createProxyMiddleware({
|
||||
target: AEROFLOT_STATIC_TARGET,
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
logLevel: "warn",
|
||||
pathRewrite: (path) => `/media${path}`,
|
||||
...(SYSTEM_PROXY ? { agent: new HttpsProxyAgent(SYSTEM_PROXY) } : {}),
|
||||
}),
|
||||
);
|
||||
|
||||
function execCurlWithFallback(buildArgs, extraArgs, res, responder = respondWithCurlResult) {
|
||||
runCurl([...extraArgs, ...buildArgs(true)], (direct) => {
|
||||
if (isSuccessfulUpstream(direct)) {
|
||||
respondWithCurlResult(direct, res);
|
||||
responder(direct, res);
|
||||
return;
|
||||
}
|
||||
// Direct hit a WAF deny / throttle / network failure — retry through
|
||||
// the system HTTPS_PROXY (gost VPN tunnel on this host).
|
||||
runCurl([...extraArgs, ...buildArgs(false)], (proxy) => {
|
||||
respondWithCurlResult(isSuccessfulUpstream(proxy) ? proxy : direct, res);
|
||||
responder(isSuccessfulUpstream(proxy) ? proxy : direct, res);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -285,6 +387,28 @@ function respondWithCurlResult({ err, stdout }, res) {
|
||||
res.send(body);
|
||||
}
|
||||
|
||||
function respondWithAeroflotFrontendResult(result, res) {
|
||||
if (result.err) {
|
||||
res.status(502).json({ error: result.err.message });
|
||||
return;
|
||||
}
|
||||
|
||||
const lastNewline = result.stdout.lastIndexOf("\n");
|
||||
const rawBody = lastNewline >= 0 ? result.stdout.substring(0, lastNewline) : result.stdout;
|
||||
const body = rawBody
|
||||
.replaceAll("https://gw.aeroflot.ru", "/gw")
|
||||
.replaceAll("https://www.aeroflot.ru", "")
|
||||
.replaceAll("https://aeroflot.ru", "");
|
||||
const statusStr = lastNewline >= 0 ? result.stdout.substring(lastNewline + 1).trim() : "200";
|
||||
const status = parseInt(statusStr) || 200;
|
||||
const isJson = body.trimStart().startsWith("{") || body.trimStart().startsWith("[");
|
||||
|
||||
res.status(status);
|
||||
res.set("Content-Type", isJson ? "application/json" : "text/html");
|
||||
res.set("Access-Control-Allow-Origin", "*");
|
||||
res.send(body);
|
||||
}
|
||||
|
||||
function summarizeBody(body) {
|
||||
const trimmed = body.trimStart();
|
||||
if (!trimmed) return "body=empty";
|
||||
|
||||
Reference in New Issue
Block a user