0.2.2 onlyoffice修复
This commit is contained in:
@@ -60,6 +60,39 @@ function buildUpstreamRequestHead(req, targetUrl) {
|
||||
lines.push(`${req.method || "GET"} ${upstreamPath} HTTP/1.1`);
|
||||
|
||||
const headers = req.headers || {};
|
||||
const headerValue = (name) => {
|
||||
const v = headers[name];
|
||||
if (!v) return "";
|
||||
return Array.isArray(v) ? v[0] : String(v);
|
||||
};
|
||||
|
||||
// 说明:ONLYOFFICE 在反向代理下会依赖 X-Forwarded-* 推导“对外地址”,用于拼出 ws/wss 与缓存资源 URL。
|
||||
// 这里尽量沿用上游(frp/nginx)注入的 x-forwarded-proto/host;若缺失,则回退到本机 http。
|
||||
const originLike = headerValue("origin") || headerValue("referer") || "";
|
||||
const originUrl = (() => {
|
||||
try {
|
||||
if (!originLike) return null;
|
||||
return new URL(originLike);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
const forwardedHostRaw =
|
||||
headerValue("x-forwarded-host") || (originUrl ? originUrl.host : "") || headerValue("host") || "";
|
||||
const forwardedProto =
|
||||
(headerValue("x-forwarded-proto") || "").split(",")[0].trim() ||
|
||||
(originUrl ? originUrl.protocol.replace(":", "") : "") ||
|
||||
"http";
|
||||
const forwardedPort = (() => {
|
||||
const fromHeader = (headerValue("x-forwarded-port") || "").split(",")[0].trim();
|
||||
if (fromHeader) return fromHeader;
|
||||
const hostHasPort = forwardedHostRaw.includes(":") ? forwardedHostRaw.split(":").pop() : "";
|
||||
if (hostHasPort && /^\d+$/.test(hostHasPort)) return hostHasPort;
|
||||
return forwardedProto === "https" ? "443" : "80";
|
||||
})();
|
||||
const forwardedHost = forwardedHostRaw.split(",")[0].trim() || "localhost";
|
||||
|
||||
for (const [k, v] of Object.entries(headers)) {
|
||||
if (!v) continue;
|
||||
const key = String(k);
|
||||
@@ -71,6 +104,12 @@ function buildUpstreamRequestHead(req, targetUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
// 说明:补齐/覆盖 forward 信息,避免 ONLYOFFICE 返回指向内部端口的绝对 URL。
|
||||
lines.push(`x-forwarded-host: ${forwardedHost}`);
|
||||
lines.push(`x-forwarded-proto: ${forwardedProto}`);
|
||||
lines.push(`x-forwarded-port: ${forwardedPort}`);
|
||||
lines.push(`x-forwarded-prefix: ${ONLYOFFICE_PREFIX}`);
|
||||
|
||||
// 说明:Host 必须指向 ONLYOFFICE_INTERNAL_URL,否则上游可能拒绝 Upgrade。
|
||||
lines.push(`Host: ${targetUrl.host}`);
|
||||
lines.push("");
|
||||
@@ -144,7 +183,16 @@ async function main() {
|
||||
if (isOnlyOfficePath(req.url || "/")) {
|
||||
try {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("[dev-server][onlyoffice-ws] upgrade", req.url);
|
||||
console.log(
|
||||
"[dev-server][onlyoffice-ws] upgrade",
|
||||
req.url,
|
||||
"host=",
|
||||
req.headers.host,
|
||||
"xfp=",
|
||||
req.headers["x-forwarded-proto"],
|
||||
"xfh=",
|
||||
req.headers["x-forwarded-host"],
|
||||
);
|
||||
} catch {}
|
||||
proxyOnlyOfficeUpgrade(req, socket, head);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user