Feature: Wg-Easy Widget (#3476)

---------

Co-authored-by: ConnerWithAnE <46903591+ConnerWithAnE@users.noreply.github.com>
Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
Conner Hnatiuk 2024-05-16 23:26:12 -06:00 committed by GitHub
parent 1144f4dfa0
commit 6ab6d6fd3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 158 additions and 0 deletions

View file

@ -0,0 +1,70 @@
import cache from "memory-cache";
import getServiceWidget from "utils/config/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import widgets from "widgets/widgets";
import createLogger from "utils/logger";
const proxyName = "wgeasyProxyHandler";
const logger = createLogger(proxyName);
const sessionSIDCacheKey = `${proxyName}__sessionSID`;
async function login(widget, service) {
const url = formatApiCall(widgets[widget.type].api, { ...widget, endpoint: "session" });
const [, , , responseHeaders] = await httpProxy(url, {
method: "POST",
body: JSON.stringify({ password: widget.password }),
headers: {
"Content-Type": "application/json",
},
});
try {
const connectSidCookie = responseHeaders["set-cookie"]
.find((cookie) => cookie.startsWith("connect.sid="))
.split(";")[0]
.replace("connect.sid=", "");
cache.put(`${sessionSIDCacheKey}.${service}`, connectSidCookie);
return connectSidCookie;
} catch (e) {
logger.error(`Error logging into wg-easy`);
cache.del(`${sessionSIDCacheKey}.${service}`);
return null;
}
}
export default async function wgeasyProxyHandler(req, res) {
const { group, service } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (!widgets?.[widget.type]?.api) {
return res.status(403).json({ error: "Service does not support API calls" });
}
if (widget) {
let sid = cache.get(`${sessionSIDCacheKey}.${service}`);
if (!sid) {
sid = await login(widget, service);
if (!sid) {
return res.status(500).json({ error: "Failed to authenticate with Wg-Easy" });
}
}
const [, , data] = await httpProxy(
formatApiCall(widgets[widget.type].api, { ...widget, endpoint: "wireguard/client" }),
{
headers: {
"Content-Type": "application/json",
Cookie: `connect.sid=${sid}`,
},
},
);
return res.json(JSON.parse(data));
}
}
return res.status(400).json({ error: "Invalid proxy service type" });
}