2023-01-29 14:44:10 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* ACME auto helper
|
|
|
|
|
|
*/
|
2025-04-04 23:16:25 +08:00
|
|
|
|
import { readCsrDomains } from "./crypto/index.js";
|
|
|
|
|
|
import { wait } from "./wait.js";
|
|
|
|
|
|
import { CancelError } from "./error.js";
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const defaultOpts = {
|
|
|
|
|
|
csr: null,
|
|
|
|
|
|
email: null,
|
|
|
|
|
|
preferredChain: null,
|
|
|
|
|
|
termsOfServiceAgreed: false,
|
|
|
|
|
|
skipChallengeVerification: false,
|
2025-04-04 23:16:25 +08:00
|
|
|
|
challengePriority: ["http-01", "dns-01"],
|
2024-08-30 18:50:53 +08:00
|
|
|
|
challengeCreateFn: async () => {
|
2025-04-04 23:16:25 +08:00
|
|
|
|
throw new Error("Missing challengeCreateFn()");
|
2024-08-30 18:50:53 +08:00
|
|
|
|
},
|
|
|
|
|
|
challengeRemoveFn: async () => {
|
2025-04-04 23:16:25 +08:00
|
|
|
|
throw new Error("Missing challengeRemoveFn()");
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* ACME client auto mode
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {AcmeClient} client ACME client
|
|
|
|
|
|
* @param {object} userOpts Options
|
|
|
|
|
|
* @returns {Promise<buffer>} Certificate
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
export default async (client, userOpts) => {
|
2024-05-22 19:24:07 +00:00
|
|
|
|
const opts = { ...defaultOpts, ...userOpts };
|
2023-01-29 14:44:10 +08:00
|
|
|
|
const accountPayload = { termsOfServiceAgreed: opts.termsOfServiceAgreed };
|
|
|
|
|
|
|
|
|
|
|
|
if (!Buffer.isBuffer(opts.csr)) {
|
|
|
|
|
|
opts.csr = Buffer.from(opts.csr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (opts.email) {
|
|
|
|
|
|
accountPayload.contact = [`mailto:${opts.email}`];
|
|
|
|
|
|
}
|
2024-07-04 01:14:09 +08:00
|
|
|
|
if (opts.externalAccountBinding) {
|
|
|
|
|
|
accountPayload.externalAccountBinding = opts.externalAccountBinding;
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
2025-10-15 23:03:59 +08:00
|
|
|
|
const log = (...args)=>{
|
|
|
|
|
|
return client.logger.info(...args);
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Register account
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Checking account");
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
client.getAccountUrl();
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Account URL already exists, skipping account registration( 证书申请账户已存在,跳过注册 )");
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log("[auto] Registering account (注册证书申请账户)");
|
2023-01-29 14:44:10 +08:00
|
|
|
|
await client.createAccount(accountPayload);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Parse domains from CSR
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Parsing domains from Certificate Signing Request ");
|
2024-05-21 19:24:05 +00:00
|
|
|
|
const { commonName, altNames } = readCsrDomains(opts.csr);
|
|
|
|
|
|
const uniqueDomains = Array.from(new Set([commonName].concat(altNames).filter((d) => d)));
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
log(`[auto] Resolved ${uniqueDomains.length} unique domains from parsing the Certificate Signing Request`);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Place order
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Placing new certificate order with ACME provider");
|
|
|
|
|
|
const orderPayload = { identifiers: uniqueDomains.map((d) => ({ type: "dns", value: d })) };
|
2025-06-06 15:12:24 +08:00
|
|
|
|
if (opts.profile && client.sslProvider === 'letsencrypt' ){
|
|
|
|
|
|
orderPayload.profile = opts.profile;
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
const order = await client.createOrder(orderPayload);
|
|
|
|
|
|
const authorizations = await client.getAuthorizations(order);
|
|
|
|
|
|
|
|
|
|
|
|
log(`[auto] Placed certificate order successfully, received ${authorizations.length} identity authorizations`);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Resolve and satisfy challenges
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Resolving and satisfying authorization challenges");
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
2024-03-06 18:36:10 +08:00
|
|
|
|
const clearTasks = [];
|
2025-04-04 23:16:25 +08:00
|
|
|
|
const localVerifyTasks = [];
|
|
|
|
|
|
const completeChallengeTasks = [];
|
2024-03-06 18:36:10 +08:00
|
|
|
|
|
2023-01-29 14:44:10 +08:00
|
|
|
|
const challengeFunc = async (authz) => {
|
|
|
|
|
|
const d = authz.identifier.value;
|
|
|
|
|
|
let challengeCompleted = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* Skip authz that already has valid status */
|
2025-04-04 23:16:25 +08:00
|
|
|
|
if (authz.status === "valid") {
|
2023-01-29 14:44:10 +08:00
|
|
|
|
log(`[auto] [${d}] Authorization already has valid status, no need to complete challenges`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-03 01:17:20 +08:00
|
|
|
|
const keyAuthorizationGetter = async (challenge) => {
|
|
|
|
|
|
return await client.getChallengeKeyAuthorization(challenge);
|
2025-04-04 23:16:25 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async function deactivateAuth(e) {
|
|
|
|
|
|
log(`[auto] [${d}] Unable to complete challenge: ${e.message}`);
|
|
|
|
|
|
try {
|
|
|
|
|
|
log(`[auto] [${d}] Deactivating failed authorization`);
|
|
|
|
|
|
await client.deactivateAuthorization(authz);
|
|
|
|
|
|
} catch (f) {
|
|
|
|
|
|
/* Suppress deactivateAuthorization() errors */
|
|
|
|
|
|
log(`[auto] [${d}] Authorization deactivation threw error: ${f.message}`);
|
|
|
|
|
|
}
|
2025-01-03 01:17:20 +08:00
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log(`[auto] [${d}] Trigger challengeCreateFn()`);
|
2025-01-03 01:17:20 +08:00
|
|
|
|
try {
|
2025-04-27 01:52:42 +08:00
|
|
|
|
const { recordReq, recordRes, dnsProvider, challenge, keyAuthorization ,httpUploader} = await opts.challengeCreateFn(authz, keyAuthorizationGetter);
|
2025-04-04 23:16:25 +08:00
|
|
|
|
clearTasks.push(async () => {
|
|
|
|
|
|
/* Trigger challengeRemoveFn(), suppress errors */
|
|
|
|
|
|
log(`[auto] [${d}] Trigger challengeRemoveFn()`);
|
|
|
|
|
|
try {
|
2025-04-27 01:52:42 +08:00
|
|
|
|
await opts.challengeRemoveFn(authz, challenge, keyAuthorization, recordReq, recordRes, dnsProvider,httpUploader);
|
2025-04-04 23:16:25 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log(`[auto] [${d}] challengeRemoveFn threw error: ${e.message}`);
|
2023-01-29 14:44:10 +08:00
|
|
|
|
}
|
2025-04-04 23:16:25 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
localVerifyTasks.push(async () => {
|
|
|
|
|
|
/* Challenge verification */
|
|
|
|
|
|
log(`[auto] [${d}] 开始本地验证, type = ${challenge.type}`);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await client.verifyChallenge(authz, challenge);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log(`[auto] [${d}] 本地验证失败,尝试请求ACME提供商获取状态: ${e.message}`);
|
2023-01-29 14:44:10 +08:00
|
|
|
|
}
|
2025-04-04 23:16:25 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
completeChallengeTasks.push(async () => {
|
2023-01-29 14:44:10 +08:00
|
|
|
|
/* Complete challenge and wait for valid status */
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log(`[auto] [${d}] 请求ACME提供商完成验证`);
|
|
|
|
|
|
try{
|
|
|
|
|
|
await client.completeChallenge(challenge);
|
|
|
|
|
|
}catch (e) {
|
|
|
|
|
|
await deactivateAuth(e);
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
challengeCompleted = true;
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log(`[auto] [${d}] 等待返回valid状态`);
|
2025-04-05 00:24:57 +08:00
|
|
|
|
await client.waitForValidStatus(challenge,d);
|
2025-04-04 23:16:25 +08:00
|
|
|
|
});
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log(`[auto] [${d}] challengeCreateFn threw error: ${e.message}`);
|
|
|
|
|
|
await deactivateAuth(e);
|
2023-01-29 14:44:10 +08:00
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
2025-04-04 23:16:25 +08:00
|
|
|
|
|
2023-01-29 14:44:10 +08:00
|
|
|
|
};
|
2024-08-23 13:15:06 +08:00
|
|
|
|
const domainSets = [];
|
2023-01-29 14:44:10 +08:00
|
|
|
|
|
2024-08-23 13:15:06 +08:00
|
|
|
|
authorizations.forEach((authz) => {
|
|
|
|
|
|
const d = authz.identifier.value;
|
2024-10-22 16:21:35 +08:00
|
|
|
|
log(`authorization:domain = ${d}, value = ${JSON.stringify(authz)}`);
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
if (authz.status === "valid") {
|
2024-10-22 16:21:35 +08:00
|
|
|
|
log(`[auto] [${d}] Authorization already has valid status, no need to complete challenges`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-08-23 13:15:06 +08:00
|
|
|
|
let setd = false;
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
|
|
for (const group of domainSets) {
|
|
|
|
|
|
if (!group[d]) {
|
|
|
|
|
|
group[d] = authz;
|
|
|
|
|
|
setd = true;
|
2024-10-22 16:21:35 +08:00
|
|
|
|
break;
|
2024-08-23 13:15:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!setd) {
|
|
|
|
|
|
const group = {};
|
|
|
|
|
|
group[d] = authz;
|
|
|
|
|
|
domainSets.push(group);
|
|
|
|
|
|
}
|
2023-01-29 14:44:10 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-10-22 16:21:35 +08:00
|
|
|
|
// log(`domainSets:${JSON.stringify(domainSets)}`);
|
|
|
|
|
|
|
2024-08-23 13:15:06 +08:00
|
|
|
|
const allChallengePromises = [];
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2025-04-04 23:16:25 +08:00
|
|
|
|
const challengePromises = [];
|
|
|
|
|
|
allChallengePromises.push(challengePromises);
|
2024-08-23 13:15:06 +08:00
|
|
|
|
for (const domainSet of domainSets) {
|
|
|
|
|
|
// eslint-disable-next-line guard-for-in,no-restricted-syntax
|
|
|
|
|
|
for (const domain in domainSet) {
|
|
|
|
|
|
const authz = domainSet[domain];
|
|
|
|
|
|
challengePromises.push(async () => {
|
|
|
|
|
|
log(`[auto] [${domain}] Starting challenge`);
|
|
|
|
|
|
await challengeFunc(authz);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log(`[auto] challengeGroups:${allChallengePromises.length}`);
|
2024-08-30 18:50:53 +08:00
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
async function runAllPromise(tasks) {
|
2024-06-15 02:17:34 +08:00
|
|
|
|
let promise = Promise.resolve();
|
|
|
|
|
|
tasks.forEach((task) => {
|
|
|
|
|
|
promise = promise.then(task);
|
|
|
|
|
|
});
|
|
|
|
|
|
return promise;
|
2023-01-29 14:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-09 11:32:06 +08:00
|
|
|
|
async function runPromisePa(tasks, waitTime = 8000) {
|
2024-06-26 18:36:11 +08:00
|
|
|
|
const results = [];
|
2025-06-09 11:32:06 +08:00
|
|
|
|
let j = 0
|
2024-06-26 18:36:11 +08:00
|
|
|
|
// eslint-disable-next-line no-await-in-loop,no-restricted-syntax
|
|
|
|
|
|
for (const task of tasks) {
|
2025-06-09 11:32:06 +08:00
|
|
|
|
j++
|
|
|
|
|
|
log(`开始第${j}个任务`);
|
2024-06-26 18:36:11 +08:00
|
|
|
|
results.push(task());
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2025-07-07 00:21:23 +08:00
|
|
|
|
log(`wait ${Math.floor(waitTime/1000)}s`)
|
2025-04-04 23:16:25 +08:00
|
|
|
|
await wait(waitTime);
|
2024-06-26 18:36:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.all(results);
|
|
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log(`开始challenge,共${allChallengePromises.length}组`);
|
|
|
|
|
|
let i = 0;
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
|
|
for (const challengePromises of allChallengePromises) {
|
|
|
|
|
|
i += 1;
|
|
|
|
|
|
log(`开始第${i}组`);
|
|
|
|
|
|
if (opts.signal && opts.signal.aborted) {
|
|
|
|
|
|
throw new CancelError("用户取消");
|
|
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
|
|
2025-05-06 11:04:02 +08:00
|
|
|
|
const waitDnsDiffuseTime = opts.waitDnsDiffuseTime || 30;
|
2025-04-04 23:16:25 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
|
await runPromisePa(challengePromises);
|
|
|
|
|
|
if (opts.skipChallengeVerification === true) {
|
|
|
|
|
|
log(`跳过本地验证(skipChallengeVerification=true),等待 60s`);
|
|
|
|
|
|
await wait(60 * 1000);
|
|
|
|
|
|
} else {
|
2025-06-09 11:32:06 +08:00
|
|
|
|
log("开始本地校验")
|
2025-04-04 23:16:25 +08:00
|
|
|
|
await runPromisePa(localVerifyTasks, 1000);
|
2025-05-06 11:04:02 +08:00
|
|
|
|
log(`本地校验完成,等待${waitDnsDiffuseTime}s`)
|
|
|
|
|
|
await wait(waitDnsDiffuseTime * 1000)
|
2024-10-22 16:21:35 +08:00
|
|
|
|
}
|
2025-04-04 23:16:25 +08:00
|
|
|
|
|
2025-10-13 23:16:03 +08:00
|
|
|
|
log("开始向提供商请求检查验证");
|
2025-04-04 23:16:25 +08:00
|
|
|
|
await runPromisePa(completeChallengeTasks, 1000);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log(`证书申请失败${e.message}`);
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
// letsencrypt 如果同时检出两个TXT记录,会以第一个为准,就会校验失败,所以需要提前删除
|
|
|
|
|
|
// zerossl 此方式测试无问题
|
2024-10-22 16:21:35 +08:00
|
|
|
|
log(`清理challenge痕迹,length:${clearTasks.length}`);
|
|
|
|
|
|
try {
|
2025-04-04 23:16:25 +08:00
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2024-10-22 16:21:35 +08:00
|
|
|
|
await runAllPromise(clearTasks);
|
2025-04-04 23:16:25 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
log("清理challenge失败");
|
2024-10-22 16:21:35 +08:00
|
|
|
|
log(e);
|
|
|
|
|
|
}
|
2024-10-22 11:23:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
|
|
|
|
|
|
log("challenge结束");
|
2024-08-30 18:50:53 +08:00
|
|
|
|
|
|
|
|
|
|
// log('[auto] Waiting for challenge valid status');
|
|
|
|
|
|
// await Promise.all(challengePromises);
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Finalize order and download certificate
|
|
|
|
|
|
*/
|
2024-03-08 17:42:47 +08:00
|
|
|
|
|
2025-04-04 23:16:25 +08:00
|
|
|
|
log("[auto] Finalizing order and downloading certificate");
|
2024-08-30 18:50:53 +08:00
|
|
|
|
const finalized = await client.finalizeOrder(order, opts.csr);
|
|
|
|
|
|
const res = await client.getCertificate(finalized, opts.preferredChain);
|
|
|
|
|
|
return res;
|
2024-03-08 17:42:47 +08:00
|
|
|
|
// try {
|
|
|
|
|
|
// await Promise.allSettled(challengePromises);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// finally {
|
|
|
|
|
|
// log('清理challenge');
|
|
|
|
|
|
// await Promise.allSettled(clearTasks);
|
|
|
|
|
|
// }
|
2023-01-29 14:44:10 +08:00
|
|
|
|
};
|