71 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"SafeLine-Acme/internal/Func"
 | 
						|
	"SafeLine-Acme/internal/app/logger"
 | 
						|
	"SafeLine-Acme/internal/class/Config"
 | 
						|
	"SafeLine-Acme/internal/class/SafeLine"
 | 
						|
	"SafeLine-Acme/pkg/utils"
 | 
						|
	"log"
 | 
						|
	"os"
 | 
						|
)
 | 
						|
 | 
						|
func main() {
 | 
						|
	logger.LogInit()
 | 
						|
 | 
						|
	var conf Config.Object
 | 
						|
	if len(os.Args) == 1 || os.Args[1][0] == '-' {
 | 
						|
		conf.Command()
 | 
						|
	} else {
 | 
						|
		conf.Read(os.Args[1])
 | 
						|
	}
 | 
						|
 | 
						|
	if !conf.Server.Verify() {
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	certUpsert, noUpdate := Func.UpdateCertCheck(conf.Server, conf.Days)
 | 
						|
	if noUpdate {
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	providers, noProvider := Func.ChooseProvider(conf.Acme)
 | 
						|
	if noProvider {
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	var failedApply [][]string
 | 
						|
	var successApply [][]string
 | 
						|
 | 
						|
	var taskChan = make(chan struct{}, conf.Concurrency)
 | 
						|
 | 
						|
	for _, cert := range certUpsert {
 | 
						|
		taskChan <- struct{}{}
 | 
						|
		go func(cert SafeLine.Nodes, server SafeLine.Object, email string) {
 | 
						|
			Certificate, PrivateKey, err := utils.ApplyCert(cert.Domains, email, providers[0])
 | 
						|
			if err != nil {
 | 
						|
				failedApply = append(failedApply, cert.Domains)
 | 
						|
				logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, cert.Domains, logger.Reset, logger.Red, err, logger.Reset)
 | 
						|
			}
 | 
						|
 | 
						|
			success, failed := server.UpdateCert(email, Certificate, PrivateKey, cert)
 | 
						|
			if success != nil {
 | 
						|
				successApply = append(successApply, success)
 | 
						|
			}
 | 
						|
			if failed != nil {
 | 
						|
				failedApply = append(failedApply, failed)
 | 
						|
			}
 | 
						|
			<-taskChan
 | 
						|
		}(cert, conf.Server, conf.Acme.Email)
 | 
						|
	}
 | 
						|
 | 
						|
	if len(successApply) != 0 {
 | 
						|
		log.Printf("本次成功更新的域名证书如下: %s%s%s", logger.Cyan, successApply, logger.Reset)
 | 
						|
	}
 | 
						|
 | 
						|
	if len(failedApply) != 0 {
 | 
						|
		log.Printf("未成功更新的域名证书如下: %s%s%s", logger.Cyan, failedApply, logger.Reset)
 | 
						|
	}
 | 
						|
	log.Printf("本次任务执行完成")
 | 
						|
}
 |