Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +12 -0
- package.json +9 -0
- server.js +172 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:18-alpine
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY package*.json ./
|
| 5 |
+
RUN npm ci --only=production
|
| 6 |
+
COPY server.js ./
|
| 7 |
+
|
| 8 |
+
# Hugging Face routes to port 7860
|
| 9 |
+
ENV PORT=7860
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["node", "server.js"]
|
package.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "hf-proxy",
|
| 3 |
+
"private": true,
|
| 4 |
+
"type": "module",
|
| 5 |
+
"dependencies": {
|
| 6 |
+
"http-proxy": "^1.18.1",
|
| 7 |
+
"express": "^4.19.2"
|
| 8 |
+
}
|
| 9 |
+
}
|
server.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import httpProxy from "http-proxy";
|
| 3 |
+
|
| 4 |
+
const TARGET = process.env.TARGET_URL;
|
| 5 |
+
const TOKEN = process.env.HF_TOKEN_PRIVATE_SPACE;
|
| 6 |
+
|
| 7 |
+
if (!TARGET || !TOKEN) {
|
| 8 |
+
console.error("β Missing TARGET_URL or HF_TOKEN_PRIVATE_SPACE");
|
| 9 |
+
process.exit(1);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
const app = express();
|
| 13 |
+
|
| 14 |
+
// Parse the target URL
|
| 15 |
+
const targetUrl = new URL(TARGET);
|
| 16 |
+
|
| 17 |
+
// Create proxy with WebSocket + headers
|
| 18 |
+
const proxy = httpProxy.createProxyServer({
|
| 19 |
+
target: TARGET,
|
| 20 |
+
changeOrigin: true,
|
| 21 |
+
ws: true,
|
| 22 |
+
secure: true,
|
| 23 |
+
xfwd: true,
|
| 24 |
+
autoRewrite: true, // Important: automatically rewrite Location header
|
| 25 |
+
protocolRewrite: 'https' // Ensure HTTPS in redirects
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
// --- DEBUG LOGGING -----------------------------------------------------------
|
| 29 |
+
app.use((req, res, next) => {
|
| 30 |
+
console.log(`β‘οΈ ${req.method} ${req.originalUrl}`);
|
| 31 |
+
console.log(" Host:", req.headers.host);
|
| 32 |
+
console.log(" Referer:", req.headers.referer);
|
| 33 |
+
next();
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
// --- CRITICAL: Prevent redirect loops ---
|
| 37 |
+
proxy.on("proxyReq", (proxyReq, req) => {
|
| 38 |
+
// Add authorization
|
| 39 |
+
proxyReq.setHeader("Authorization", `Bearer ${TOKEN}`);
|
| 40 |
+
|
| 41 |
+
// Preserve important headers
|
| 42 |
+
const publicHost = req.headers["x-forwarded-host"] || req.headers.host;
|
| 43 |
+
const publicProto = req.headers["x-forwarded-proto"] || "https";
|
| 44 |
+
|
| 45 |
+
// Set forwarding headers so LibreChat knows the original request context
|
| 46 |
+
proxyReq.setHeader("X-Forwarded-Host", publicHost);
|
| 47 |
+
proxyReq.setHeader("X-Forwarded-Proto", publicProto);
|
| 48 |
+
proxyReq.setHeader("X-Forwarded-For", req.connection.remoteAddress);
|
| 49 |
+
proxyReq.setHeader("X-Real-IP", req.connection.remoteAddress);
|
| 50 |
+
|
| 51 |
+
// IMPORTANT: Rewrite Referer header if it points to our public URL
|
| 52 |
+
if (req.headers.referer) {
|
| 53 |
+
const referer = req.headers.referer;
|
| 54 |
+
if (referer.includes(publicHost)) {
|
| 55 |
+
const newReferer = referer.replace(`https://${publicHost}`, TARGET).replace(`http://${publicHost}`, TARGET);
|
| 56 |
+
proxyReq.setHeader("Referer", newReferer);
|
| 57 |
+
console.log(" π Rewriting Referer:", referer, "->", newReferer);
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// IMPORTANT: Rewrite Origin header if present
|
| 62 |
+
if (req.headers.origin) {
|
| 63 |
+
const origin = req.headers.origin;
|
| 64 |
+
if (origin.includes(publicHost)) {
|
| 65 |
+
proxyReq.setHeader("Origin", TARGET);
|
| 66 |
+
console.log(" π Rewriting Origin:", origin, "->", TARGET);
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
console.log(`π Forwarding -> ${TARGET}${req.url}`);
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
proxy.on("proxyRes", (proxyRes, req, res) => {
|
| 74 |
+
console.log(`β¬
οΈ Response ${proxyRes.statusCode} from ${TARGET}${req.url}`);
|
| 75 |
+
|
| 76 |
+
// Get the public URL base
|
| 77 |
+
const publicHost = (req.headers["x-forwarded-host"] || req.headers.host || "").toString();
|
| 78 |
+
const publicProto = req.headers["x-forwarded-proto"] || "https";
|
| 79 |
+
const publicBase = publicHost ? `${publicProto}://${publicHost}` : null;
|
| 80 |
+
|
| 81 |
+
// --- FIX 1: Handle Location redirects ---
|
| 82 |
+
if (proxyRes.headers["location"]) {
|
| 83 |
+
const location = proxyRes.headers["location"];
|
| 84 |
+
console.log(" π§ Original Location:", location);
|
| 85 |
+
|
| 86 |
+
// Rewrite absolute URLs pointing to target
|
| 87 |
+
if (location.startsWith(TARGET)) {
|
| 88 |
+
proxyRes.headers["location"] = location.replace(TARGET, publicBase);
|
| 89 |
+
console.log(" β
Rewritten to:", proxyRes.headers["location"]);
|
| 90 |
+
}
|
| 91 |
+
// Handle protocol-relative or absolute paths
|
| 92 |
+
else if (location.startsWith("/")) {
|
| 93 |
+
proxyRes.headers["location"] = `${publicBase}${location}`;
|
| 94 |
+
console.log(" β
Rewritten to:", proxyRes.headers["location"]);
|
| 95 |
+
}
|
| 96 |
+
// If it already points to our public URL, leave it alone
|
| 97 |
+
else if (location.startsWith(publicBase)) {
|
| 98 |
+
console.log(" βΉοΈ Already points to public URL, keeping as-is");
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// --- FIX 2: Fix cookies ---
|
| 103 |
+
const setCookies = proxyRes.headers["set-cookie"];
|
| 104 |
+
if (Array.isArray(setCookies) && publicHost) {
|
| 105 |
+
console.log(" πͺ Processing", setCookies.length, "cookies");
|
| 106 |
+
|
| 107 |
+
proxyRes.headers["set-cookie"] = setCookies.map((cookie) => {
|
| 108 |
+
let newCookie = cookie;
|
| 109 |
+
|
| 110 |
+
// Remove or replace Domain attribute to match public host
|
| 111 |
+
if (/;\s*Domain=/i.test(newCookie)) {
|
| 112 |
+
// Remove the target domain
|
| 113 |
+
newCookie = newCookie.replace(/;\s*Domain=[^;]*/i, "");
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Add our public domain (without leading dot for exact match)
|
| 117 |
+
newCookie = `${newCookie}; Domain=${publicHost}`;
|
| 118 |
+
|
| 119 |
+
// Ensure Secure flag (required for SameSite=None)
|
| 120 |
+
if (!/;\s*Secure/i.test(newCookie)) {
|
| 121 |
+
newCookie = `${newCookie}; Secure`;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// Set SameSite=None for cross-site cookies (required for iframe/embed contexts)
|
| 125 |
+
if (!/SameSite=/i.test(newCookie)) {
|
| 126 |
+
newCookie = `${newCookie}; SameSite=None`;
|
| 127 |
+
} else {
|
| 128 |
+
newCookie = newCookie.replace(/SameSite=[^;]*/i, "SameSite=None");
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
return newCookie;
|
| 132 |
+
});
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// --- FIX 3: Add CORS headers if needed ---
|
| 136 |
+
// This helps if LibreChat makes client-side requests
|
| 137 |
+
if (!proxyRes.headers["access-control-allow-credentials"]) {
|
| 138 |
+
proxyRes.headers["access-control-allow-credentials"] = "true";
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
proxy.on("error", (err, req, res) => {
|
| 143 |
+
console.error("β Proxy error:", err.message);
|
| 144 |
+
console.error(" Stack:", err.stack);
|
| 145 |
+
|
| 146 |
+
if (res && !res.headersSent) {
|
| 147 |
+
res.status(502).json({
|
| 148 |
+
error: "Proxy Error",
|
| 149 |
+
message: "Unable to reach the upstream server",
|
| 150 |
+
details: err.message
|
| 151 |
+
});
|
| 152 |
+
}
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
// Health check endpoint (optional but useful)
|
| 156 |
+
app.get("/health", (req, res) => {
|
| 157 |
+
res.json({ status: "ok", target: TARGET, timestamp: new Date().toISOString() });
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
// Forward everything else
|
| 161 |
+
app.use((req, res) => proxy.web(req, res));
|
| 162 |
+
|
| 163 |
+
const server = app.listen(process.env.PORT || 7860, () => {
|
| 164 |
+
console.log("β
Proxy listening on port", process.env.PORT || 7860);
|
| 165 |
+
console.log("π― Target:", TARGET);
|
| 166 |
+
console.log("π Using HF token:", TOKEN ? "***" + TOKEN.slice(-4) : "MISSING");
|
| 167 |
+
});
|
| 168 |
+
|
| 169 |
+
server.on("upgrade", (req, socket, head) => {
|
| 170 |
+
console.log("βοΈ WebSocket upgrade:", req.url);
|
| 171 |
+
proxy.ws(req, socket, head);
|
| 172 |
+
});
|