mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-12 20:25:06 +01:00
url: return host instead of bool for success
This commit is contained in:
parent
3056624b3d
commit
662360509c
2 changed files with 9 additions and 10 deletions
|
@ -4,13 +4,14 @@ import { apiJSON } from "./sub/utils.js";
|
||||||
import { errorUnsupported } from "./sub/errors.js";
|
import { errorUnsupported } from "./sub/errors.js";
|
||||||
import loc from "../localization/manager.js";
|
import loc from "../localization/manager.js";
|
||||||
import match from "./processing/match.js";
|
import match from "./processing/match.js";
|
||||||
import { hasValidHostname, normalizeURL } from "./processing/url.js";
|
import { getHostIfValid, normalizeURL } from "./processing/url.js";
|
||||||
|
|
||||||
export async function getJSON(originalURL, lang, obj) {
|
export async function getJSON(originalURL, lang, obj) {
|
||||||
try {
|
try {
|
||||||
const url = normalizeURL(decodeURIComponent(originalURL));
|
const url = normalizeURL(decodeURIComponent(originalURL));
|
||||||
|
const host = getHostIfValid(url);
|
||||||
|
|
||||||
if (!hasValidHostname(url) || !services[host].enabled) {
|
if (!host || !services[host].enabled) {
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) });
|
return apiJSON(0, { t: errorUnsupported(lang) });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,19 +84,17 @@ export function normalizeURL(url) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasValidHostname(url) {
|
export function getHostIfValid(url) {
|
||||||
const host = psl.parse(url.hostname);
|
const host = psl.parse(url.hostname);
|
||||||
if (host.error) return false;
|
if (host.error) return;
|
||||||
|
|
||||||
const service = services[host.sld];
|
const service = services[host.sld];
|
||||||
if (!service) return false;
|
if (!service) return;
|
||||||
|
if ((service.tld ?? 'com') !== host.tld) return;
|
||||||
if ((service.tld ?? 'com') !== host.tld) return false;
|
|
||||||
|
|
||||||
const anySubdomainAllowed = service.subdomains === '*';
|
const anySubdomainAllowed = service.subdomains === '*';
|
||||||
const validSubdomain = [null, 'www', ...(service.subdomains ?? [])].includes(host.subdomain);
|
const validSubdomain = [null, 'www', ...(service.subdomains ?? [])].includes(host.subdomain);
|
||||||
if (!validSubdomain && !anySubdomainAllowed)
|
if (!validSubdomain && !anySubdomainAllowed) return;
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
return host.sld;
|
||||||
}
|
}
|
Loading…
Reference in a new issue