Refactor normalizeOptions function; simplify argument handling and improve boolean conversion logic for better clarity and performance
This commit is contained in:
@@ -24,52 +24,10 @@ const AMZ_DEFAULTS = {
|
|||||||
* - dns=false => remote-dns-resolve: false (вне зависимости от входа)
|
* - dns=false => remote-dns-resolve: false (вне зависимости от входа)
|
||||||
* - ipv6=false => удалить IPv6 из allowed-ips (и вообще не добавлять ipv6-части)
|
* - ipv6=false => удалить IPv6 из allowed-ips (и вообще не добавлять ipv6-части)
|
||||||
**********************/
|
**********************/
|
||||||
function normalizeOptions(rawOptions) {
|
function normalizeOptions() {
|
||||||
const opts = rawOptions ?? {};
|
const args = (typeof $arguments !== "undefined" && $arguments) ? $arguments : {};
|
||||||
|
|
||||||
// попробуем найти URL в разных местах (Sub Store версии гуляют)
|
const asBool = (v, def = true) => {
|
||||||
const urlStr =
|
|
||||||
(typeof opts === "string" ? opts : "") ||
|
|
||||||
opts.url ||
|
|
||||||
(opts.meta && opts.meta.url) ||
|
|
||||||
(opts.subscription && opts.subscription.url) ||
|
|
||||||
(opts.profile && opts.profile.url) ||
|
|
||||||
(opts.ctx && opts.ctx.url) ||
|
|
||||||
(opts.request && opts.request.url) ||
|
|
||||||
"";
|
|
||||||
|
|
||||||
// вытащить hash часть (#...)
|
|
||||||
let hash = "";
|
|
||||||
if (typeof urlStr === "string") {
|
|
||||||
const idx = urlStr.indexOf("#");
|
|
||||||
if (idx >= 0) hash = urlStr.slice(idx + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// иногда Sub Store может передать query прямо как объект
|
|
||||||
// пробуем дополнительно распарсить opts.query/opts.params, если они строки
|
|
||||||
const extra =
|
|
||||||
(typeof opts.query === "string" ? opts.query : "") ||
|
|
||||||
(typeof opts.params === "string" ? opts.params : "") ||
|
|
||||||
(typeof opts.hash === "string" ? opts.hash : "") ||
|
|
||||||
"";
|
|
||||||
|
|
||||||
const qstr = (hash || extra || "").trim();
|
|
||||||
|
|
||||||
const params = {};
|
|
||||||
if (qstr.includes("=")) {
|
|
||||||
for (const part of qstr.split("&")) {
|
|
||||||
if (!part) continue;
|
|
||||||
const [k, v] = part.split("=");
|
|
||||||
if (!k) continue;
|
|
||||||
params[k.trim().toLowerCase()] = (v ?? "").trim();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ещё фоллбек: если opts.dns/opts.ipv6 передали напрямую
|
|
||||||
const directDns = opts.dns;
|
|
||||||
const directIpv6 = opts.ipv6;
|
|
||||||
|
|
||||||
const asBool = (v, def) => {
|
|
||||||
if (v === undefined || v === null || v === "") return def;
|
if (v === undefined || v === null || v === "") return def;
|
||||||
if (typeof v === "boolean") return v;
|
if (typeof v === "boolean") return v;
|
||||||
const s = String(v).toLowerCase().trim();
|
const s = String(v).toLowerCase().trim();
|
||||||
@@ -79,13 +37,12 @@ function normalizeOptions(rawOptions) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dns: asBool(params.dns ?? directDns, true),
|
dns: asBool(args.dns, true),
|
||||||
ipv6: asBool(params.ipv6 ?? directIpv6, true),
|
ipv6: asBool(args.ipv6, true),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* Parsing WG INI blocks
|
* Parsing WG INI blocks
|
||||||
**********************/
|
**********************/
|
||||||
@@ -249,7 +206,7 @@ function buildProxy(blockName, wg, options) {
|
|||||||
/**********************
|
/**********************
|
||||||
* ENTRYPOINT
|
* ENTRYPOINT
|
||||||
**********************/
|
**********************/
|
||||||
const opts = normalizeOptions($options);
|
const opts = normalizeOptions();
|
||||||
|
|
||||||
// Вход: чаще всего $content, но на всякий пожарный берём $files[0]
|
// Вход: чаще всего $content, но на всякий пожарный берём $files[0]
|
||||||
const input = String($content ?? ($files && $files[0]) ?? "");
|
const input = String($content ?? ($files && $files[0]) ?? "");
|
||||||
|
|||||||
Reference in New Issue
Block a user