clean: suppression complète PrestaShop

This commit is contained in:
EduBox Dev
2026-06-20 13:57:37 +00:00
parent 20baf3878f
commit dd49993157
25 changed files with 496 additions and 249 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server";
const AGENT_VERSION = "0.2.7";
const AGENT_VERSION = "0.3.0";
export async function GET() {
return NextResponse.json({
+7 -5
View File
@@ -42,9 +42,13 @@ export async function GET(req: NextRequest) {
const publicUrl = domain
? `https://${inst.id}.${domain}`
: null;
const localUrl = inst.node.tailscaleIp
? `http://${inst.node.tailscaleIp}:${inst.port}`
: null;
return {
...inst,
publicUrl,
localUrl,
};
});
@@ -70,7 +74,6 @@ export async function POST(req: NextRequest) {
const domain = node?.student?.class.establishment?.domain;
const publicDomain = domain ? `${instance.id}.${domain}` : "localhost";
const publicUrl = domain ? `https://${publicDomain}` : null;
const sent = sendToNode(nodeId, {
action: "start",
instanceId: instance.id,
@@ -80,7 +83,7 @@ export async function POST(req: NextRequest) {
.replace(/{PORT}/g, String(instance.port))
.replace(/{INSTANCE_ID}/g, instance.id)
.replace(/{PUBLIC_URL}/g, publicUrl || `http://localhost:${instance.port}`)
.replace(/{PUBLIC_DOMAIN}/g, publicDomain),
.replace(/{PUBLIC_DOMAIN}/g, "localhost"),
});
if (!sent) {
@@ -99,7 +102,6 @@ export async function PATCH(req: NextRequest) {
const domain = instance.node.student?.class.establishment?.domain;
const publicDomain = domain ? `${instance.id}.${domain}` : "localhost";
const publicUrl = domain ? `https://${publicDomain}` : null;
if (action === "stop") {
sendToNode(instance.nodeId, { action: "delete", instanceId: instance.id });
await prisma.instance.update({ where: { id }, data: { status: "stopped" } });
@@ -113,7 +115,7 @@ export async function PATCH(req: NextRequest) {
.replace(/{PORT}/g, String(instance.port))
.replace(/{INSTANCE_ID}/g, instance.id)
.replace(/{PUBLIC_URL}/g, publicUrl || `http://localhost:${instance.port}`)
.replace(/{PUBLIC_DOMAIN}/g, publicDomain),
.replace(/{PUBLIC_DOMAIN}/g, "localhost"),
});
if (!sent) await prisma.instance.update({ where: { id }, data: { status: "error" } });
} else if (action === "reset") {
@@ -126,7 +128,7 @@ export async function PATCH(req: NextRequest) {
.replace(/{PORT}/g, String(instance.port))
.replace(/{INSTANCE_ID}/g, instance.id)
.replace(/{PUBLIC_URL}/g, publicUrl || `http://localhost:${instance.port}`)
.replace(/{PUBLIC_DOMAIN}/g, publicDomain),
.replace(/{PUBLIC_DOMAIN}/g, "localhost"),
});
if (!sent) await prisma.instance.update({ where: { id }, data: { status: "error" } });
}
-149
View File
@@ -1,149 +0,0 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
const MAIN_DOMAIN = process.env.MAIN_DOMAIN || "alfrednobel.edudeploy.com";
async function proxyRequest(req: NextRequest) {
const host = req.headers.get("host") || "";
const cleanHost = host.split(":")[0];
if (!cleanHost.endsWith(`.${MAIN_DOMAIN}`)) {
return NextResponse.json({ error: "Invalid host" }, { status: 404 });
}
const subdomain = cleanHost.replace(`.${MAIN_DOMAIN}`, "");
const instance = await prisma.instance.findUnique({
where: { id: subdomain },
include: { node: true },
});
if (!instance || !instance.node?.tailscaleIp) {
return NextResponse.json(
{ error: "Instance not found or not connected" },
{ status: 404 }
);
}
const publicUrl = `https://${cleanHost}`;
const targetUrl = new URL(req.url);
// The middleware rewrites /foo to /api/proxy/foo; strip the prefix before forwarding
let pathname = targetUrl.pathname;
if (pathname.startsWith("/api/proxy")) {
pathname = pathname.slice("/api/proxy".length) || "/";
}
const upstream = `http://${instance.node.tailscaleIp}:${instance.port}${pathname}${targetUrl.search}`;
const headers = new Headers(req.headers);
headers.delete("host");
headers.set("host", cleanHost);
headers.set("x-forwarded-host", cleanHost);
headers.set("x-forwarded-proto", "https");
headers.set("x-forwarded-port", "443");
headers.set("x-forwarded-for", req.headers.get("x-forwarded-for") || "unknown");
const needsBody = req.method !== "GET" && req.method !== "HEAD";
const upstreamRes = await fetch(upstream, {
method: req.method,
headers,
body: needsBody ? (req.body as BodyInit) : undefined,
// Node.js fetch requires duplex when forwarding a request body stream
...(needsBody ? { duplex: "half" } : {}),
redirect: "manual",
});
const responseHeaders = new Headers(upstreamRes.headers);
// Remove content-encoding because Next.js/fetch handles decompression automatically
responseHeaders.delete("content-encoding");
responseHeaders.delete("content-length");
// Rewrite any header values that point to localhost or the internal Tailscale address
const localPatterns = [
`http://${instance.node.tailscaleIp}:${instance.port}`,
`https://${instance.node.tailscaleIp}:${instance.port}`,
`http://localhost:${instance.port}`,
`https://localhost:${instance.port}`,
`http://localhost`,
`https://localhost`,
];
responseHeaders.forEach((value, key) => {
let newValue = value;
for (const pattern of localPatterns) {
newValue = newValue.replaceAll(pattern, publicUrl);
}
if (newValue !== value) {
responseHeaders.set(key, newValue);
}
});
// Sanitize Set-Cookie headers so sessions work through the public domain.
// WordPress may issue cookies for "localhost" or without Secure flag; fix it.
const setCookies = responseHeaders.getSetCookie();
if (setCookies.length > 0) {
responseHeaders.delete("set-cookie");
for (let cookie of setCookies) {
// Drop any Domain=... set for localhost/internal domains
cookie = cookie.replace(/;\s*Domain=[^;]+/gi, "");
// Ensure Secure is present for HTTPS public URLs
if (!/;\s*Secure\b/i.test(cookie)) {
cookie += "; Secure";
}
// Make sure cookies are sent on sub-domain navigations
if (!/;\s*SameSite\b/i.test(cookie)) {
cookie += "; SameSite=Lax";
}
responseHeaders.append("set-cookie", cookie);
}
}
const contentType = responseHeaders.get("content-type") || "";
const shouldRewriteBody =
contentType.includes("text/html") ||
contentType.includes("text/css") ||
contentType.includes("application/javascript") ||
contentType.includes("application/json");
if (!shouldRewriteBody) {
return new Response(upstreamRes.body, {
status: upstreamRes.status,
statusText: upstreamRes.statusText,
headers: responseHeaders,
});
}
// For text responses, rewrite localhost/internal URLs to the public URL.
// Also handle protocol-relative URLs that some WordPress plugins/themes use.
let body = await upstreamRes.text();
const localBase = `http://${instance.node.tailscaleIp}:${instance.port}`;
const localBaseHttps = `https://${instance.node.tailscaleIp}:${instance.port}`;
const localLocalhostHttp = `http://localhost:${instance.port}`;
const localLocalhostHttps = `https://localhost:${instance.port}`;
const localLocalhostPlainHttp = `http://localhost`;
const localLocalhostPlainHttps = `https://localhost`;
const localLocalhostProtocolRelative = `//localhost`;
const localTailscaleProtocolRelative = `//${instance.node.tailscaleIp}:${instance.port}`;
body = body
.replaceAll(localBase, publicUrl)
.replaceAll(localBaseHttps, publicUrl)
.replaceAll(localLocalhostHttp, publicUrl)
.replaceAll(localLocalhostHttps, publicUrl)
.replaceAll(localLocalhostPlainHttp, publicUrl)
.replaceAll(localLocalhostPlainHttps, publicUrl)
.replaceAll(localTailscaleProtocolRelative, publicUrl.replace(/^https?:/, ""))
.replaceAll(localLocalhostProtocolRelative, publicUrl.replace(/^https?:/, ""));
return new Response(body, {
status: upstreamRes.status,
statusText: upstreamRes.statusText,
headers: responseHeaders,
});
}
export const GET = proxyRequest;
export const POST = proxyRequest;
export const PUT = proxyRequest;
export const PATCH = proxyRequest;
export const DELETE = proxyRequest;
export const HEAD = proxyRequest;
export const OPTIONS = proxyRequest;
+28
View File
@@ -0,0 +1,28 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const subdomain = searchParams.get("subdomain");
if (!subdomain) {
return NextResponse.json({ error: "subdomain required" }, { status: 400 });
}
const instance = await prisma.instance.findUnique({
where: { id: subdomain },
include: { node: true },
});
if (!instance || !instance.node) {
return NextResponse.json({ error: "instance not found" }, { status: 404 });
}
if (instance.node.status !== "online" || !instance.node.tailscaleIp) {
return NextResponse.json({ error: "node offline" }, { status: 503 });
}
return NextResponse.json({
upstream: `${instance.node.tailscaleIp}:${instance.port}`,
});
}
+1 -19
View File
@@ -1,6 +1,6 @@
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
const AGENT_VERSION = "0.2.7";
const AGENT_VERSION = "0.3.0";
export const dynamic = "force-dynamic";
@@ -19,24 +19,6 @@ export default function DownloadPage() {
<a href={`/edubox-agent-v${AGENT_VERSION}.exe`} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger (.exe)</a>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Linux</CardTitle>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground mb-4">Agent EduBox pour Linux (64 bits)</p>
<a href={`/edubox-agent-v${AGENT_VERSION}`} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger</a>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>macOS</CardTitle>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground mb-4">Agent EduBox pour macOS (Intel & Apple Silicon)</p>
<a href={`/edubox-agent-v${AGENT_VERSION}-mac`} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger</a>
</CardContent>
</Card>
</div>
</div>
);
+6 -1
View File
@@ -30,6 +30,7 @@ export default async function InstancesPage() {
return {
...inst,
publicUrl: domain ? `https://${inst.id}.${domain}` : null,
localUrl: inst.node.tailscaleIp ? `http://${inst.node.tailscaleIp}:${inst.port}` : null,
};
});
@@ -53,6 +54,7 @@ export default async function InstancesPage() {
<TableHead>Port</TableHead>
<TableHead>Statut</TableHead>
<TableHead>URL publique</TableHead>
<TableHead>Accès prof</TableHead>
<TableHead>Actions</TableHead>
</TableRow>
</TableHeader>
@@ -70,6 +72,9 @@ export default async function InstancesPage() {
<TableCell>
{inst.publicUrl ? <a href={inst.publicUrl} target="_blank" rel="noopener" className="text-blue-600 hover:underline">{inst.publicUrl}</a> : "-"}
</TableCell>
<TableCell>
{inst.localUrl ? <a href={inst.localUrl} target="_blank" rel="noopener" className="text-blue-600 hover:underline">{inst.localUrl}</a> : "-"}
</TableCell>
<TableCell>
<InstanceActions instanceId={inst.id} status={inst.status} />
</TableCell>
@@ -77,7 +82,7 @@ export default async function InstancesPage() {
))}
{instances.length === 0 && (
<TableRow>
<TableCell colSpan={8} className="text-center text-muted-foreground">Aucune instance</TableCell>
<TableCell colSpan={9} className="text-center text-muted-foreground">Aucune instance</TableCell>
</TableRow>
)}
</TableBody>