2022-11-10 09:44:38 +00:00
|
|
|
package ssl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
2024-05-07 10:26:34 +00:00
|
|
|
"os"
|
2024-02-22 07:32:07 +00:00
|
|
|
"time"
|
|
|
|
|
2023-11-14 10:44:09 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-11-24 16:08:44 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
|
|
|
"github.com/go-acme/lego/v4/acme/api"
|
2022-11-10 09:44:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
|
|
"github.com/go-acme/lego/v4/challenge"
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/lego"
|
2022-11-16 02:31:35 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/alidns"
|
2022-11-25 08:23:24 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
|
2022-11-10 09:44:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/dnspod"
|
2023-11-17 09:28:09 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/godaddy"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/namecheap"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/namedotcom"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/namesilo"
|
2024-01-22 14:28:52 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/tencentcloud"
|
2022-11-24 06:58:29 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/http/webroot"
|
2022-11-10 09:44:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/registration"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AcmeUser struct {
|
|
|
|
Email string
|
|
|
|
Registration *registration.Resource
|
2022-11-11 09:41:39 +00:00
|
|
|
Key crypto.PrivateKey
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AcmeUser) GetEmail() string {
|
|
|
|
return u.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AcmeUser) GetRegistration() *registration.Resource {
|
|
|
|
return u.Registration
|
|
|
|
}
|
|
|
|
func (u *AcmeUser) GetPrivateKey() crypto.PrivateKey {
|
2022-11-11 09:41:39 +00:00
|
|
|
return u.Key
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AcmeClient struct {
|
|
|
|
Config *lego.Config
|
|
|
|
Client *lego.Client
|
|
|
|
User *AcmeUser
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:44:09 +00:00
|
|
|
func NewAcmeClient(acmeAccount *model.WebsiteAcmeAccount) (*AcmeClient, error) {
|
|
|
|
if acmeAccount.Email == "" {
|
2022-11-10 09:44:38 +00:00
|
|
|
return nil, errors.New("email can not blank")
|
|
|
|
}
|
2023-11-14 10:44:09 +00:00
|
|
|
client, err := NewRegisterClient(acmeAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
2023-11-14 10:44:09 +00:00
|
|
|
return client, nil
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DnsType string
|
|
|
|
|
|
|
|
const (
|
2024-01-22 14:28:52 +00:00
|
|
|
DnsPod DnsType = "DnsPod"
|
|
|
|
AliYun DnsType = "AliYun"
|
|
|
|
CloudFlare DnsType = "CloudFlare"
|
|
|
|
NameSilo DnsType = "NameSilo"
|
|
|
|
NameCheap DnsType = "NameCheap"
|
|
|
|
NameCom DnsType = "NameCom"
|
|
|
|
Godaddy DnsType = "Godaddy"
|
|
|
|
TencentCloud DnsType = "TencentCloud"
|
2022-11-10 09:44:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DNSParam struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
AccessKey string `json:"accessKey"`
|
|
|
|
SecretKey string `json:"secretKey"`
|
|
|
|
Email string `json:"email"`
|
2023-11-17 09:28:09 +00:00
|
|
|
APIkey string `json:"apiKey"`
|
|
|
|
APIUser string `json:"apiUser"`
|
|
|
|
APISecret string `json:"apiSecret"`
|
2024-01-22 14:28:52 +00:00
|
|
|
SecretID string `json:"secretID"`
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 07:15:14 +00:00
|
|
|
var (
|
|
|
|
propagationTimeout = 30 * time.Minute
|
|
|
|
pollingInterval = 10 * time.Second
|
|
|
|
ttl = 3600
|
|
|
|
)
|
|
|
|
|
2024-05-07 10:26:34 +00:00
|
|
|
func (c *AcmeClient) UseDns(dnsType DnsType, params string, websiteSSL model.WebsiteSSL) error {
|
2023-11-14 10:44:09 +00:00
|
|
|
var (
|
|
|
|
param DNSParam
|
|
|
|
p challenge.Provider
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(params), ¶m); err != nil {
|
2022-11-10 09:44:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:44:09 +00:00
|
|
|
switch dnsType {
|
|
|
|
case DnsPod:
|
2022-11-10 09:44:38 +00:00
|
|
|
dnsPodConfig := dnspod.NewDefaultConfig()
|
|
|
|
dnsPodConfig.LoginToken = param.ID + "," + param.Token
|
2024-05-30 07:15:14 +00:00
|
|
|
dnsPodConfig.PropagationTimeout = propagationTimeout
|
|
|
|
dnsPodConfig.PollingInterval = pollingInterval
|
|
|
|
dnsPodConfig.TTL = ttl
|
2022-11-10 09:44:38 +00:00
|
|
|
p, err = dnspod.NewDNSProviderConfig(dnsPodConfig)
|
2023-11-14 10:44:09 +00:00
|
|
|
case AliYun:
|
2022-11-16 02:31:35 +00:00
|
|
|
alidnsConfig := alidns.NewDefaultConfig()
|
|
|
|
alidnsConfig.SecretKey = param.SecretKey
|
|
|
|
alidnsConfig.APIKey = param.AccessKey
|
2024-05-30 07:15:14 +00:00
|
|
|
alidnsConfig.PropagationTimeout = propagationTimeout
|
|
|
|
alidnsConfig.PollingInterval = pollingInterval
|
|
|
|
alidnsConfig.TTL = ttl
|
2022-11-16 02:31:35 +00:00
|
|
|
p, err = alidns.NewDNSProviderConfig(alidnsConfig)
|
2023-11-14 10:44:09 +00:00
|
|
|
case CloudFlare:
|
2022-11-25 08:23:24 +00:00
|
|
|
cloudflareConfig := cloudflare.NewDefaultConfig()
|
|
|
|
cloudflareConfig.AuthEmail = param.Email
|
2023-12-04 07:44:10 +00:00
|
|
|
cloudflareConfig.AuthToken = param.APIkey
|
2024-05-30 07:15:14 +00:00
|
|
|
cloudflareConfig.PropagationTimeout = propagationTimeout
|
|
|
|
cloudflareConfig.PollingInterval = pollingInterval
|
2023-11-17 09:28:09 +00:00
|
|
|
cloudflareConfig.TTL = 3600
|
2022-11-25 08:23:24 +00:00
|
|
|
p, err = cloudflare.NewDNSProviderConfig(cloudflareConfig)
|
2023-11-17 09:28:09 +00:00
|
|
|
case NameCheap:
|
|
|
|
namecheapConfig := namecheap.NewDefaultConfig()
|
|
|
|
namecheapConfig.APIKey = param.APIkey
|
|
|
|
namecheapConfig.APIUser = param.APIUser
|
2024-05-30 07:15:14 +00:00
|
|
|
namecheapConfig.PropagationTimeout = propagationTimeout
|
|
|
|
namecheapConfig.PollingInterval = pollingInterval
|
|
|
|
namecheapConfig.TTL = ttl
|
2023-11-17 09:28:09 +00:00
|
|
|
p, err = namecheap.NewDNSProviderConfig(namecheapConfig)
|
|
|
|
case NameSilo:
|
|
|
|
nameSiloConfig := namesilo.NewDefaultConfig()
|
|
|
|
nameSiloConfig.APIKey = param.APIkey
|
2024-05-30 07:15:14 +00:00
|
|
|
nameSiloConfig.PropagationTimeout = propagationTimeout
|
|
|
|
nameSiloConfig.PollingInterval = pollingInterval
|
|
|
|
nameSiloConfig.TTL = ttl
|
2023-11-17 09:28:09 +00:00
|
|
|
p, err = namesilo.NewDNSProviderConfig(nameSiloConfig)
|
|
|
|
case Godaddy:
|
|
|
|
godaddyConfig := godaddy.NewDefaultConfig()
|
|
|
|
godaddyConfig.APIKey = param.APIkey
|
|
|
|
godaddyConfig.APISecret = param.APISecret
|
2024-05-30 07:15:14 +00:00
|
|
|
godaddyConfig.PropagationTimeout = propagationTimeout
|
|
|
|
godaddyConfig.PollingInterval = pollingInterval
|
|
|
|
godaddyConfig.TTL = ttl
|
2023-11-17 09:28:09 +00:00
|
|
|
p, err = godaddy.NewDNSProviderConfig(godaddyConfig)
|
|
|
|
case NameCom:
|
|
|
|
nameComConfig := namedotcom.NewDefaultConfig()
|
|
|
|
nameComConfig.APIToken = param.Token
|
|
|
|
nameComConfig.Username = param.APIUser
|
2024-05-30 07:15:14 +00:00
|
|
|
nameComConfig.PropagationTimeout = propagationTimeout
|
|
|
|
nameComConfig.PollingInterval = pollingInterval
|
|
|
|
nameComConfig.TTL = ttl
|
2023-11-17 09:28:09 +00:00
|
|
|
p, err = namedotcom.NewDNSProviderConfig(nameComConfig)
|
2024-01-22 14:28:52 +00:00
|
|
|
case TencentCloud:
|
|
|
|
tencentCloudConfig := tencentcloud.NewDefaultConfig()
|
|
|
|
tencentCloudConfig.SecretID = param.SecretID
|
|
|
|
tencentCloudConfig.SecretKey = param.SecretKey
|
2024-05-30 07:15:14 +00:00
|
|
|
tencentCloudConfig.PropagationTimeout = propagationTimeout
|
|
|
|
tencentCloudConfig.PollingInterval = pollingInterval
|
|
|
|
tencentCloudConfig.TTL = ttl
|
2024-01-22 14:28:52 +00:00
|
|
|
p, err = tencentcloud.NewDNSProviderConfig(tencentCloudConfig)
|
2023-11-17 09:28:09 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-25 08:23:24 +00:00
|
|
|
}
|
2024-05-07 10:26:34 +00:00
|
|
|
var nameservers []string
|
|
|
|
if websiteSSL.Nameserver1 != "" {
|
|
|
|
nameservers = append(nameservers, websiteSSL.Nameserver1)
|
|
|
|
}
|
|
|
|
if websiteSSL.Nameserver2 != "" {
|
|
|
|
nameservers = append(nameservers, websiteSSL.Nameserver2)
|
|
|
|
}
|
|
|
|
if websiteSSL.DisableCNAME {
|
|
|
|
_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "true")
|
|
|
|
} else {
|
|
|
|
_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "false")
|
|
|
|
}
|
2022-11-10 09:44:38 +00:00
|
|
|
|
2024-05-07 10:01:00 +00:00
|
|
|
return c.Client.Challenge.SetDNS01Provider(p,
|
|
|
|
dns01.CondOption(len(nameservers) > 0,
|
|
|
|
dns01.AddRecursiveNameservers(nameservers)),
|
2024-05-07 10:26:34 +00:00
|
|
|
dns01.CondOption(websiteSSL.SkipDNS,
|
2024-05-07 10:01:00 +00:00
|
|
|
dns01.DisableCompletePropagationRequirement()),
|
|
|
|
dns01.AddDNSTimeout(10*time.Minute),
|
|
|
|
)
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
2022-11-16 02:31:35 +00:00
|
|
|
|
2023-03-16 10:44:32 +00:00
|
|
|
func (c *AcmeClient) UseManualDns() error {
|
2022-11-16 02:31:35 +00:00
|
|
|
p := &manualDnsProvider{}
|
2023-11-17 09:28:09 +00:00
|
|
|
if err := c.Client.Challenge.SetDNS01Provider(p, dns01.AddDNSTimeout(10*time.Minute)); err != nil {
|
2023-03-16 10:44:32 +00:00
|
|
|
return err
|
2022-11-16 02:31:35 +00:00
|
|
|
}
|
2023-03-16 10:44:32 +00:00
|
|
|
return nil
|
2022-11-16 02:31:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 06:58:29 +00:00
|
|
|
func (c *AcmeClient) UseHTTP(path string) error {
|
|
|
|
httpProvider, err := webroot.NewHTTPProvider(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-10 09:44:38 +00:00
|
|
|
|
2022-11-24 06:58:29 +00:00
|
|
|
err = c.Client.Challenge.SetHTTP01Provider(httpProvider)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 06:38:07 +00:00
|
|
|
func (c *AcmeClient) ObtainSSL(domains []string, privateKey crypto.PrivateKey) (certificate.Resource, error) {
|
2022-11-10 09:44:38 +00:00
|
|
|
request := certificate.ObtainRequest{
|
2023-11-16 06:38:07 +00:00
|
|
|
Domains: domains,
|
|
|
|
Bundle: true,
|
|
|
|
PrivateKey: privateKey,
|
2022-11-10 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certificates, err := c.Client.Certificate.Obtain(request)
|
|
|
|
if err != nil {
|
|
|
|
return certificate.Resource{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *certificates, nil
|
|
|
|
}
|
2022-11-16 02:31:35 +00:00
|
|
|
|
|
|
|
type Resolve struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
2022-11-24 16:08:44 +00:00
|
|
|
Err string
|
2022-11-16 02:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type manualDnsProvider struct {
|
|
|
|
Resolve *Resolve
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *manualDnsProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *manualDnsProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-24 16:08:44 +00:00
|
|
|
func (c *AcmeClient) GetDNSResolve(domains []string) (map[string]Resolve, error) {
|
|
|
|
core, err := api.New(c.Config.HTTPClient, c.Config.UserAgent, c.Config.CADirURL, c.User.Registration.URI, c.User.Key)
|
|
|
|
if err != nil {
|
2023-03-11 13:46:43 +00:00
|
|
|
return nil, err
|
2022-11-24 16:08:44 +00:00
|
|
|
}
|
|
|
|
order, err := core.Orders.New(domains)
|
|
|
|
if err != nil {
|
2023-03-11 13:46:43 +00:00
|
|
|
return nil, err
|
2022-11-24 16:08:44 +00:00
|
|
|
}
|
|
|
|
resolves := make(map[string]Resolve)
|
|
|
|
resc, errc := make(chan acme.Authorization), make(chan domainError)
|
|
|
|
for _, authzURL := range order.Authorizations {
|
|
|
|
go func(authzURL string) {
|
|
|
|
authz, err := core.Authorizations.Get(authzURL)
|
|
|
|
if err != nil {
|
|
|
|
errc <- domainError{Domain: authz.Identifier.Value, Error: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resc <- authz
|
|
|
|
}(authzURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responses []acme.Authorization
|
|
|
|
for i := 0; i < len(order.Authorizations); i++ {
|
|
|
|
select {
|
|
|
|
case res := <-resc:
|
|
|
|
responses = append(responses, res)
|
|
|
|
case err := <-errc:
|
|
|
|
resolves[err.Domain] = Resolve{Err: err.Error.Error()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(resc)
|
|
|
|
close(errc)
|
|
|
|
|
|
|
|
for _, auth := range responses {
|
|
|
|
domain := challenge.GetTargetedDomain(auth)
|
|
|
|
chlng, err := challenge.FindChallenge(challenge.DNS01, auth)
|
|
|
|
if err != nil {
|
|
|
|
resolves[domain] = Resolve{Err: err.Error()}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keyAuth, err := core.GetKeyAuthorization(chlng.Token)
|
|
|
|
if err != nil {
|
|
|
|
resolves[domain] = Resolve{Err: err.Error()}
|
|
|
|
continue
|
|
|
|
}
|
2023-11-16 06:38:07 +00:00
|
|
|
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-11-24 16:08:44 +00:00
|
|
|
resolves[domain] = Resolve{
|
2023-11-16 06:38:07 +00:00
|
|
|
Key: challengeInfo.FQDN,
|
|
|
|
Value: challengeInfo.Value,
|
2022-11-24 16:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-16 02:31:35 +00:00
|
|
|
|
2022-11-24 16:08:44 +00:00
|
|
|
return resolves, nil
|
2022-11-16 02:31:35 +00:00
|
|
|
}
|