迁移并重构项目,优化了执行流程
This commit is contained in:
		
							
								
								
									
										71
									
								
								pkg/utils/apply.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								pkg/utils/apply.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto"
 | 
			
		||||
	"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, error) {
 | 
			
		||||
 | 
			
		||||
	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user := RegistrationUser{
 | 
			
		||||
		Email: email,
 | 
			
		||||
		Key:   privateKey,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config := lego.NewConfig(&user)
 | 
			
		||||
	config.Certificate.KeyType = certcrypto.RSA2048
 | 
			
		||||
	client, err := lego.NewClient(config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, err
 | 
			
		||||
	}
 | 
			
		||||
	err = client.Challenge.SetDNS01Provider(provider)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	request := certificate.ObtainRequest{
 | 
			
		||||
		Domains: domains,
 | 
			
		||||
		Bundle:  true,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	certResource, err := client.Certificate.Obtain(request)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return certResource.Certificate, certResource.PrivateKey, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type RegistrationUser struct {
 | 
			
		||||
	Email        string
 | 
			
		||||
	Registration *registration.Resource
 | 
			
		||||
	Key          crypto.PrivateKey
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (registrationUser *RegistrationUser) GetEmail() string {
 | 
			
		||||
	return registrationUser.Email
 | 
			
		||||
}
 | 
			
		||||
func (registrationUser *RegistrationUser) GetRegistration() *registration.Resource {
 | 
			
		||||
	return registrationUser.Registration
 | 
			
		||||
}
 | 
			
		||||
func (registrationUser *RegistrationUser) GetPrivateKey() crypto.PrivateKey {
 | 
			
		||||
	return registrationUser.Key
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								pkg/utils/kvp.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								pkg/utils/kvp.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type KVPair map[string]string
 | 
			
		||||
 | 
			
		||||
func (kvp *KVPair) Set(str string) {
 | 
			
		||||
	kvps := strings.Split(str, ",")
 | 
			
		||||
	for _, i := range kvps {
 | 
			
		||||
		kv := strings.SplitN(i, "=", 2)
 | 
			
		||||
		(*kvp)[kv[0]] = kv[1]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								pkg/utils/request.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								pkg/utils/request.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Request(method, urlStr string, body io.Reader, header http.Header) (*http.Response, error) {
 | 
			
		||||
	req, _ := http.NewRequest(method, urlStr, body)
 | 
			
		||||
	req.Header = header
 | 
			
		||||
	client := http.Client{
 | 
			
		||||
		Transport: &http.Transport{
 | 
			
		||||
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user