| 1 | package acme |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/go-acme/lego/v4/challenge" |
| 9 | |
| 10 | "github.com/gosuda/portal-tunnel/v2/portal/acme/cloudflare" |
| 11 | "github.com/gosuda/portal-tunnel/v2/portal/acme/gcloud" |
| 12 | "github.com/gosuda/portal-tunnel/v2/portal/acme/hetzner" |
| 13 | "github.com/gosuda/portal-tunnel/v2/portal/acme/njalla" |
| 14 | "github.com/gosuda/portal-tunnel/v2/portal/acme/route53" |
| 15 | "github.com/gosuda/portal-tunnel/v2/portal/acme/vultr" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | TypeCloudflare = "cloudflare" |
| 20 | TypeGCloud = "gcloud" |
| 21 | TypeHetzner = "hetzner" |
| 22 | TypeNjalla = "njalla" |
| 23 | TypeRoute53 = "route53" |
| 24 | TypeVultr = "vultr" |
| 25 | ) |
| 26 | |
| 27 | type DNSProvider interface { |
| 28 | Name() string |
| 29 | ChallengeProvider(ctx context.Context) (challenge.Provider, error) |
| 30 | EnsureARecords(ctx context.Context, baseDomain, publicIPv4 string) error |
| 31 | EnsureARecord(ctx context.Context, name, publicIPv4 string) error |
| 32 | DeleteARecord(ctx context.Context, name string) error |
| 33 | EnsureTXTRecord(ctx context.Context, name, value string) error |
| 34 | DeleteTXTRecords(ctx context.Context, name, matchPrefix string) error |
| 35 | EnsureHTTPSRecord(ctx context.Context, name string, priority uint16, target, svcParams, content string) error |
| 36 | DeleteHTTPSRecord(ctx context.Context, name string) error |
| 37 | EnsureDNSSEC(ctx context.Context, baseDomain string) (state, dsRecord, message string, err error) |
| 38 | } |
| 39 | |
| 40 | func NewDNSProvider(providerType string, cfg Config) (DNSProvider, error) { |
| 41 | switch strings.ToLower(strings.TrimSpace(providerType)) { |
| 42 | case "": |
| 43 | return nil, nil |
| 44 | case TypeCloudflare: |
| 45 | return cloudflare.New(cfg.CloudflareToken), nil |
| 46 | case TypeGCloud: |
| 47 | return gcloud.New(gcloud.Config{ |
| 48 | ProjectID: cfg.GCPProjectID, |
| 49 | ManagedZone: cfg.GCPManagedZone, |
| 50 | }), nil |
| 51 | case TypeHetzner: |
| 52 | return hetzner.New(cfg.HetznerAPIToken), nil |
| 53 | case TypeNjalla: |
| 54 | return njalla.New(cfg.NjallaToken), nil |
| 55 | case TypeRoute53: |
| 56 | return route53.New(route53.Config{ |
| 57 | AccessKeyID: cfg.AWSAccessKeyID, |
| 58 | SecretAccessKey: cfg.AWSSecretAccessKey, |
| 59 | SessionToken: cfg.AWSSessionToken, |
| 60 | Region: cfg.AWSRegion, |
| 61 | HostedZoneID: cfg.AWSHostedZoneID, |
| 62 | KMSKeyARN: cfg.AWSKMSKeyARN, |
| 63 | }), nil |
| 64 | case TypeVultr: |
| 65 | return vultr.New(cfg.VultrAPIKey), nil |
| 66 | default: |
| 67 | return nil, fmt.Errorf("unsupported acme dns provider: %q", providerType) |
| 68 | } |
| 69 | } |