Add ACME DNS01 challenge support via libdns

This commit is contained in:
世界 2023-09-16 21:37:22 +08:00
parent c1ffcf365e
commit d17e93384b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
10 changed files with 170 additions and 2 deletions

View file

@ -1,5 +1,11 @@
package option
import (
"github.com/sagernet/sing-box/common/json"
C "github.com/sagernet/sing-box/constant"
E "github.com/sagernet/sing/common/exceptions"
)
type InboundACMEOptions struct {
Domain Listable[string] `json:"domain,omitempty"`
DataDirectory string `json:"data_directory,omitempty"`
@ -11,9 +17,62 @@ type InboundACMEOptions struct {
AlternativeHTTPPort uint16 `json:"alternative_http_port,omitempty"`
AlternativeTLSPort uint16 `json:"alternative_tls_port,omitempty"`
ExternalAccount *ACMEExternalAccountOptions `json:"external_account,omitempty"`
DNS01Challenge *ACMEDNS01ChallengeOptions `json:"dns01_challenge,omitempty"`
}
type ACMEExternalAccountOptions struct {
KeyID string `json:"key_id,omitempty"`
MACKey string `json:"mac_key,omitempty"`
}
type _ACMEDNS01ChallengeOptions struct {
Provider string `json:"provider,omitempty"`
AliDNSOptions ACMEDNS01AliDNSOptions `json:"-"`
CloudflareOptions ACMEDNS01CloudflareOptions `json:"-"`
}
type ACMEDNS01ChallengeOptions _ACMEDNS01ChallengeOptions
func (o ACMEDNS01ChallengeOptions) MarshalJSON() ([]byte, error) {
var v any
switch o.Provider {
case C.DNSProviderAliDNS:
v = o.AliDNSOptions
case C.DNSProviderCloudflare:
v = o.CloudflareOptions
default:
return nil, E.New("unknown provider type: " + o.Provider)
}
return MarshallObjects((_ACMEDNS01ChallengeOptions)(o), v)
}
func (o *ACMEDNS01ChallengeOptions) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_ACMEDNS01ChallengeOptions)(o))
if err != nil {
return err
}
var v any
switch o.Provider {
case C.DNSProviderAliDNS:
v = &o.AliDNSOptions
case C.DNSProviderCloudflare:
v = &o.CloudflareOptions
default:
return E.New("unknown provider type: " + o.Provider)
}
err = UnmarshallExcluded(bytes, (*_ACMEDNS01ChallengeOptions)(o), v)
if err != nil {
return E.Cause(err, "DNS01 challenge options")
}
return nil
}
type ACMEDNS01AliDNSOptions struct {
AccessKeyID string `json:"access_key_id,omitempty"`
AccessKeySecret string `json:"access_key_secret,omitempty"`
RegionID string `json:"region_id,omitempty"`
}
type ACMEDNS01CloudflareOptions struct {
APIToken string `json:"api_token,omitempty"`
}