重构项目
This commit is contained in:
		| @ -1,20 +1,18 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"SafelineAPI/internal/app/config" | ||||
| 	"SafelineAPI/internal/Func" | ||||
| 	"SafelineAPI/internal/app/SafelineRequest" | ||||
| 	"SafelineAPI/internal/app/logger" | ||||
| 	"SafelineAPI/internal/app/safeLineApi" | ||||
| 	"SafelineAPI/pkg/moudle" | ||||
| 	"SafelineAPI/pkg/services" | ||||
| 	"SafelineAPI/pkg/utils" | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"SafelineAPI/internal/class/config" | ||||
| 	"SafelineAPI/internal/moudle" | ||||
| 	"log" | ||||
| 	"os" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	logger.LogInit() | ||||
|  | ||||
| 	var conf config.Config | ||||
| 	if len(os.Args) == 1 || os.Args[1][0] == '-' { | ||||
| 		conf.Command() | ||||
| @ -22,39 +20,42 @@ func main() { | ||||
| 		conf.Read(os.Args[1]) | ||||
| 	} | ||||
|  | ||||
| 	err := os.MkdirAll(conf.SavePath, os.ModePerm) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("创建文件夹 %s%s%s 时发生错误: %s%s%s", logger.Cyan, conf.SavePath, logger.Reset, logger.Red, err.Error(), logger.Reset) | ||||
| 	certUpsert, update := Func.UpdateCertCheck(conf) | ||||
| 	if update { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	urlStr := conf.Url() | ||||
| 	certNodes := utils.GetList(urlStr, conf.ApiToken.String()) | ||||
| 	certUpsert := moudle.CheckNodes(certNodes, conf.Days) | ||||
| 	p, err := moudle.ChooseDNSProvider(conf.DNSProviderConfig) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Println(fmt.Sprintf("验证 DNS 服务提供商时发生错误: %s%s%s", logger.Red, err, logger.Reset)) | ||||
| 		os.Exit(0) | ||||
| 	providers, provider := Func.ChooseProvider(conf.Acme) | ||||
| 	if provider { | ||||
| 		return | ||||
| 	} | ||||
| 	log.Printf("本次需要更新证书数量有 %d 个", len(certUpsert)) | ||||
|  | ||||
| 	var email string | ||||
| 	var failedApply [][]string | ||||
| 	var successApply [][]string | ||||
|  | ||||
| 	var taskChan = make(chan struct{}, conf.Concurrency) | ||||
|  | ||||
| 	for _, cert := range certUpsert { | ||||
| 		if services.ApplyCert(cert.Domains, conf.ApplyCert.Email, conf.SavePath, p) { | ||||
| 			failedApply = append(failedApply, cert.Domains) | ||||
| 			continue | ||||
| 		} | ||||
| 		var upsertReq safeLineApi.UpsertReq | ||||
| 		upsertReq.Create(cert.Domains, conf.ApplyCert.Email, conf.SavePath, cert.Id, cert.Type) | ||||
| 		body := bytes.NewReader(upsertReq.Marshal()) | ||||
| 		result := utils.Upsert(urlStr, conf.ApiToken.String(), body) | ||||
| 		if result.Msg != "" { | ||||
| 			logger.Error.Printf("域名 %s%s%s 证书更新失败: %s%s%s", logger.Cyan, cert.Domains, logger.Reset, logger.Red, result.Msg, logger.Reset) | ||||
| 			failedApply = append(failedApply, cert.Domains) | ||||
| 			continue | ||||
| 		} | ||||
| 		successApply = append(successApply, cert.Domains) | ||||
| 		logger.Success.Printf("域名 %s%s%s 证书更新成功!", logger.Cyan, cert.Domains, logger.Reset) | ||||
| 		taskChan <- struct{}{} | ||||
| 		go func(cert SafelineRequest.Nodes) { | ||||
| 			Certificate, PrivateKey, status := moudle.ApplyCert(cert.Domains, conf.Acme.Email, providers[0]) | ||||
| 			if status { | ||||
| 				failedApply = append(failedApply, cert.Domains) | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			success, failed := moudle.UpdateCert(conf, email, Certificate, PrivateKey, cert) | ||||
| 			if success != nil { | ||||
| 				successApply = append(successApply, success) | ||||
| 			} | ||||
| 			if failed != nil { | ||||
| 				failedApply = append(failedApply, failed) | ||||
| 			} | ||||
| 			<-taskChan | ||||
| 		}(cert) | ||||
| 	} | ||||
|  | ||||
| 	if len(successApply) != 0 { | ||||
| 		log.Printf("本次成功更新的域名证书如下: %s%s%s", logger.Cyan, successApply, logger.Reset) | ||||
| 	} | ||||
| @ -63,5 +64,4 @@ func main() { | ||||
| 		log.Printf("未成功更新的域名证书如下: %s%s%s", logger.Cyan, failedApply, logger.Reset) | ||||
| 	} | ||||
| 	log.Printf("本次任务执行完成") | ||||
| 	_ = os.RemoveAll(conf.SavePath) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user