acmex 0.8.0

AcmeX: High-performance, extensible ACME v2 (RFC 8555) client and server in Rust, supporting multiple DNS providers, storage backends, and crypto libraries.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# AcmeX v0.4.0 - 新功能使用指南

## 📌 内置 DNS 提供商

### CloudFlare DNS-01

```rust
use acmex::{
    AcmeClient, AcmeConfig, Dns01Solver, ChallengeSolverRegistry,
    CloudFlareDnsProvider, Contact,
};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. 创建 CloudFlare 提供商
    let cf_provider = CloudFlareDnsProvider::new(
        CloudFlareConfig {
            api_token: std::env::var("CF_API_TOKEN")?,
            zone_id: std::env::var("CF_ZONE_ID")?,
        }
    );

    // 2. 配置 ACME 客户端
    let config = AcmeConfig::lets_encrypt_staging()
        .with_contact(Contact::email("admin@example.com"))
        .with_tos_agreed(true);

    let mut client = AcmeClient::new(config)?;
    client.register_account().await?;

    // 3. 创建 DNS-01 求解器
    let mut registry = ChallengeSolverRegistry::new();
    registry.register(Dns01Solver::new(
        Arc::new(cf_provider),
        "example.com".to_string(),
    ));

    // 4. 申请证书 (支持通配符)
    let domains = vec![
        "example.com".to_string(),
        "*.example.com".to_string(),
    ];

    let cert = client.issue_certificate(domains, &mut registry).await?;
    cert.save_to_files("certificate.pem", "private_key.pem")?;

    println!("✅ 通配符证书已签发!");
    Ok(())
}
```

### DigitalOcean DNS-01

```rust
use acmex::{
    AcmeClient, AcmeConfig, Dns01Solver, ChallengeSolverRegistry,
    DigitalOceanDnsProvider, Contact,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let do_provider = DigitalOceanDnsProvider::new(
        DigitalOceanConfig {
            api_token: std::env::var("DO_API_TOKEN")?,
            domain: "example.com".to_string(),
        }
    );

    // ... 其他配置类似 CloudFlare ...

    Ok(())
}
```

### Linode DNS-01

```rust
use acmex::{
    LinodeDnsProvider, LinodeConfig,
};

let linode_provider = LinodeDnsProvider::new(
LinodeConfig {
api_token: std::env::var("LINODE_TOKEN") ?,
domain_id: 12345, // Linode 域名 ID
}
);
```

---

## 🔄 自动续期系统

### 基础续期示例

```rust
use acmex::{
    AcmeClient, AcmeConfig, RenewalScheduler, RenewalHook,
    storage::{FileStorage, CertificateStore},
    CertificateBundle,
};
use std::sync::Arc;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. 创建存储后端
    let storage = FileStorage::new(".acmex");
    let store = CertificateStore::new(storage);

    // 2. 创建 ACME 客户端
    let config = AcmeConfig::lets_encrypt();
    let client = AcmeClient::new(config)?;

    // 3. 创建续期调度器
    let scheduler = RenewalScheduler::new(client, store)
        .with_check_interval(Duration::from_secs(3600)) // 每小时检查
        .with_renew_before(Duration::from_secs(30 * 24 * 3600)); // 30 天前续期

    // 4. 启动后台任务
    let domains_list = vec![
        vec!["example.com".to_string(), "www.example.com".to_string()],
        vec!["api.example.com".to_string()],
    ];

    scheduler.run(domains_list).await?;

    Ok(())
}
```

### 带续期钩子的示例

```rust
use acmex::RenewalHook;

struct LoggingHook;

impl RenewalHook for LoggingHook {
    fn before_renewal(&self, domains: &[String]) {
        tracing::info!("开始续期:{:?}", domains);
    }

    fn after_renewal(&self, domains: &[String], bundle: &acmex::CertificateBundle) {
        tracing::info!("续期成功:{:?}", domains);
        // 可以在这里部署证书到服务器
        // 示例:复制到 /etc/nginx/certs/
        let _ = std::fs::copy(
            &bundle.certificate_pem,
            format!("/etc/nginx/certs/{}.pem", domains[0]),
        );
    }

    fn on_error(&self, domains: &[String], error: &acmex::AcmeError) {
        tracing::error!("续期失败 {:?}: {}", domains, error);
        // 可以在这里发送告警邮件
    }
}

// 使用钩子
let scheduler = scheduler.with_hook(Arc::new(LoggingHook));
```

---

## 💾 证书存储后端

### 文件系统存储 (默认)

```rust
use acmex::storage::{FileStorage, CertificateStore};

let storage = FileStorage::new(".acmex");
let store = CertificateStore::new(storage);

// 保存证书
store.save(&certificate_bundle).await?;

// 加载证书
if let Some(bundle) = store.load(&["example.com"]).await? {
    println!("找到已保存的证书");
}

// 删除证书
store.delete(&["example.com"]).await?;
```

### Redis 存储

```rust
// 启用 redis feature
// cargo build --features redis

use acmex::storage::{RedisStorage, CertificateStore};

let redis_storage = RedisStorage::new("redis://127.0.0.1:6379")?;
let store = CertificateStore::new(redis_storage);

// API 与文件存储相同
store.save(&certificate_bundle).await?;
```

### 加密存储

```rust
use acmex::storage::{FileStorage, EncryptedStorage, CertificateStore};
use rand::RngCore;

// 生成 256-bit 加密密钥
let mut key = [0u8; 32];
rand::rngs::OsRng.fill_bytes(&mut key);

// 创建加密的文件存储
let file_storage = FileStorage::new(".acmex");
let encrypted_storage = EncryptedStorage::new(file_storage, key);
let store = CertificateStore::new(encrypted_storage);

// 所有数据自动加密存储
store.save(&certificate_bundle).await?;
```

### Redis + 加密存储

```rust
use acmex::storage::{RedisStorage, EncryptedStorage, CertificateStore};

let redis_storage = RedisStorage::new("redis://127.0.0.1:6379") ?;
let encrypted = EncryptedStorage::new(redis_storage, key);
let store = CertificateStore::new(encrypted);

// Redis 中的数据被加密存储
store.save( & certificate_bundle).await?;
```

---

## 📊 Prometheus 指标

### 基础使用

```rust
use acmex::MetricsRegistry;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let metrics = MetricsRegistry::new();

    // 记录请求
    metrics.requests_total.inc();

    // 记录续期
    metrics.renewals_total.inc();

    // 更新当前管理的证书数
    metrics.certs_managed.set(5);

    // 输出 Prometheus 格式
    let text = metrics.gather_text();
    println!("{}", text);

    Ok(())
}
```

### 与 Axum 服务器集成

```rust
use axum::{
    routing::get,
    Router,
};
use std::sync::Arc;

async fn metrics_handler(
    axum::extract::State(metrics): axum::extract::State<Arc<MetricsRegistry>>
) -> String {
    metrics.gather_text()
}

#[tokio::main]
async fn main() {
    let metrics = Arc::new(MetricsRegistry::new());

    let app = Router::new()
        .route("/metrics", get(metrics_handler))
        .with_state(metrics);

    let listener = tokio::net::TcpListener::bind("127.0.0.1:9090")
        .await
        .unwrap();

    axum::serve(listener, app).await.unwrap();
}
```

---

## 🎛️ CLI 工具使用

### 申请证书

```bash
# 基础用法
acmex obtain --domains example.com --email admin@example.com

# 多个域名
acmex obtain \
  --domains example.com \
  --domains www.example.com \
  --email admin@example.com

# 使用 DNS-01 和生产环境
acmex obtain \
  --domains example.com \
  --domains "*.example.com" \
  --email admin@example.com \
  --challenge dns-01 \
  --prod

# 自定义输出路径
acmex obtain \
  --domains example.com \
  --email admin@example.com \
  --cert-path /etc/ssl/certs/example.pem \
  --key-path /etc/ssl/private/example.key
```

### 续期证书

```bash
# 续期单个域名
acmex renew --domains example.com

# 续期多个域名
acmex renew \
  --domains example.com \
  --domains www.example.com

# 强制续期
acmex renew --domains example.com --force
```

### 启动续期守护程序

```bash
# 使用默认配置
acmex daemon --storage-dir .acmex --interval 3600

# 使用配置文件
acmex daemon --config /etc/acmex/daemon.toml
```

### 查看证书信息

```bash
acmex info --cert certificate.pem
```

### 日志级别控制

```bash
# Debug 日志
acmex --log-level debug obtain --domains example.com

# 生产环境 (Info 日志)
acmex --log-level info daemon
```

---

## 🔧 Feature Flags 使用

### 最小化构建 (仅库)

```bash
cargo build --release
```

### 完整构建 (所有功能)

```bash
cargo build --release \
  --features dns-cloudflare,dns-route53,dns-digitalocean,dns-linode,redis,metrics,cli
```

### 特定组合

```bash
# CloudFlare + Redis + 指标
cargo build --features dns-cloudflare,redis,metrics

# 所有 DNS 提供商 + 加密
cargo build --features dns-cloudflare,dns-route53,dns-digitalocean,dns-linode

# CLI 工具
cargo build --features cli --bin acmex
```

### 使用 Ring 而不是 AWS-LC

```bash
cargo build --no-default-features --features ring-crypto,dns-cloudflare
```

---

## 📋 高级配置示例

### 多提供商 DNS-01

```rust
use acmex::{Dns01Solver, ChallengeSolverRegistry};
use std::sync::Arc;

let mut registry = ChallengeSolverRegistry::new();

// 为不同的域名注册不同的 DNS 提供商
registry.register(Dns01Solver::new(
Arc::new(CloudFlareDnsProvider::new(cf_config)),
"example.com".to_string(),
));

registry.register(Dns01Solver::new(
Arc::new(DigitalOceanDnsProvider::new(do_config)),
"api.otherdomain.com".to_string(),
));

// ACME 客户端会自动使用合适的提供商
```

### 完整的企业部署

```rust
use acmex::*;
use std::sync::Arc;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. 初始化日志
    tracing_subscriber::fmt::init();

    // 2. 存储配置 (加密 Redis)
    let redis_storage = storage::RedisStorage::new("redis://redis.local:6379")?;
    let encrypted = storage::EncryptedStorage::new(redis_storage, key);
    let store = storage::CertificateStore::new(encrypted);

    // 3. 创建 ACME 客户端
    let config = AcmeConfig::lets_encrypt()
        .with_contact(Contact::email("admin@company.com"))
        .with_tos_agreed(true);

    let client = AcmeClient::new(config)?;

    // 4. 创建续期调度器
    let scheduler = RenewalScheduler::new(client, store)
        .with_check_interval(Duration::from_secs(3600))
        .with_renew_before(Duration::from_secs(30 * 24 * 3600))
        .with_hook(Arc::new(CompanyRenewalHook));

    // 5. 启动 Prometheus 指标服务
    let metrics = Arc::new(MetricsRegistry::new());
    tokio::spawn(metrics_server(metrics.clone()));

    // 6. 启动续期守护程序
    let domains = vec![
        vec!["company.com".to_string(), "www.company.com".to_string()],
        vec!["api.company.com".to_string()],
        vec!["*.internal.company.com".to_string()],
    ];

    scheduler.run(domains).await?;

    Ok(())
}

struct CompanyRenewalHook;

impl RenewalHook for CompanyRenewalHook {
    fn after_renewal(&self, domains: &[String], bundle: &CertificateBundle) {
        // 1. 部署证书
        deploy_certificate(domains, bundle);

        // 2. 重加载服务
        reload_nginx();

        // 3. 通知监控系统
        notify_monitoring("Certificate renewed", domains);
    }

    fn on_error(&self, domains: &[String], error: &AcmeError) {
        // 发送告警
        send_alert(&format!("Certificate renewal failed: {}", error), domains);
    }
}
```

---

## 🚀 性能优化建议

### 1. 使用 Redis 存储

```rust
// 相比文件存储快 5-10 倍
let storage = RedisStorage::new("redis://...") ?;
```

### 2. 调整检查间隔

```rust
// 每 6 小时检查一次 (而不是每小时)
.with_check_interval(Duration::from_secs(6 * 3600))
```

### 3. 批量操作

```rust
// 在一个续期周期中处理多个域名
let domains = vec![
    vec!["site1.com".to_string()],
    vec!["site2.com".to_string()],
    // ...
];
```

---

**版本**: v0.4.0  
**最后更新**: 2026-02-07