feat: sync app-created peers into clients table

This commit is contained in:
andrey271192
2026-06-21 19:19:49 +03:00
parent fd66b0a024
commit dfeabe02d7
7 changed files with 102 additions and 3 deletions

View File

@@ -976,6 +976,11 @@ function clientDisplayNameFromRow(row, id) {
return String(ud.clientName || row?.name || row?.clientName || `${id.slice(0, 10)}`);
}
function defaultPeerClientName(peer) {
const ip = String(peer?.allowedIPs || "").split(",")[0].trim().replace(/\/\d+$/, "");
return ip ? `Клиент ${ip}` : `Peer ${String(peer?.publicKey || "").slice(0, 10)}`;
}
/** Совпадает с defaults Amnezia Desktop (protocolConstants awg, desktop MTU). */
const AWG_EXPORT_DEFAULTS = {
Jc: "3",
@@ -2347,6 +2352,46 @@ app.get("/api/clients", requireAuth, async (req, res) => {
}
});
app.post("/api/clients/sync-peers", requireAuth, requireProTier, async (req, res) => {
const rt = runtimeFromExportRequest(req);
try {
await rt.backupRemoteFiles();
const { conf, clients } = await rt.loadState();
const existing = new Set(clients.map(clientRowId).filter(Boolean));
const now = new Date().toISOString();
const added = [];
for (const peer of conf.peers) {
const id = peer.publicKey;
if (!id || existing.has(id)) continue;
const row = {
clientId: id,
userData: {
clientName: defaultPeerClientName(peer),
creationDate: now,
importedFromPeerAt: now,
allowedIps: peer.allowedIPs || "",
},
};
clients.push(row);
existing.add(id);
added.push({ clientId: id, name: row.userData.clientName, allowedIps: row.userData.allowedIps });
}
if (added.length) {
await rt.dockerWriteFile(rt.clientsPath, stringifyClientsTable(clients));
}
res.json({
ok: true,
added: added.length,
totalPeers: conf.peers.length,
totalClientsTable: clients.length,
clients: added,
});
} catch (e) {
console.error(e);
res.status(500).json({ error: String(e.message || e) });
}
});
async function serveClientConfigExport(req, res) {
if (IS_COMMUNITY) {
res.status(403).json({