fix cpanel
This commit is contained in:
@@ -76,9 +76,13 @@ async function parseResponse(res, debug = false) {
|
|||||||
if (debug) console.log('[cpanel] full envelope:', JSON.stringify(envelope, null, 2));
|
if (debug) console.log('[cpanel] full envelope:', JSON.stringify(envelope, null, 2));
|
||||||
if (res.status >= 400) throw new Error(`cPanel HTTP ${res.status}`);
|
if (res.status >= 400) throw new Error(`cPanel HTTP ${res.status}`);
|
||||||
|
|
||||||
// UAPI can nest the real payload under result{} or return it flat
|
// UAPI wraps the payload in result{} but some installs omit it
|
||||||
const result = envelope.result ?? envelope;
|
const result = envelope.result ?? envelope;
|
||||||
if (result.status === 0) throw new Error(`cPanel UAPI error: ${result.errors?.join(', ') ?? 'Unknown error'}`);
|
const statusVal = result.status ?? result.result;
|
||||||
|
if (statusVal === 0 || statusVal === '0') {
|
||||||
|
const msg = result.errors?.join(', ') ?? result.error ?? result.message ?? 'Unknown UAPI error';
|
||||||
|
throw new Error(`cPanel UAPI error: ${msg}`);
|
||||||
|
}
|
||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,29 +141,49 @@ async function api2(func, params = {}, debug = false) {
|
|||||||
// ─── Public API ───────────────────────────────────────────────────────────────
|
// ─── Public API ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
async function listZones() {
|
async function listZones() {
|
||||||
// Try DNS::list_zones first (cPanel v82+), fall back to DomainInfo::list_domains
|
|
||||||
let domains = [];
|
let domains = [];
|
||||||
|
|
||||||
|
// Try DNS::list_zones first (cPanel v82+).
|
||||||
|
// Fall back for ANY failure — different cPanel versions use different error messages.
|
||||||
try {
|
try {
|
||||||
const data = await uapiGet('DNS', 'list_zones');
|
const data = await uapiGet('DNS', 'list_zones');
|
||||||
if (data && data.length > 0) {
|
if (Array.isArray(data) && data.length > 0) {
|
||||||
return data.map(z => {
|
const zones = data.map(z => {
|
||||||
const name = typeof z === 'string' ? z : (z.domain ?? z.zone ?? z.name);
|
const raw = typeof z === 'string' ? z : (z.domain ?? z.zone ?? z.name ?? '');
|
||||||
|
const name = raw.replace(/\.$/, ''); // strip trailing dot if present
|
||||||
return { id: name, name };
|
return { id: name, name };
|
||||||
});
|
}).filter(z => z.name);
|
||||||
|
if (zones.length > 0) return zones;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!err.message.includes('could not find the function')) throw err;
|
console.log('[cpanel] DNS::list_zones unavailable, falling back to DomainInfo:', err.message);
|
||||||
// Function doesn't exist on this cPanel version — use DomainInfo fallback
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = await uapiGet('DomainInfo', 'list_domains');
|
// Fallback: DomainInfo::list_domains (available on all cPanel versions)
|
||||||
// Returns { main_domain, addon_domains, parked_domains, sub_domains }
|
try {
|
||||||
if (info) {
|
const info = await uapiGet('DomainInfo', 'list_domains');
|
||||||
const main = info.main_domain ? [info.main_domain] : [];
|
if (info) {
|
||||||
const addon = Array.isArray(info.addon_domains) ? info.addon_domains : [];
|
const main = info.main_domain ? [info.main_domain] : [];
|
||||||
const parked = Array.isArray(info.parked_domains) ? info.parked_domains : [];
|
const addon = Array.isArray(info.addon_domains) ? info.addon_domains : [];
|
||||||
domains = [...new Set([...main, ...addon, ...parked])];
|
const parked = Array.isArray(info.parked_domains) ? info.parked_domains : [];
|
||||||
|
// sub_domains are sub.domain.tld — only include the apex, not subs
|
||||||
|
domains = [...new Set([...main, ...addon, ...parked])].filter(Boolean);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[cpanel] DomainInfo::list_domains failed:', err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (domains.length === 0) {
|
||||||
|
// Last resort: Zone::listzone lists the raw zone names (some legacy cPanel installs)
|
||||||
|
try {
|
||||||
|
const data = await uapiGet('Zone', 'listzone');
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
domains = data.map(z => {
|
||||||
|
const raw = typeof z === 'string' ? z : (z.domain ?? z.zone ?? z.name ?? '');
|
||||||
|
return raw.replace(/\.$/, '');
|
||||||
|
}).filter(Boolean);
|
||||||
|
}
|
||||||
|
} catch { /* ignore */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
return domains.map(d => ({ id: d, name: d }));
|
return domains.map(d => ({ id: d, name: d }));
|
||||||
|
|||||||
Reference in New Issue
Block a user