Skip to content

Commit 6529eee

Browse files
author
zhangchenhao
committed
【新增】【申请】dns验证可挂http代理
1 parent dadc15d commit 6529eee

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

backend/internal/cert/apply/apply.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func GetSqlite() (*public.Sqlite, error) {
6363
return s, nil
6464
}
6565

66-
func GetDNSProvider(providerName string, creds map[string]string) (challenge.Provider, error) {
66+
func GetDNSProvider(providerName string, creds map[string]string, httpClient *http.Client) (challenge.Provider, error) {
6767
switch providerName {
6868
case "tencentcloud":
6969
config := tencentcloud.NewDefaultConfig()
@@ -106,6 +106,9 @@ func GetDNSProvider(providerName string, creds map[string]string) (challenge.Pro
106106
config := godaddy.NewDefaultConfig()
107107
config.APIKey = creds["api_key"]
108108
config.APISecret = creds["api_secret"]
109+
if httpClient != nil {
110+
config.HTTPClient = httpClient
111+
}
109112
return godaddy.NewDNSProviderConfig(config)
110113
case "namecheap":
111114
config := namecheap.NewDefaultConfig()
@@ -148,7 +151,7 @@ func GetDNSProvider(providerName string, creds map[string]string) (challenge.Pro
148151
}
149152
}
150153

151-
func GetAcmeClient(db *public.Sqlite, email, algorithm, proxy, eabId string, logger *public.Logger) (*lego.Client, error) {
154+
func GetAcmeClient(db *public.Sqlite, email, algorithm, eabId string, httpClient *http.Client, logger *public.Logger) (*lego.Client, error) {
152155
var (
153156
ca string
154157
eabData map[string]any
@@ -194,18 +197,7 @@ func GetAcmeClient(db *public.Sqlite, email, algorithm, proxy, eabId string, log
194197
config := lego.NewConfig(user)
195198
config.Certificate.KeyType = AlgorithmMap[algorithm]
196199
config.CADirURL = CADirURLMap[ca]
197-
if proxy != "" {
198-
// 构建代理 HTTP 客户端
199-
proxyURL, err := url.Parse(proxy) // 替换为你的代理地址
200-
if err != nil {
201-
return nil, fmt.Errorf("无效的代理地址: %v", err)
202-
}
203-
httpClient := &http.Client{
204-
Transport: &http.Transport{
205-
Proxy: http.ProxyURL(proxyURL),
206-
},
207-
Timeout: 30 * time.Second,
208-
}
200+
if httpClient != nil {
209201
config.HTTPClient = httpClient
210202
}
211203
client, err := lego.NewClient(config)
@@ -341,9 +333,20 @@ func Apply(cfg map[string]any, logger *public.Logger) (map[string]any, error) {
341333
if !ok {
342334
algorithm = "RSA2048"
343335
}
336+
var httpClient *http.Client
344337
proxy, ok := cfg["proxy"].(string)
345-
if !ok {
346-
proxy = ""
338+
if ok {
339+
// 构建代理 HTTP 客户端
340+
proxyURL, err := url.Parse(proxy) // 替换为你的代理地址
341+
if err != nil {
342+
return nil, fmt.Errorf("无效的代理地址: %v", err)
343+
}
344+
httpClient = &http.Client{
345+
Transport: &http.Transport{
346+
Proxy: http.ProxyURL(proxyURL),
347+
},
348+
Timeout: 30 * time.Second,
349+
}
347350
}
348351
var eabId string
349352
switch v := cfg["eabId"].(type) {
@@ -434,7 +437,7 @@ func Apply(cfg map[string]any, logger *public.Logger) (map[string]any, error) {
434437
}
435438
logger.Debug("正在申请证书,域名: " + domains)
436439
// 创建 ACME 客户端
437-
client, err := GetAcmeClient(db, email, algorithm, proxy, eabId, logger)
440+
client, err := GetAcmeClient(db, email, algorithm, eabId, httpClient, logger)
438441
if err != nil {
439442
return nil, err
440443
}
@@ -455,7 +458,7 @@ func Apply(cfg map[string]any, logger *public.Logger) (map[string]any, error) {
455458
}
456459

457460
// DNS 验证
458-
provider, err := GetDNSProvider(providerStr, providerConfig)
461+
provider, err := GetDNSProvider(providerStr, providerConfig, httpClient)
459462
if err != nil {
460463
return nil, fmt.Errorf("创建 DNS provider 失败: %v", err)
461464
}

0 commit comments

Comments
 (0)