mirror of
https://github.com/certd/certd.git
synced 2026-07-10 15:17:32 +08:00
chore: format
This commit is contained in:
@@ -115,9 +115,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
}
|
||||
|
||||
const withoutTrailingSlash = trimmed.replace(/\/+$/, "");
|
||||
return withoutTrailingSlash.endsWith("/api")
|
||||
? withoutTrailingSlash.slice(0, -4)
|
||||
: withoutTrailingSlash;
|
||||
return withoutTrailingSlash.endsWith("/api") ? withoutTrailingSlash.slice(0, -4) : withoutTrailingSlash;
|
||||
}
|
||||
|
||||
private describeError(error: unknown, action: string): Error {
|
||||
@@ -158,10 +156,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
});
|
||||
}
|
||||
|
||||
async getCertificatesWithExpand(
|
||||
searchQuery?: string,
|
||||
expand: string[] = []
|
||||
): Promise<Certificate[]> {
|
||||
async getCertificatesWithExpand(searchQuery?: string, expand: string[] = []): Promise<Certificate[]> {
|
||||
return await this.requestWithAuth<Certificate[]>({
|
||||
method: "GET",
|
||||
url: "/nginx/certificates",
|
||||
@@ -174,15 +169,12 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
|
||||
async findCustomCertificateByNiceName(niceName: string): Promise<Certificate | undefined> {
|
||||
const certificates = await this.getCertificates(niceName);
|
||||
return certificates.find((certificate) => {
|
||||
return certificates.find(certificate => {
|
||||
return certificate.provider === "other" && certificate.nice_name === niceName;
|
||||
});
|
||||
}
|
||||
|
||||
async createCustomCertificate(
|
||||
niceName: string,
|
||||
domainNames: string[] = []
|
||||
): Promise<Certificate> {
|
||||
async createCustomCertificate(niceName: string, domainNames: string[] = []): Promise<Certificate> {
|
||||
return await this.requestWithAuth<Certificate>({
|
||||
method: "POST",
|
||||
url: "/nginx/certificates",
|
||||
@@ -277,9 +269,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
}
|
||||
|
||||
if (!this.totpSecret) {
|
||||
throw new Error(
|
||||
"登录失败:该 Nginx Proxy Manager 账号启用了 2FA,但未配置 totpSecret"
|
||||
);
|
||||
throw new Error("登录失败:该 Nginx Proxy Manager 账号启用了 2FA,但未配置 totpSecret");
|
||||
}
|
||||
|
||||
let code: string;
|
||||
@@ -305,13 +295,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
return completedLogin.token;
|
||||
}
|
||||
|
||||
private async requestWithAuth<T>(config: {
|
||||
method: string;
|
||||
url: string;
|
||||
params?: Record<string, unknown>;
|
||||
data?: unknown;
|
||||
headers?: Record<string, string>;
|
||||
}): Promise<T> {
|
||||
private async requestWithAuth<T>(config: { method: string; url: string; params?: Record<string, unknown>; data?: unknown; headers?: Record<string, string> }): Promise<T> {
|
||||
const token = await this.login();
|
||||
const headers = {
|
||||
...(config.headers ?? {}),
|
||||
@@ -324,13 +308,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
});
|
||||
}
|
||||
|
||||
private async request<T>(config: {
|
||||
method: string;
|
||||
url: string;
|
||||
params?: Record<string, unknown>;
|
||||
data?: unknown;
|
||||
headers?: Record<string, string>;
|
||||
}): Promise<T> {
|
||||
private async request<T>(config: { method: string; url: string; params?: Record<string, unknown>; data?: unknown; headers?: Record<string, string> }): Promise<T> {
|
||||
const action = `${config.method ?? "GET"} ${config.url ?? "/"}`;
|
||||
try {
|
||||
const response = await this.ctx.http.request({
|
||||
@@ -340,9 +318,11 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
data: config.data,
|
||||
headers: config.headers,
|
||||
timeout: 30000,
|
||||
httpsAgent: this.ignoreTls ? {
|
||||
rejectUnauthorized: false
|
||||
} : undefined,
|
||||
httpsAgent: this.ignoreTls
|
||||
? {
|
||||
rejectUnauthorized: false,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
@@ -352,9 +332,7 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
|
||||
async onTestRequest(): Promise<string> {
|
||||
const result = await this.verifyAccess();
|
||||
this.ctx.logger.info(
|
||||
`Nginx Proxy Manager 授权验证成功,找到 ${result.proxyHostCount} 个代理主机`
|
||||
);
|
||||
this.ctx.logger.info(`Nginx Proxy Manager 授权验证成功,找到 ${result.proxyHostCount} 个代理主机`);
|
||||
return `成功(${result.proxyHostCount} 个代理主机)`;
|
||||
}
|
||||
|
||||
@@ -363,4 +341,4 @@ export class NginxProxyManagerAccess extends BaseAccess {
|
||||
}
|
||||
}
|
||||
|
||||
new NginxProxyManagerAccess();
|
||||
new NginxProxyManagerAccess();
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from "./plugin-deploy-to-proxy-hosts.js";
|
||||
export * from "./plugin-deploy-to-proxy-hosts.js";
|
||||
|
||||
+15
-40
@@ -1,10 +1,4 @@
|
||||
import {
|
||||
AbstractTaskPlugin,
|
||||
IsTaskPlugin,
|
||||
pluginGroups,
|
||||
RunStrategy,
|
||||
TaskInput,
|
||||
} from "@certd/pipeline";
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
||||
import { CertInfo, CertReader, createCertDomainGetterInputDefine } from "@certd/plugin-cert";
|
||||
import { NginxProxyManagerAccess, ProxyHost } from "../access.js";
|
||||
|
||||
@@ -94,7 +88,9 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
cleanupMatchingCertificates = false;
|
||||
|
||||
private normalizeDomain(domain: string): string {
|
||||
return String(domain ?? "").trim().toLowerCase();
|
||||
return String(domain ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
private wildcardMatches(pattern: string, candidate: string): boolean {
|
||||
@@ -110,11 +106,7 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
const normalizedLeft = this.normalizeDomain(left);
|
||||
const normalizedRight = this.normalizeDomain(right);
|
||||
|
||||
return (
|
||||
normalizedLeft === normalizedRight ||
|
||||
this.wildcardMatches(normalizedLeft, normalizedRight) ||
|
||||
this.wildcardMatches(normalizedRight, normalizedLeft)
|
||||
);
|
||||
return normalizedLeft === normalizedRight || this.wildcardMatches(normalizedLeft, normalizedRight) || this.wildcardMatches(normalizedRight, normalizedLeft);
|
||||
}
|
||||
|
||||
private sanitizeDomainSegment(value: string): string {
|
||||
@@ -147,7 +139,7 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
|
||||
private resolveCertificateDomains(cert: CertInfo, configuredDomains: string | string[] | null | undefined): string[] {
|
||||
const configured = this.normalizeStringList(configuredDomains)
|
||||
.map((value) => String(value).trim())
|
||||
.map(value => String(value).trim())
|
||||
.filter(Boolean);
|
||||
|
||||
if (configured.length > 0) {
|
||||
@@ -168,9 +160,7 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
}
|
||||
|
||||
const hostDomains = host.domain_names ?? [];
|
||||
return hostDomains.some((hostDomain) =>
|
||||
certDomains.some((certDomain) => this.isDomainMatch(hostDomain, certDomain))
|
||||
);
|
||||
return hostDomains.some(hostDomain => certDomains.some(certDomain => this.isDomainMatch(hostDomain, certDomain)));
|
||||
}
|
||||
|
||||
private buildProxyHostOptions(hosts: ProxyHost[], certDomains: string[]) {
|
||||
@@ -215,19 +205,14 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
return Array.from(
|
||||
new Set(
|
||||
this.normalizeStringList(proxyHostIds as string | string[] | null | undefined)
|
||||
.map((value) => Number.parseInt(String(value), 10))
|
||||
.filter((value) => Number.isInteger(value) && value > 0)
|
||||
.map(value => Number.parseInt(String(value), 10))
|
||||
.filter(value => Number.isInteger(value) && value > 0)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private certificateHasBindings(certificate: { proxy_hosts?: unknown[]; redirection_hosts?: unknown[]; dead_hosts?: unknown[]; streams?: unknown[] }): boolean {
|
||||
return (
|
||||
(certificate.proxy_hosts?.length ?? 0) > 0 ||
|
||||
(certificate.redirection_hosts?.length ?? 0) > 0 ||
|
||||
(certificate.dead_hosts?.length ?? 0) > 0 ||
|
||||
(certificate.streams?.length ?? 0) > 0
|
||||
);
|
||||
return (certificate.proxy_hosts?.length ?? 0) > 0 || (certificate.redirection_hosts?.length ?? 0) > 0 || (certificate.dead_hosts?.length ?? 0) > 0 || (certificate.streams?.length ?? 0) > 0;
|
||||
}
|
||||
|
||||
async execute(): Promise<void> {
|
||||
@@ -238,8 +223,7 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
throw new Error("请至少选择一个 Nginx Proxy Manager 代理主机");
|
||||
}
|
||||
|
||||
const certificateLabel =
|
||||
this.certificateLabel?.trim() || this.buildDefaultCertificateLabel(this.cert);
|
||||
const certificateLabel = this.certificateLabel?.trim() || this.buildDefaultCertificateLabel(this.cert);
|
||||
const certificateDomains = this.resolveCertificateDomains(this.cert, this.certDomains);
|
||||
|
||||
let certificate = await access.findCustomCertificateByNiceName(certificateLabel);
|
||||
@@ -285,13 +269,8 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
}
|
||||
|
||||
private async cleanupOldCertificates(access: NginxProxyManagerAccess, currentCertificateId: number): Promise<void> {
|
||||
const certificates = await access.getCertificatesWithExpand(undefined, [
|
||||
"proxy_hosts",
|
||||
"redirection_hosts",
|
||||
"dead_hosts",
|
||||
"streams",
|
||||
]);
|
||||
const candidates = certificates.filter((certificate) => {
|
||||
const certificates = await access.getCertificatesWithExpand(undefined, ["proxy_hosts", "redirection_hosts", "dead_hosts", "streams"]);
|
||||
const candidates = certificates.filter(certificate => {
|
||||
return certificate.id !== currentCertificateId;
|
||||
});
|
||||
|
||||
@@ -321,11 +300,7 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
}
|
||||
|
||||
if (deletedIds.length > 0) {
|
||||
this.logger.info(
|
||||
`自动清理完成,共删除 ${deletedIds.length} 张旧证书:${deletedIds
|
||||
.map((id) => `#${id}`)
|
||||
.join(", ")}`
|
||||
);
|
||||
this.logger.info(`自动清理完成,共删除 ${deletedIds.length} 张旧证书:${deletedIds.map(id => `#${id}`).join(", ")}`);
|
||||
} else {
|
||||
this.logger.info("未删除任何旧证书");
|
||||
}
|
||||
@@ -340,4 +315,4 @@ export class NginxProxyManagerDeploy extends AbstractTaskPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
new NginxProxyManagerDeploy();
|
||||
new NginxProxyManagerDeploy();
|
||||
|
||||
Reference in New Issue
Block a user