80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package caddyplesk
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/libdns/libdns"
|
|
|
|
libdnsplesk "git.ryanpalmer.tech/Ryan/libdns-plesk"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(Provider{})
|
|
}
|
|
|
|
// Provider is the Caddy module that allows DNS challenges via Plesk.
|
|
type Provider struct {
|
|
// We'll embed or reference the libdns Provider
|
|
DNSProvider libdnsplesk.Provider `json:"-"`
|
|
|
|
// Or we store these fields separately and pass them to DNSProvider in Provision()
|
|
BaseURL string `json:"base_url,omitempty"`
|
|
SecretToken string `json:"secret_token,omitempty"`
|
|
}
|
|
|
|
// CaddyModule returns the module information.
|
|
func (p Provider) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "tls.dns.plesk",
|
|
New: func() caddy.Module { return new(Provider) },
|
|
}
|
|
}
|
|
|
|
// Provision sets up the module.
|
|
func (p *Provider) Provision(ctx caddy.Context) error {
|
|
if p.BaseURL == "" {
|
|
return fmt.Errorf("plesk: base_url must be provided")
|
|
}
|
|
if p.SecretToken == "" {
|
|
return fmt.Errorf("plesk: secret_token must be provided")
|
|
}
|
|
|
|
p.DNSProvider.BaseURL = p.BaseURL
|
|
p.DNSProvider.SecretToken = p.SecretToken
|
|
|
|
// any other initialization logic
|
|
return nil
|
|
}
|
|
|
|
// Present adds the ACME challenge TXT record.
|
|
func (p *Provider) Present(ctx context.Context, domain, token, keyAuth string) error {
|
|
// Usually the record name: _acme-challenge.<domain>
|
|
// For DNS challenge, the record value is the keyAuth digest
|
|
recordName := "_acme-challenge." + domain
|
|
|
|
txtRecord := libdns.Record{
|
|
Type: "TXT",
|
|
Name: recordName,
|
|
Value: keyAuth,
|
|
}
|
|
|
|
_, err := p.DNSProvider.AppendRecords(ctx, domain, []libdns.Record{txtRecord})
|
|
return err
|
|
}
|
|
|
|
// CleanUp removes the ACME challenge TXT record.
|
|
func (p *Provider) CleanUp(ctx context.Context, domain, token, keyAuth string) error {
|
|
recordName := "_acme-challenge." + domain
|
|
|
|
txtRecord := libdns.Record{
|
|
Type: "TXT",
|
|
Name: recordName,
|
|
Value: keyAuth,
|
|
}
|
|
|
|
_, err := p.DNSProvider.DeleteRecords(ctx, domain, []libdns.Record{txtRecord})
|
|
return err
|
|
}
|