重构项目
This commit is contained in:
		
							
								
								
									
										61
									
								
								internal/moudle/ApplyCert.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								internal/moudle/ApplyCert.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,61 @@ | ||||
| package moudle | ||||
|  | ||||
| import ( | ||||
| 	"SafelineAPI/internal/app/logger" | ||||
| 	"SafelineAPI/internal/class/MyUser" | ||||
| 	"crypto/ecdsa" | ||||
| 	"crypto/elliptic" | ||||
| 	"crypto/rand" | ||||
|  | ||||
| 	"github.com/go-acme/lego/v4/certcrypto" | ||||
| 	"github.com/go-acme/lego/v4/certificate" | ||||
| 	"github.com/go-acme/lego/v4/challenge" | ||||
| 	"github.com/go-acme/lego/v4/lego" | ||||
| 	"github.com/go-acme/lego/v4/registration" | ||||
| ) | ||||
|  | ||||
| func ApplyCert(domains []string, email string, provider challenge.Provider) ([]byte, []byte, bool) { | ||||
| 	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, domains, logger.Reset, logger.Red, err, logger.Reset) | ||||
| 		return nil, nil, true | ||||
| 	} | ||||
|  | ||||
| 	myUser := MyUser.MyUser{ | ||||
| 		Email: email, | ||||
| 		Key:   privateKey, | ||||
| 	} | ||||
|  | ||||
| 	config := lego.NewConfig(&myUser) | ||||
| 	config.Certificate.KeyType = certcrypto.RSA2048 | ||||
| 	client, err := lego.NewClient(config) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, domains, logger.Reset, logger.Red, err, logger.Reset) | ||||
| 		return nil, nil, true | ||||
| 	} | ||||
| 	err = client.Challenge.SetDNS01Provider(provider) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, domains, logger.Reset, logger.Red, err, logger.Reset) | ||||
| 		return nil, nil, true | ||||
| 	} | ||||
|  | ||||
| 	reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true}) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, domains, logger.Reset, logger.Red, err, logger.Reset) | ||||
| 		return nil, nil, true | ||||
| 	} | ||||
|  | ||||
| 	myUser.Registration = reg | ||||
| 	request := certificate.ObtainRequest{ | ||||
| 		Domains: domains, | ||||
| 		Bundle:  true, | ||||
| 	} | ||||
|  | ||||
| 	certResource, err := client.Certificate.Obtain(request) | ||||
| 	if err != nil { | ||||
| 		logger.Error.Printf("申请 %s%s%s 证书时发生错误: %s%s%s", logger.Cyan, domains, logger.Reset, logger.Red, err, logger.Reset) | ||||
| 		return nil, nil, true | ||||
| 	} | ||||
|  | ||||
| 	return certResource.Certificate, certResource.PrivateKey, false | ||||
| } | ||||
		Reference in New Issue
	
	Block a user