126 lines
2.9 KiB
JavaScript

import axios from "axios";
import https from "https";
import dns from "node:dns";
dns.setDefaultResultOrder("ipv4first");
const API = "vvQjauyYQ2YDY2Au-_DC4QO5MUPFiHNn";
// const SERVER = "https://host.metatronhost.com:8083/api/";
const SERVER = "https://127.0.0.1:8083/api/";
// Ignore self-signed SSL
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
// Generic Hestia API function
async function hestia(cmd, args = {}) {
const form = new URLSearchParams({
hash: API,
cmd,
...args,
});
// const res = await axios.post(SERVER, form, { httpsAgent });
const res = await axios.post(SERVER, form, {
httpsAgent,
family: 4,
});
return res.data;
}
// 1) Fetch raw DNS table (your way)
async function getRawDomainList(user) {
const form = new URLSearchParams({
hash: API,
cmd: "v-list-dns-domains",
arg1: user,
});
const res = await axios.post(SERVER, form, {
httpsAgent,
family: 4,
});
// const res = await axios.post(SERVER, form, { httpsAgent });
return res.data; // plain text
}
// 2) Parse domains from the table
function parseDomainsFromTable(text) {
const lines = text.split("\n");
const domains = [];
for (let line of lines) {
line = line.trim();
if (!line.includes(".")) continue;
if (line.startsWith("DOMAIN")) continue;
if (line.startsWith("------")) continue;
const parts = line.split(/\s+/);
const domain = parts[0];
if (domain && domain.includes(".")) {
domains.push(domain);
}
}
return domains;
}
// 3) Remove SSL
async function removeSsl(user, domain) {
return hestia("v-delete-web-domain-ssl", {
arg1: user,
arg2: domain,
});
}
// 4) Add Let's Encrypt SSL (no email param)
async function addLetsEncrypt(user, domain) {
// v-add-letsencrypt-domain USER DOMAIN [ALIASES] [MAIL]
// Here: generate SSL for domain + www.domain, no mail cert
return hestia("v-add-letsencrypt-domain", {
arg1: user,
arg2: domain,
//arg3: `${domain}`, // aliases
// no arg4 (MAIL) -> defaults to "no"
});
}
(async () => {
const user = "user";
try {
console.log("🔍 Fetching DNS domain table...");
const table = await getRawDomainList(user);
console.log(table);
console.log("\n🔍 Parsing domain names...");
const domains = parseDomainsFromTable(table);
console.log("Parsed domains:", domains);
if (domains.length === 0) {
console.log("❌ No domains found.");
return;
}
const domain = "test.metatronhost.com"; // or domains[0] if you want auto-pick
console.log("\n🟦 Selected domain:", domain);
console.log("\n🔧 Removing old SSL...");
console.log(await removeSsl(user, domain));
console.log("\n🔐 Adding Let's Encrypt SSL...");
console.log(await addLetsEncrypt(user, domain));
console.log("\n✅ SSL refreshed successfully for:", domain);
} catch (err) {
console.error("❌ ERROR:", err.response?.data || err.message || err);
}
})();