Fix: use password header to fix wg-easy widget
This commit is contained in:
parent
30c5614114
commit
5fc046c9a2
@ -1,5 +1,3 @@
|
|||||||
import cache from "memory-cache";
|
|
||||||
|
|
||||||
import getServiceWidget from "utils/config/service-helpers";
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
@ -8,51 +6,6 @@ import createLogger from "utils/logger";
|
|||||||
|
|
||||||
const proxyName = "wgeasyProxyHandler";
|
const proxyName = "wgeasyProxyHandler";
|
||||||
const logger = createLogger(proxyName);
|
const logger = createLogger(proxyName);
|
||||||
const sessionSIDCacheKey = `${proxyName}__sessionSID`;
|
|
||||||
|
|
||||||
async function login(widget, service) {
|
|
||||||
const url = formatApiCall(widgets[widget.type].api, { ...widget, endpoint: "session" });
|
|
||||||
const [statusCode, , data, responseHeaders] = await httpProxy(url, {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({ password: widget.password }),
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (statusCode > 400) {
|
|
||||||
cache.del(`${sessionSIDCacheKey}.${service}`);
|
|
||||||
logger.error(
|
|
||||||
`Failed to login to wg-easy, statusCode: ${statusCode}, responseHeaders: ${JSON.stringify(responseHeaders)}`,
|
|
||||||
);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
logger.debug(`Logging into wg-easy, responseHeaders: ${JSON.stringify(responseHeaders)}`);
|
|
||||||
let connectSidCookie = responseHeaders["set-cookie"];
|
|
||||||
if (!connectSidCookie) {
|
|
||||||
const sid = cache.get(`${sessionSIDCacheKey}.${service}`);
|
|
||||||
if (sid) {
|
|
||||||
return sid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
connectSidCookie = connectSidCookie
|
|
||||||
.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: ${JSON.stringify(e)}, statusCode: ${statusCode} responseHeaders: ${JSON.stringify(
|
|
||||||
responseHeaders,
|
|
||||||
)}, data: ${Buffer.isBuffer(data) ? Buffer.from(data).toString() : JSON.stringify(data)}`,
|
|
||||||
);
|
|
||||||
cache.del(`${sessionSIDCacheKey}.${service}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function wgeasyProxyHandler(req, res) {
|
export default async function wgeasyProxyHandler(req, res) {
|
||||||
const { group, service } = req.query;
|
const { group, service } = req.query;
|
||||||
@ -65,29 +18,31 @@ export default async function wgeasyProxyHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (widget) {
|
if (widget) {
|
||||||
let sid = cache.get(`${sessionSIDCacheKey}.${service}`);
|
const [statusCode, , data] = await httpProxy(
|
||||||
if (!sid) {
|
|
||||||
sid = await login(widget, service);
|
|
||||||
if (!sid) {
|
|
||||||
return res.status(500).json({ error: "Failed to authenticate with Wg-Easy. See logs for more details." });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const [, , data] = await httpProxy(
|
|
||||||
formatApiCall(widgets[widget.type].api, { ...widget, endpoint: "wireguard/client" }),
|
formatApiCall(widgets[widget.type].api, { ...widget, endpoint: "wireguard/client" }),
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Cookie: `connect.sid=${sid}`,
|
authorization: widget.password,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const parsedData = JSON.parse(data);
|
let parsedData;
|
||||||
|
try {
|
||||||
|
parsedData = JSON.parse(data);
|
||||||
|
} catch (e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
if (parsedData.statusCode > 400) {
|
if (parsedData?.statusCode > 400 || statusCode > 400) {
|
||||||
cache.del(`${sessionSIDCacheKey}.${service}`);
|
logger.error(
|
||||||
|
`Error communicating with Wg-Easy. StatusCode: ${
|
||||||
|
parsedData?.statusCode ?? statusCode
|
||||||
|
}. Data: ${JSON.stringify(parsedData)}`,
|
||||||
|
);
|
||||||
return res
|
return res
|
||||||
.status(parsedData.statusCode)
|
.status(parsedData?.statusCode ?? statusCode)
|
||||||
.json({ error: { message: "Error communicating with Wg-Easy", data: parsedData } });
|
.json({ error: { message: "Error communicating with Wg-Easy", data: parsedData } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user